/
Bench.java
156 lines (137 loc) · 5.15 KB
/
Bench.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.jayway.jsonpath.web.bench;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import io.gatling.jsonpath.JsonPath$;
import org.boon.json.JsonParser;
import org.boon.json.ObjectMapper;
import org.boon.json.implementation.JsonParserCharArray;
import org.boon.json.implementation.ObjectMapperImpl;
import scala.collection.Iterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Bench {
protected final String json;
protected final String path;
private final boolean optionAsValues;
private final boolean flagWrap;
private final boolean flagSuppress;
private final boolean flagNullLeaf;
private final boolean flagRequireProps;
public Bench(String json, String path, boolean optionAsValues, boolean flagWrap, boolean flagSuppress, boolean flagNullLeaf, boolean flagRequireProps) {
this.json = json;
this.path = path;
this.optionAsValues = optionAsValues;
this.flagWrap = flagWrap;
this.flagSuppress = flagSuppress;
this.flagNullLeaf = flagNullLeaf;
this.flagRequireProps = flagRequireProps;
}
public Result runJayway() {
String result = null;
String error = null;
long time;
Object res = null;
Configuration configuration = Configuration.defaultConfiguration();
if(flagWrap){
configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST);
}
if(flagSuppress){
configuration = configuration.addOptions(Option.SUPPRESS_EXCEPTIONS);
}
if (!optionAsValues) {
configuration = configuration.addOptions(Option.AS_PATH_LIST);
}
if(flagNullLeaf){
configuration = configuration.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
}
if(flagRequireProps){
configuration = configuration.addOptions(Option.REQUIRE_PROPERTIES);
}
long now = System.currentTimeMillis();
try {
res = JsonPath.using(configuration).parse(json).read(path);
} catch (Exception e) {
error = getError(e);
} finally {
time = System.currentTimeMillis() - now;
if (res instanceof String) {
result = "\"" + res + "\"";
} else if (res instanceof Number) {
result = res.toString();
} else if (res instanceof Boolean) {
result = res.toString();
} else {
result = res != null ? Configuration.defaultConfiguration().jsonProvider().toJson(res) : "null";
}
return new Result("jayway", time, result, error);
}
}
public Result runBoon() {
String result = null;
String error = null;
long time;
Iterator<Object> query = null;
long now = System.currentTimeMillis();
try {
if (!optionAsValues) {
throw new UnsupportedOperationException("Not supported!");
}
io.gatling.jsonpath.JsonPath jsonPath = JsonPath$.MODULE$.compile(path).right().get();
JsonParser jsonParser = new JsonParserCharArray();
Object jsonModel = jsonParser.parse(json);
query = jsonPath.query(jsonModel);
} catch (Exception e) {
error = getError(e);
} finally {
time = System.currentTimeMillis() - now;
if (query != null) {
List<Object> res = new ArrayList<Object>();
while (query.hasNext()) {
res.add(query.next());
}
ObjectMapper mapper = new ObjectMapperImpl();
result = mapper.toJson(res);
}
return new Result("boon", time, result, error);
}
}
public Result runNebhale() {
String result = null;
String error = null;
long time;
Object res = null;
JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
long now = System.currentTimeMillis();
try {
if (!optionAsValues) {
throw new UnsupportedOperationException("Not supported!");
}
com.nebhale.jsonpath.JsonPath compiled = com.nebhale.jsonpath.JsonPath.compile(path);
res = compiled.read(json, Object.class);
} catch (Exception e) {
error = getError(e);
} finally {
time = System.currentTimeMillis() - now;
result = res != null ? jacksonProvider.toJson(res) : null;
return new Result("nebhale", time, result, error);
}
}
public Map<String, Result> runAll() {
Map<String, Result> res = new HashMap<String, Result>();
res.put("jayway", runJayway());
res.put("boon", runBoon());
res.put("nebhale", runNebhale());
return res;
}
private String getError(Exception e) {
String ex = e.getMessage();
if (ex == null || ex.trim().isEmpty()) {
ex = "Undefined error";
}
return ex;
}
}