|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.tinkerpop.gremlin.process; |
| 20 | + |
| 21 | +import org.apache.tinkerpop.benchmark.util.AbstractBenchmarkBase; |
| 22 | +import org.apache.tinkerpop.gremlin.process.traversal.P; |
| 23 | +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; |
| 24 | +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; |
| 25 | +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; |
| 26 | +import org.apache.tinkerpop.gremlin.structure.Graph; |
| 27 | +import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.Level; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Scope; |
| 32 | +import org.openjdk.jmh.annotations.Setup; |
| 33 | +import org.openjdk.jmh.annotations.State; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal; |
| 40 | + |
| 41 | +/** |
| 42 | + * Benchmark for measuring the performance of applying strategies to a complex traversal. |
| 43 | + */ |
| 44 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 45 | +@State(Scope.Thread) |
| 46 | +public class ApplyStrategiesBenchmark extends AbstractBenchmarkBase { |
| 47 | + |
| 48 | + private Graph graph = EmptyGraph.instance(); |
| 49 | + private GraphTraversalSource g = traversal().withEmbedded(graph); |
| 50 | + private Traversal<?, ?> traversal; |
| 51 | + |
| 52 | + /** |
| 53 | + * Setup method that constructs the complex traversal before benchmarking on each invocation of the test. |
| 54 | + */ |
| 55 | + @Setup(Level.Invocation) |
| 56 | + public void setup() { |
| 57 | + traversal = g.V().or( |
| 58 | + __.has("name", P.within(new HashSet<>(Arrays.asList("DARK STAR", "ST. STEPHEN", "CHINA CAT SUNFLOWER")))), |
| 59 | + __.has("songType", P.eq("cover"))).where( |
| 60 | + __.coalesce( |
| 61 | + __.where( |
| 62 | + __.union( |
| 63 | + __.as("a").inE("sungBy").choose( |
| 64 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 65 | + ).outV().filter(__.has("weight", P.lt(1))), |
| 66 | + __.as("a").outE("followedBy").choose( |
| 67 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 68 | + ).inV().where( |
| 69 | + __.coalesce( |
| 70 | + __.where( |
| 71 | + __.union( |
| 72 | + __.as("a").outE("followedBy").choose( |
| 73 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 74 | + ).inV().has("songType", P.neq("cover")).where( |
| 75 | + __.coalesce( |
| 76 | + __.where( |
| 77 | + __.union( |
| 78 | + __.as("a").outE("followedBy").choose( |
| 79 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 80 | + ).inV().where( |
| 81 | + __.coalesce( |
| 82 | + __.where( |
| 83 | + __.union( |
| 84 | + __.as("a").outE("followedBy").choose( |
| 85 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 86 | + ).inV().where( |
| 87 | + __.coalesce( |
| 88 | + __.where( |
| 89 | + __.union( |
| 90 | + __.as("a").outE("followedBy").choose( |
| 91 | + __.has("weight"), __.has("weight", P.gt(1)), __.identity() |
| 92 | + ).inV().has("songType", P.within("original")) |
| 93 | + ) |
| 94 | + ) |
| 95 | + ) |
| 96 | + ) |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + ) |
| 106 | + ) |
| 107 | + ) |
| 108 | + ) |
| 109 | + ).dedup().select("a") |
| 110 | + ) |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected int getWarmupIterations() { |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected int getForks() { |
| 121 | + return 1; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Benchmark that measures only the time it takes to apply strategies to a complex traversal. |
| 126 | + */ |
| 127 | + @Benchmark |
| 128 | + public Traversal testLockTraversal() { |
| 129 | + if (traversal.asAdmin().isLocked()) |
| 130 | + throw new RuntimeException("Traversal is locked - that shouldn't be possible if the traversal is new"); |
| 131 | + traversal.asAdmin().applyStrategies(); |
| 132 | + return traversal; |
| 133 | + } |
| 134 | +} |
0 commit comments