1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.examples.jmh.euclidean;
18
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.apache.commons.geometry.euclidean.twod.LineConvexSubset;
23 import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
24 import org.apache.commons.geometry.euclidean.twod.Vector2D;
25 import org.apache.commons.geometry.euclidean.twod.shape.Circle;
26 import org.apache.commons.numbers.core.Precision;
27 import org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.BenchmarkMode;
29 import org.openjdk.jmh.annotations.Fork;
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
42 @BenchmarkMode(Mode.AverageTime)
43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
44 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
45 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
46 @Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"})
47 public class RegionBSPTree2DPerformance {
48
49
50
51 @State(Scope.Thread)
52 public static class CircularBoundaryInputBase {
53
54
55 @Param({"10", "20", "50"})
56 private int segments;
57
58
59
60
61 protected List<LineConvexSubset> computeBoundaries() {
62 final Circle circle = Circle.from(Vector2D.ZERO, 1, Precision.doubleEquivalenceOfEpsilon(1e-10));
63 return circle.toTree(segments).getBoundaries();
64 }
65 }
66
67
68
69 @State(Scope.Thread)
70 public static class CircularBoundaryInput extends CircularBoundaryInputBase {
71
72
73 private List<LineConvexSubset> boundaries;
74
75
76 @Setup(Level.Iteration)
77 public void setup() {
78 boundaries = computeBoundaries();
79 }
80
81
82
83
84 public List<LineConvexSubset> getBoundaries() {
85 return boundaries;
86 }
87 }
88
89
90
91
92 @State(Scope.Thread)
93 public static class WorstCaseCircularRegionInput extends CircularBoundaryInputBase {
94
95
96 private RegionBSPTree2D tree;
97
98
99 @Setup(Level.Iteration)
100 public void setup() {
101 tree = RegionBSPTree2D.empty();
102 tree.insert(computeBoundaries());
103 }
104
105
106
107
108 public RegionBSPTree2D getTree() {
109 return tree;
110 }
111 }
112
113
114
115
116
117
118
119 @Benchmark
120 public RegionBSPTree2D insertConvexWorstCase(final CircularBoundaryInput input) {
121 final RegionBSPTree2D tree = RegionBSPTree2D.empty();
122
123 for (final LineConvexSubset boundary : input.getBoundaries()) {
124 tree.insert(boundary);
125 }
126
127 return tree;
128 }
129
130
131
132
133
134
135 @Benchmark
136 public List<LineConvexSubset> boundaryConvexWorstCase(final WorstCaseCircularRegionInput input) {
137 return input.getTree().getBoundaries();
138 }
139 }