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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.yahoo.test.json.Jackson;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.Integer;
/**
* @author baldersheim
*/
public class JsonBenchmark {
private static byte[] createJson(int numElements) {
Slime slime = new Slime();
Cursor a = slime.setArray();
for (int i = 0; i < numElements; i++) {
Cursor e = a.addObject();
e.setString("key", "i");
e.setLong("weight", i);
}
ByteArrayOutputStream bs = new ByteArrayOutputStream();
JsonFormat json = new JsonFormat(false);
try {
json.encode(bs, slime);
} catch (IOException e) {
throw new RuntimeException(e);
}
return bs.toByteArray();
}
private static long benchmarkJacksonStreaming(byte[] json, int numIterations) {
long count = 0;
JsonFactory jsonFactory = new JsonFactory();
try {
for (int i = 0; i < numIterations; i++) {
try (JsonParser jsonParser = jsonFactory.createParser(json)) {
JsonToken array = jsonParser.nextToken();
for (JsonToken token = jsonParser.nextToken(); !JsonToken.END_ARRAY.equals(token); token = jsonParser.nextToken()) {
if (JsonToken.FIELD_NAME.equals(token) && "weight".equals(jsonParser.getCurrentName())) {
token = jsonParser.nextToken();
count += jsonParser.getLongValue();
}
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return count;
}
private static long benchmarkJacksonTree(byte[] json, int numIterations) {
long count = 0;
// use the ObjectMapper to read the json string and create a tree
var mapper = Jackson.mapper();
try {
for (int i = 0; i < numIterations; i++) {
JsonNode node = mapper.readTree(json);
for(JsonNode item : node) {
count += item.get("weight").asLong();
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return count;
}
private static long benchmarkSlime(byte[] json, int numIterations) {
long count = 0;
for (int i = 0; i < numIterations; i++) {
JsonDecoder decoder = new JsonDecoder();
Slime slime = decoder.decode(new Slime(), json);
Cursor array = slime.get();
int weightSymbol = slime.lookup("weight");
for (int j=0, m=slime.get().children(); j < m; j++) {
count += array.entry(j).field(weightSymbol).asLong();
}
}
return count;
}
private static void warmup(byte[] json) {
System.out.println(System.currentTimeMillis() + " Warming up");
benchmarkSlime(json, 5000);
System.out.println(System.currentTimeMillis() + " Done Warming up");
}
/**
* jacksons 1000 40000 = 5.6 seconds
* jacksont 1000 40000 = 11.0 seconds
* slime 1000 40000 = 17.5 seconds
* @param argv type, num elements in weigted set, num iterations
*/
static public void main(String[] argv) {
String type = argv[0];
byte[] json = createJson(Integer.parseInt(argv[1]));
warmup(json);
int count = Integer.parseInt(argv[2]);
System.out.println(System.currentTimeMillis() + " Start");
long start = System.currentTimeMillis();
long numValues;
if ("jacksons".equals(type)) {
numValues = benchmarkJacksonStreaming(json, count);
} else if ("jacksont".equals(type)) {
numValues = benchmarkJacksonTree(json, count);
} else{
numValues = benchmarkSlime(json, count);
}
System.out.println(System.currentTimeMillis() + " End with " + numValues + " values in " + (System.currentTimeMillis() - start) + " milliseconds.");
}
}
|