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.threed.PlaneConvexSubset;
23 import org.apache.commons.geometry.euclidean.threed.RegionBSPTree3D;
24 import org.apache.commons.geometry.euclidean.threed.Vector3D;
25 import org.apache.commons.geometry.euclidean.threed.shape.Sphere;
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 RegionBSPTree3DPerformance {
48
49
50
51 @State(Scope.Thread)
52 public static class SphericalBoundaryInputBase {
53
54
55
56
57 @Param({"2", "3", "4"})
58 private int subdivisions;
59
60
61
62
63 protected List<PlaneConvexSubset> computeBoundaries() {
64 final Sphere sphere = Sphere.from(Vector3D.ZERO, 1, Precision.doubleEquivalenceOfEpsilon(1e-10));
65 return sphere.toTree(subdivisions).getBoundaries();
66 }
67 }
68
69
70
71 @State(Scope.Thread)
72 public static class SphericalBoundaryInput extends SphericalBoundaryInputBase {
73
74
75 private List<PlaneConvexSubset> boundaries;
76
77
78 @Setup(Level.Iteration)
79 public void setup() {
80 boundaries = computeBoundaries();
81 }
82
83
84
85
86 public List<PlaneConvexSubset> getBoundaries() {
87 return boundaries;
88 }
89 }
90
91
92
93
94 @State(Scope.Thread)
95 public static class WorstCaseSphericalRegionInput extends SphericalBoundaryInputBase {
96
97
98 private RegionBSPTree3D tree;
99
100
101 @Setup(Level.Iteration)
102 public void setup() {
103 tree = RegionBSPTree3D.empty();
104 tree.insert(computeBoundaries());
105 }
106
107
108
109
110 public RegionBSPTree3D getTree() {
111 return tree;
112 }
113 }
114
115
116
117
118
119
120
121 @Benchmark
122 public RegionBSPTree3D insertConvexWorstCase(final SphericalBoundaryInput input) {
123 final RegionBSPTree3D tree = RegionBSPTree3D.empty();
124
125 for (final PlaneConvexSubset boundary : input.getBoundaries()) {
126 tree.insert(boundary);
127 }
128
129 return tree;
130 }
131
132
133
134
135
136
137 @Benchmark
138 public List<PlaneConvexSubset> boundaryConvexWorstCase(final WorstCaseSphericalRegionInput input) {
139 return input.getTree().getBoundaries();
140 }
141 }