/
ActiveJDirectScopebindBenchmark.java
289 lines (229 loc) · 6.05 KB
/
ActiveJDirectScopebindBenchmark.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package io.activej.inject;
import io.activej.common.ApplicationSettings;
import io.activej.inject.annotation.Inject;
import io.activej.inject.module.Module;
import io.activej.inject.module.ModuleBuilder;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import java.util.concurrent.TimeUnit;
/**
* @since 3.0.0
*/
@State(org.openjdk.jmh.annotations.Scope.Benchmark)
@SuppressWarnings("FieldCanBeLocal")
public class ActiveJDirectScopebindBenchmark {
public static final boolean SPECIALIZE = ApplicationSettings.getBoolean(ActiveJDiScopesBenchmark.class, "specialize", false);
static class Kitchen {
private final int places;
@Inject
Kitchen() {
this.places = 1;
}
public int getPlaces() {
return places;
}
}
static class Sugar {
private final String name;
private final float weight;
public Sugar() {
this.name = "WhiteSugar";
this.weight = 10.f;
}
//[END REGION_8]
public Sugar(String name, float weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public float getWeight() {
return weight;
}
}
static class Butter {
private final float weight;
private final String name;
public Butter() {
this.weight = 10.f;
this.name = "Butter";
}
public Butter(String name, float weight) {
this.weight = weight;
this.name = name;
}
public float getWeight() {
return weight;
}
public String getName() {
return name;
}
}
static class Flour {
private float weight;
private String name;
public Flour() { }
public Flour(String name, float weight) {
this.weight = weight;
this.name = name;
}
public float getWeight() {
return weight;
}
public String getName() {
return name;
}
}
static class Pastry {
private final Sugar sugar;
private final Butter butter;
private final Flour flour;
Pastry(Sugar sugar, Butter butter, Flour flour) {
this.sugar = sugar;
this.butter = butter;
this.flour = flour;
}
public Flour getFlour() {
return flour;
}
public Sugar getSugar() {
return sugar;
}
public Butter getButter() {
return butter;
}
}
static class Cookie1 {
private final Pastry pastry;
Cookie1(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class Cookie2 {
private final Pastry pastry;
Cookie2(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class Cookie3 {
private final Pastry pastry;
Cookie3(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class Cookie4 {
private final Pastry pastry;
Cookie4(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class Cookie5 {
private final Pastry pastry;
Cookie5(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class Cookie6 {
private final Pastry pastry;
Cookie6(Pastry pastry) {
this.pastry = pastry;
}
public Pastry getPastry() {
return pastry;
}
}
static class CookieBucket {
private final Cookie1 c1;
private final Cookie2 c2;
private final Cookie3 c3;
private final Cookie4 c4;
private final Cookie5 c5;
private final Cookie6 c6;
public Cookie4 getC4() {
return c4;
}
CookieBucket(Cookie1 c1, Cookie2 c2, Cookie3 c3, Cookie4 c4, Cookie5 c5, Cookie6 c6) {
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
this.c4 = c4;
this.c5 = c5;
this.c6 = c6;
}
}
Module cookbook;
Injector injector;
public static final Scope ORDER_SCOPE = Scope.of(OrderScope.class);
@Setup
public void setup() {
cookbook = ModuleBuilder.create()
.bind(Kitchen.class).to(Kitchen::new)
.bind(Sugar.class).to(() -> new Sugar("WhiteSugar", 10.f)).in(OrderScope.class)
.bind(Butter.class).to(() -> new Butter("PerfectButter", 20.0f)).in(OrderScope.class)
.bind(Flour.class).to(() -> new Flour("GoodFlour", 100.0f)).in(OrderScope.class)
.bind(Pastry.class).to(Pastry::new, Sugar.class, Butter.class, Flour.class).in(OrderScope.class)
.bind(Cookie1.class).to(Cookie1::new, Pastry.class).in(OrderScope.class)
.bind(Cookie2.class).to(Cookie2::new, Pastry.class).in(OrderScope.class)
.bind(Cookie3.class).to(Cookie3::new, Pastry.class).in(OrderScope.class)
.bind(Cookie4.class).to(Cookie4::new, Pastry.class).in(OrderScope.class)
.bind(Cookie5.class).to(Cookie5::new, Pastry.class).in(OrderScope.class)
.bind(Cookie6.class).to(Cookie6::new, Pastry.class).in(OrderScope.class)
.bind(CookieBucket.class).to(CookieBucket::new, Cookie1.class, Cookie2.class,
Cookie3.class, Cookie4.class, Cookie5.class, Cookie6.class).in(OrderScope.class)
.build();
if (SPECIALIZE) {
Injector.useSpecializer();
}
injector = Injector.of(cookbook);
}
CookieBucket cb;
final Key<CookieBucket> key = Key.of(CookieBucket.class);
@Param({"1", "10"})
int arg;
@Benchmark
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
public void measure(Blackhole blackhole) {
Kitchen kitchen = injector.getInstance(Kitchen.class);
for (int i = 0; i < arg; ++i) {
Injector subInjector = injector.enterScope(ORDER_SCOPE);
cb = subInjector.getInstance(key);
blackhole.consume(cb);
}
blackhole.consume(kitchen);
}
public static void main(String[] args) throws RunnerException {
System.out.println("Running benchmark with specialization " + (SPECIALIZE ? "ON" : "OFF"));
Options opt = new OptionsBuilder()
.include(ActiveJDirectScopebindBenchmark.class.getSimpleName())
.forks(2)
.warmupIterations(3)
.warmupTime(TimeValue.seconds(1L))
.measurementIterations(10)
.measurementTime(TimeValue.seconds(2L))
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.NANOSECONDS)
.build();
new Runner(opt).run();
}
}