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.concurrent.TimeUnit;
20
21 import org.apache.commons.geometry.euclidean.twod.RegionBSPTree2D;
22 import org.apache.commons.geometry.euclidean.twod.Vector2D;
23 import org.apache.commons.geometry.euclidean.twod.shape.Circle;
24 import org.apache.commons.numbers.core.Precision;
25 import org.apache.commons.rng.UniformRandomProvider;
26 import org.apache.commons.rng.simple.RandomSource;
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 CirclePerformance {
48
49
50 private static final double EPS = 1e-10;
51
52
53 private static final double MIN_VALUE = 1e-1;
54
55
56 private static final double MAX_VALUE = 1e-100;
57
58
59
60 @State(Scope.Thread)
61 public static class RandomCircle {
62
63
64 private Circle circle;
65
66
67 @Setup(Level.Iteration)
68 public void setup() {
69 circle = randomCircle(RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP));
70 }
71
72
73
74
75 public Circle getCircle() {
76 return circle;
77 }
78 }
79
80
81
82 @State(Scope.Thread)
83 public static class ToTreeInput {
84
85
86 @Param({"10", "100", "1000"})
87 private int segments;
88
89
90
91
92 public int getSegments() {
93 return segments;
94 }
95 }
96
97
98
99 @State(Scope.Thread)
100 public static class ToTreeInstance extends ToTreeInput {
101
102
103 private RegionBSPTree2D tree;
104
105
106 @Setup(Level.Iteration)
107 public void setup() {
108 final Circle circle = randomCircle(RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP));
109
110 tree = circle.toTree(getSegments());
111 }
112
113
114
115
116 public RegionBSPTree2D getTree() {
117 return tree;
118 }
119 }
120
121
122
123
124
125
126 private static Circle randomCircle(final UniformRandomProvider rand) {
127 final Vector2D center = Vector2D.of(nextDouble(rand), nextDouble(rand));
128 final double radius = nextDouble(rand);
129
130 return Circle.from(center, radius, Precision.doubleEquivalenceOfEpsilon(EPS));
131 }
132
133
134
135
136
137 private static double nextDouble(final UniformRandomProvider rand) {
138 return (rand.nextDouble() * (MAX_VALUE - MIN_VALUE)) + MIN_VALUE;
139 }
140
141
142
143
144
145
146 @Benchmark
147 public RegionBSPTree2D toTreeCreation(final RandomCircle randomCircle, final ToTreeInput toTreeInput) {
148 final Circle circle = randomCircle.getCircle();
149 final int segments = toTreeInput.getSegments();
150
151 return circle.toTree(segments);
152 }
153
154
155
156
157
158
159 @Benchmark
160 public double toTreeSize(final ToTreeInstance toTreeInstance) {
161 final RegionBSPTree2D tree = toTreeInstance.getTree();
162
163 return tree.getSize();
164 }
165 }