|
| 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 | +} |
0 commit comments