Skip to content

Commit 841b90d

Browse files
committed
minor improvements to manual micro-benchmarks
1 parent 1e6e497 commit 841b90d

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

src/test/java/perf/ManualCharAccessTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private void test() throws Exception
112112
private final long readClassic(int REPS, char[] input, char[] output) throws Exception
113113
{
114114
long start = System.currentTimeMillis();
115-
final byte[] codes = BYTE_CODES;
115+
final byte[] codes = SMALL_BYTE_CODES;
116116
final int MAX = 256;
117117

118118
while (--REPS >= 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package perf;
2+
3+
import com.fasterxml.jackson.core.*;
4+
5+
/**
6+
* Manually run micro-benchmark for checking performance of tokenizing
7+
* simple tokens (false, true, null).
8+
*/
9+
public class ManualSmallTokenRead extends ParserTestBase
10+
{
11+
protected final JsonFactory _factory;
12+
13+
protected final byte[] _jsonBytes;
14+
protected final char[] _jsonChars;
15+
16+
private ManualSmallTokenRead(JsonFactory f, String json) throws Exception {
17+
_factory = f;
18+
_jsonChars = json.toCharArray();
19+
_jsonBytes = json.getBytes("UTF-8");
20+
}
21+
22+
public static void main(String[] args) throws Exception
23+
{
24+
if (args.length != 0) {
25+
System.err.println("Usage: java ...");
26+
System.exit(1);
27+
}
28+
final JsonFactory f = new JsonFactory();
29+
final String jsonStr = aposToQuotes(
30+
"{'data':[true,false,null,false,null,true],'last':true}"
31+
);
32+
new ManualSmallTokenRead(f, jsonStr).test("char[]", "byte[]", jsonStr.length());
33+
}
34+
35+
@Override
36+
protected void testRead1(int reps) throws Exception
37+
{
38+
while (--reps >= 0) {
39+
JsonParser p = _factory.createParser(_jsonChars);
40+
_stream(p);
41+
p.close();
42+
}
43+
}
44+
45+
@Override
46+
protected void testRead2(int reps) throws Exception
47+
{
48+
while (--reps >= 0) {
49+
JsonParser p = _factory.createParser(_jsonBytes);
50+
_stream(p);
51+
p.close();
52+
}
53+
}
54+
55+
private final void _stream(JsonParser p) throws Exception
56+
{
57+
JsonToken t;
58+
59+
while ((t = p.nextToken()) != null) {
60+
// force decoding/reading of scalar values too (booleans are fine, nulls too)
61+
if (t == JsonToken.VALUE_STRING) {
62+
p.getText();
63+
} else if (t == JsonToken.VALUE_NUMBER_INT) {
64+
p.getLongValue();
65+
}
66+
}
67+
}
68+
}

src/test/java/perf/ParserTestBase.java

+5
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ protected long _testRead2(int reps) throws Exception {
8383
protected abstract void testRead1(int reps) throws Exception;
8484

8585
protected abstract void testRead2(int reps) throws Exception;
86+
87+
protected static String aposToQuotes(String json) {
88+
return json.replace("'", "\"");
89+
}
8690
}
91+

0 commit comments

Comments
 (0)