/
ManualIntRead.java
69 lines (60 loc) · 1.9 KB
/
ManualIntRead.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
package perf;
import tools.jackson.core.*;
import tools.jackson.core.json.JsonFactory;
/**
* Manually run micro-benchmark for checking performance of tokenizing
* simple tokens (false, true, null).
*/
public class ManualIntRead extends ManualPerfTestBase
{
protected final JsonFactory _factory;
protected final byte[] _jsonBytes;
protected final char[] _jsonChars;
private ManualIntRead(JsonFactory f, String json) throws Exception {
_factory = f;
_jsonChars = json.toCharArray();
_jsonBytes = json.getBytes("UTF-8");
}
public static void main(String[] args) throws Exception
{
if (args.length != 0) {
System.err.println("Usage: java ...");
System.exit(1);
}
final JsonFactory f = new JsonFactory();
final String jsonStr = a2q(
"{'data':[1,-2,138,-78,0,12435,-12,-9],'last':12345}"
);
new ManualIntRead(f, jsonStr).test("char[]", "byte[]", jsonStr.length());
}
@Override
protected void testRead1(int reps) throws Exception
{
while (--reps >= 0) {
JsonParser p = _factory.createParser(ObjectReadContext.empty(), _jsonChars);
_stream(p);
p.close();
}
}
@Override
protected void testRead2(int reps) throws Exception
{
while (--reps >= 0) {
JsonParser p = _factory.createParser(ObjectReadContext.empty(), _jsonBytes);
_stream(p);
p.close();
}
}
private final void _stream(JsonParser p) throws Exception
{
JsonToken t;
while ((t = p.nextToken()) != null) {
// force decoding/reading of scalar values too (booleans are fine, nulls too)
if (t == JsonToken.VALUE_STRING) {
p.getText();
} else if (t == JsonToken.VALUE_NUMBER_INT) {
p.getIntValue();
}
}
}
}