generated from creek-service/single-module-template
/
JsonValidateBenchmark.java
315 lines (256 loc) · 10.7 KB
/
JsonValidateBenchmark.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2023 Creek Contributors (https://github.com/creek-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.creekservice.kafka.test.perf.performance;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import org.creekservice.api.test.util.TestPaths;
import org.creekservice.kafka.test.perf.implementations.DevHarrelImplementation;
import org.creekservice.kafka.test.perf.implementations.EveritImplementation;
import org.creekservice.kafka.test.perf.implementations.Implementation;
import org.creekservice.kafka.test.perf.implementations.JustifyImplementation;
import org.creekservice.kafka.test.perf.implementations.MedeiaImplementation;
import org.creekservice.kafka.test.perf.implementations.NetworkNtImplementation;
import org.creekservice.kafka.test.perf.implementations.SchemaFriendImplementation;
import org.creekservice.kafka.test.perf.implementations.SkemaImplementation;
import org.creekservice.kafka.test.perf.implementations.SnowImplementation;
import org.creekservice.kafka.test.perf.implementations.VertxImplementation;
import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite;
import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite.Result;
import org.creekservice.kafka.test.perf.testsuite.SchemaSpec;
import org.creekservice.kafka.test.perf.testsuite.TestCase;
import org.creekservice.kafka.test.perf.testsuite.TestSuiteLoader;
import org.creekservice.kafka.test.perf.util.Logging;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
/**
* Benchmark results for JSON Schema validation.
*
* <p>The benchmark runs each validator through the <a
* href="https://github.com/json-schema-org/JSON-Schema-Test-Suite">standard set of tests</a>
*
* <p>The results show the average time it takes each impl to run through the test suite, per draft.
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MILLISECONDS)
@Threads(1) // GitHub linux runners have two cores, so running more threads is pointless.
@Fork(6) // Note: to debug, set fork to 0.
// @Warmup(iterations = 0, time = 10)
// @Measurement(iterations = 1, time = 10)
@SuppressWarnings({"FieldMayBeFinal", "MethodName"}) // not final to avoid folding.
public class JsonValidateBenchmark {
public static final JsonSchemaTestSuite TEST_SUITE =
new TestSuiteLoader(p -> true)
.load(
TestPaths.moduleRoot("json-schema-validation-comparison")
.resolve("build/json-schema-test-suite"));
static {
Logging.disable();
}
public static class MedeiaValidator extends ValidatorState {
public MedeiaValidator() {
super(new MedeiaImplementation());
}
}
@Benchmark
public Result measureDraft_04_Medeia(final MedeiaValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_06_Medeia(final MedeiaValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_Medeia(final MedeiaValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
public static class EveritValidator extends ValidatorState {
public EveritValidator() {
super(new EveritImplementation());
}
}
@Benchmark
public Result measureDraft_04_Everit(final EveritValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_06_Everit(final EveritValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_Everit(final EveritValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
public static class SkemaValidator extends ValidatorState {
public SkemaValidator() {
super(new SkemaImplementation());
}
}
@Benchmark
public Result measureDraft_2020_12_Skema(final SkemaValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2020_12);
}
public static class VertxValidator extends ValidatorState {
public VertxValidator() {
super(new VertxImplementation());
}
}
@Benchmark
public Result measureDraft_04_Vertx(final VertxValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_07_Vertx(final VertxValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
@Benchmark
public Result measureDraft_2019_09_Vertx(final VertxValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2019_09);
}
@Benchmark
public Result measureDraft_2020_12_Vertx(final VertxValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2020_12);
}
public static class SchemaFriendValidator extends ValidatorState {
public SchemaFriendValidator() {
super(new SchemaFriendImplementation());
}
}
@Benchmark
public Result measureDraft_03_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_03);
}
@Benchmark
public Result measureDraft_04_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_06_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
@Benchmark
public Result measureDraft_2019_09_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2019_09);
}
@Benchmark
public Result measureDraft_2020_12_SchemaFriend(final SchemaFriendValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2020_12);
}
public static class NetworkNtValidator extends ValidatorState {
public NetworkNtValidator() {
super(new NetworkNtImplementation());
}
}
@Benchmark
public Result measureDraft_04_NetworkNt(final NetworkNtValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_06_NetworkNt(final NetworkNtValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_NetworkNt(final NetworkNtValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
@Benchmark
public Result measureDraft_2019_09_NetworkNt(final NetworkNtValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2019_09);
}
@Benchmark
public Result measureDraft_2020_12_NetworkNt(final NetworkNtValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2020_12);
}
public static class SnowValidator extends ValidatorState {
public SnowValidator() {
super(new SnowImplementation());
}
}
@Benchmark
public Result measureDraft_06_Snow(final SnowValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_Snow(final SnowValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
@Benchmark
public Result measureDraft_2019_09_Snow(final SnowValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2019_09);
}
public static class JustifyValidator extends ValidatorState {
public JustifyValidator() {
super(new JustifyImplementation());
}
}
@Benchmark
public Result measureDraft_04_Justify(final JustifyValidator validator) {
return validator.validate(SchemaSpec.DRAFT_04);
}
@Benchmark
public Result measureDraft_06_Justify(final JustifyValidator validator) {
return validator.validate(SchemaSpec.DRAFT_06);
}
@Benchmark
public Result measureDraft_07_Justify(final JustifyValidator validator) {
return validator.validate(SchemaSpec.DRAFT_07);
}
public static class DevHarrelValidator extends ValidatorState {
public DevHarrelValidator() {
super(new DevHarrelImplementation());
}
}
@Benchmark
public Result measureDraft_2019_09_DevHarrel(final DevHarrelValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2019_09);
}
@Benchmark
public Result measureDraft_2020_12_DevHarrel(final DevHarrelValidator validator) {
return validator.validate(SchemaSpec.DRAFT_2020_12);
}
@State(Scope.Benchmark)
@SuppressWarnings("FieldMayBeFinal") // not final to avoid folding.
abstract static class ValidatorState {
private final JsonSchemaTestSuite.Runner runner;
protected ValidatorState(final Implementation implementation) {
runner = TEST_SUITE.prepare(implementation, new ValidatorState.PreTestPredicate());
}
public Result validate(final SchemaSpec spec) {
return runner.run(spec::equals);
}
private static final class PreTestPredicate implements JsonSchemaTestSuite.TestPredicate {
@Override
public boolean test(final TestCase testCase) {
// Only test valid cases during performance testing,
// As the cost of error handling varies massively between impls.
// There is a strong correlation between that cost and the richness of error
// messages.
// Impls should not be penalised for rich error handling!
// Production use cases should almost never see validation errors, so its cost isn't
// that relevant.
return testCase.valid();
}
}
}
}