|
| 1 | +/* |
| 2 | + * Copyright 2025 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.common.geometry.benchmarks; |
| 17 | + |
| 18 | +import static com.google.common.geometry.TestDataGenerator.DEFAULT_LOOP_RADIUS; |
| 19 | +import static java.util.concurrent.TimeUnit.MICROSECONDS; |
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 21 | + |
| 22 | +import com.google.common.geometry.S2ContainsVertexQuery; |
| 23 | +import com.google.common.geometry.S2Loop; |
| 24 | +import com.google.common.geometry.S2Point; |
| 25 | +import com.google.common.geometry.S2Polygon; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 30 | +import org.openjdk.jmh.annotations.Level; |
| 31 | +import org.openjdk.jmh.annotations.Measurement; |
| 32 | +import org.openjdk.jmh.annotations.Mode; |
| 33 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 34 | +import org.openjdk.jmh.annotations.Param; |
| 35 | +import org.openjdk.jmh.annotations.Scope; |
| 36 | +import org.openjdk.jmh.annotations.Setup; |
| 37 | +import org.openjdk.jmh.annotations.State; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | + |
| 40 | +/** |
| 41 | + * A simple benchmark for S2ContainsVertexQuery. |
| 42 | + */ |
| 43 | +public class S2ContainsVertexQueryBenchmark { |
| 44 | + |
| 45 | + private S2ContainsVertexQueryBenchmark() {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Benchmark state consisting of a polygon of many triangles, all around a common center point, |
| 49 | + * forming a fan or windmill shape. |
| 50 | + */ |
| 51 | + @State(Scope.Thread) |
| 52 | + @BenchmarkMode(Mode.AverageTime) |
| 53 | + @OutputTimeUnit(MICROSECONDS) |
| 54 | + @Warmup(iterations = 3, time = 5, timeUnit = SECONDS) |
| 55 | + @Measurement(iterations = 5, time = 5, timeUnit = SECONDS) |
| 56 | + public static class PolygonState extends S2BenchmarkBaseState { |
| 57 | + @Param({"4", "8", "64", "512", "4096", "32768"}) |
| 58 | + int numTriangles; |
| 59 | + |
| 60 | + private S2Polygon polygon; |
| 61 | + private S2Point center; |
| 62 | + |
| 63 | + @Setup(Level.Trial) |
| 64 | + @Override |
| 65 | + public void setup() { |
| 66 | + super.setup(); |
| 67 | + center = data.getRandomPoint(); |
| 68 | + List<S2Point> regularVertices = S2Loop.makeRegularVertices( |
| 69 | + center, DEFAULT_LOOP_RADIUS, numTriangles * 2); |
| 70 | + |
| 71 | + List<S2Loop> loops = new ArrayList<>(); |
| 72 | + for (int tri = 0; tri < numTriangles; tri++) { |
| 73 | + List<S2Point> loopVertices = new ArrayList<>(); |
| 74 | + loopVertices.add(center); |
| 75 | + loopVertices.add(regularVertices.get(tri * 2 + 0)); |
| 76 | + loopVertices.add(regularVertices.get(tri * 2 + 1)); |
| 77 | + loops.add(new S2Loop(loopVertices)); |
| 78 | + } |
| 79 | + |
| 80 | + polygon = new S2Polygon(loops); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Measures the average time to create an S2ContainsVertexQuery, add all the incoming and |
| 85 | + * outgoing edges of the polygon (w.r.t. the center point), and determine if the center point is |
| 86 | + * contained or not. |
| 87 | + */ |
| 88 | + @Benchmark |
| 89 | + public int query() { |
| 90 | + S2ContainsVertexQuery query = new S2ContainsVertexQuery(center); |
| 91 | + |
| 92 | + for (S2Loop loop : polygon.getLoops()) { |
| 93 | + // Loop vertex 0 is the center point. |
| 94 | + query.addOutgoing(loop.vertex(1)); // The first loop edge is outgoing from the center. |
| 95 | + // The edge from vertex 1 to vertex 2 isn't incident on the center. |
| 96 | + query.addIncoming(loop.vertex(2)); // The third loop edge is incoming to the center. |
| 97 | + } |
| 98 | + |
| 99 | + return query.containsSign(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | +} |
0 commit comments