Skip to content

Commit 4d37c54

Browse files
committed
JsonArbitraryFieldNameBenchmark uses Supplier<byte[]>
1 parent e1a6928 commit 4d37c54

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

src/main/java/com/fasterxml/jackson/perf/json/JsonArbitraryFieldNameBenchmark.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.openjdk.jmh.annotations.Scope;
1919
import org.openjdk.jmh.annotations.Setup;
2020
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;
2124

2225
import java.io.ByteArrayInputStream;
2326
import java.io.IOException;
@@ -51,7 +54,8 @@ <F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory) {
5154
<F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory) {
5255
return factory.disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES);
5356
}
54-
};
57+
},
58+
;
5559

5660
abstract <F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory);
5761
}
@@ -62,38 +66,47 @@ <F extends JsonFactory, B extends TSFBuilder<F, B>> B apply(B factory) {
6266
public enum InputType {
6367
INPUT_STREAM() {
6468
@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()));
6771
}
6872
},
6973
READER() {
7074
@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
7377
// to more closely match overhead of INPUT_STREAM for comparison.
7478
return factory.createParser(new InputStreamReader(
75-
new ByteArrayInputStream(jsonSupplier.get().getBytes(StandardCharsets.UTF_8)),
79+
new ByteArrayInputStream(bytesSupplier.get()),
7680
StandardCharsets.UTF_8));
7781
}
78-
};
82+
},
83+
;
7984

80-
abstract JsonParser create(JsonFactory factory, Supplier<String> jsonSupplier) throws IOException;
85+
abstract JsonParser create(JsonFactory factory, Supplier<byte[]> jsonSupplier) throws IOException;
8186
}
8287

8388
public enum InputShape {
89+
KEY_MAP(
90+
new TypeReference<Map<String, Boolean>>() {
91+
},
92+
() -> "{\"key\":true}"),
8493
RANDOM_KEY_MAP(
85-
new TypeReference<Map<String, Boolean>>() {},
94+
new TypeReference<Map<String, Boolean>>() {
95+
},
8696
() -> "{\"" + ThreadLocalRandom.current().nextInt() + "\":true}"),
8797
BEAN_WITH_RANDOM_KEY_MAP(
88-
new TypeReference<SimpleClass>() {},
98+
new TypeReference<SimpleClass>() {
99+
},
89100
() -> "{\"fieldWithMap\":{\"" + ThreadLocalRandom.current().nextInt()
90-
+ "\":true},\"stringOne\":\"a\",\"stringTwo\":\"a\",\"stringThree\":\"a\"}");
101+
+ "\":true},\"stringOne\":\"a\",\"stringTwo\":\"a\",\"stringThree\":\"a\"}"),
102+
;
91103

92104
private final TypeReference<?> typereference;
93-
private final Supplier<String> jsonSupplier;
105+
private final Supplier<byte[]> bytesSupplier;
106+
94107
InputShape(TypeReference<?> typereference, Supplier<String> jsonSupplier) {
95108
this.typereference = typereference;
96-
this.jsonSupplier = jsonSupplier;
109+
this.bytesSupplier = () -> jsonSupplier.get().getBytes(StandardCharsets.UTF_8);
97110
}
98111
}
99112

@@ -121,7 +134,7 @@ public void setup() {
121134

122135
@Benchmark
123136
public Object parse() throws IOException {
124-
try (JsonParser parser = type.create(factory, shape.jsonSupplier)) {
137+
try (JsonParser parser = type.create(factory, shape.bytesSupplier)) {
125138
return reader.readValue(parser);
126139
}
127140
}
@@ -140,4 +153,16 @@ public static final class SimpleClass {
140153
@JsonProperty("stringThree")
141154
public String stringThree;
142155
}
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+
}
143168
}

0 commit comments

Comments
 (0)