18
18
import org .openjdk .jmh .annotations .Scope ;
19
19
import org .openjdk .jmh .annotations .Setup ;
20
20
import org .openjdk .jmh .annotations .State ;
21
+ import org .openjdk .jmh .runner .Runner ;
22
+ import org .openjdk .jmh .runner .options .OptionsBuilder ;
23
+ import org .openjdk .jmh .runner .options .TimeValue ;
21
24
22
25
import java .io .ByteArrayInputStream ;
23
26
import java .io .IOException ;
@@ -51,7 +54,8 @@ <F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory) {
51
54
<F extends JsonFactory , B extends TSFBuilder <F , B >> B apply (B factory ) {
52
55
return factory .disable (JsonFactory .Feature .CANONICALIZE_FIELD_NAMES );
53
56
}
54
- };
57
+ },
58
+ ;
55
59
56
60
abstract <F extends JsonFactory , B extends TSFBuilder <F , B >> B apply (B factory );
57
61
}
@@ -62,38 +66,47 @@ <F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory) {
62
66
public enum InputType {
63
67
INPUT_STREAM () {
64
68
@ Override
65
- JsonParser create (JsonFactory factory , Supplier <String > jsonSupplier ) throws IOException {
66
- return factory .createParser (new ByteArrayInputStream (jsonSupplier .get (). getBytes ( StandardCharsets . UTF_8 )));
69
+ JsonParser create (JsonFactory factory , Supplier <byte []> bytesSupplier ) throws IOException {
70
+ return factory .createParser (new ByteArrayInputStream (bytesSupplier .get ()));
67
71
}
68
72
},
69
73
READER () {
70
74
@ Override
71
- JsonParser create (JsonFactory factory , Supplier <String > jsonSupplier ) throws IOException {
72
- // Instead of using 'new StringReader(jsonSupplier .get())', we construct an InputStreamReader
75
+ JsonParser create (JsonFactory factory , Supplier <byte []> bytesSupplier ) throws IOException {
76
+ // Instead of using 'new StringReader(bytesSupplier .get())', we construct an InputStreamReader
73
77
// to more closely match overhead of INPUT_STREAM for comparison.
74
78
return factory .createParser (new InputStreamReader (
75
- new ByteArrayInputStream (jsonSupplier .get (). getBytes ( StandardCharsets . UTF_8 )),
79
+ new ByteArrayInputStream (bytesSupplier .get ()),
76
80
StandardCharsets .UTF_8 ));
77
81
}
78
- };
82
+ },
83
+ ;
79
84
80
- abstract JsonParser create (JsonFactory factory , Supplier <String > jsonSupplier ) throws IOException ;
85
+ abstract JsonParser create (JsonFactory factory , Supplier <byte [] > jsonSupplier ) throws IOException ;
81
86
}
82
87
83
88
public enum InputShape {
89
+ KEY_MAP (
90
+ new TypeReference <Map <String , Boolean >>() {
91
+ },
92
+ () -> "{\" key\" :true}" ),
84
93
RANDOM_KEY_MAP (
85
- new TypeReference <Map <String , Boolean >>() {},
94
+ new TypeReference <Map <String , Boolean >>() {
95
+ },
86
96
() -> "{\" " + ThreadLocalRandom .current ().nextInt () + "\" :true}" ),
87
97
BEAN_WITH_RANDOM_KEY_MAP (
88
- new TypeReference <SimpleClass >() {},
98
+ new TypeReference <SimpleClass >() {
99
+ },
89
100
() -> "{\" fieldWithMap\" :{\" " + ThreadLocalRandom .current ().nextInt ()
90
- + "\" :true},\" stringOne\" :\" a\" ,\" stringTwo\" :\" a\" ,\" stringThree\" :\" a\" }" );
101
+ + "\" :true},\" stringOne\" :\" a\" ,\" stringTwo\" :\" a\" ,\" stringThree\" :\" a\" }" ),
102
+ ;
91
103
92
104
private final TypeReference <?> typereference ;
93
- private final Supplier <String > jsonSupplier ;
105
+ private final Supplier <byte []> bytesSupplier ;
106
+
94
107
InputShape (TypeReference <?> typereference , Supplier <String > jsonSupplier ) {
95
108
this .typereference = typereference ;
96
- this .jsonSupplier = jsonSupplier ;
109
+ this .bytesSupplier = () -> jsonSupplier . get (). getBytes ( StandardCharsets . UTF_8 ) ;
97
110
}
98
111
}
99
112
@@ -121,7 +134,7 @@ public void setup() {
121
134
122
135
@ Benchmark
123
136
public Object parse () throws IOException {
124
- try (JsonParser parser = type .create (factory , shape .jsonSupplier )) {
137
+ try (JsonParser parser = type .create (factory , shape .bytesSupplier )) {
125
138
return reader .readValue (parser );
126
139
}
127
140
}
@@ -140,4 +153,16 @@ public static final class SimpleClass {
140
153
@ JsonProperty ("stringThree" )
141
154
public String stringThree ;
142
155
}
156
+
157
+ public static void main (String [] _args ) throws Exception {
158
+ new Runner (new OptionsBuilder ()
159
+ .include (JsonArbitraryFieldNameBenchmark .class .getName ())
160
+ .warmupIterations (2 )
161
+ .warmupTime (TimeValue .seconds (5 ))
162
+ .measurementIterations (4 )
163
+ .measurementTime (TimeValue .seconds (5 ))
164
+ .mode (Mode .AverageTime )
165
+ .forks (1 )
166
+ .build ()).run ();
167
+ }
143
168
}
0 commit comments