forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchGson.java
More file actions
296 lines (276 loc) · 8.04 KB
/
BenchGson.java
File metadata and controls
296 lines (276 loc) · 8.04 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.jsoniter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import com.jsoniter.extra.GsonCompatibilityMode;
import com.jsoniter.spi.DecodingMode;
import com.jsoniter.spi.JsoniterSpi;
import org.junit.Test;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.infra.Blackhole;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
@State(Scope.Thread)
public class BenchGson {
private GsonCompatibilityMode gsonCompatibilityMode;
private Gson gson;
@Setup(Level.Trial)
public void benchSetup(BenchmarkParams params) {
gson = new GsonBuilder()
.setDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
.create();
gsonCompatibilityMode = new GsonCompatibilityMode.Builder().setDateFormat("EEE MMM dd HH:mm:ss Z yyyy").build();
JsoniterSpi.setCurrentConfig(gsonCompatibilityMode);
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
if (params != null) {
if (params.getBenchmark().contains("jsoniterDynamicCodegenDecoder")) {
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
}
}
}
@Benchmark
public void gsonDecoder(Blackhole bh) throws IOException {
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
InputStreamReader reader = new InputStreamReader(stream);
try {
bh.consume(gson.fromJson(reader, new TypeReference<List<Tweet>>() {
}.getType()));
} finally {
reader.close();
stream.close();
}
}
@Benchmark
public void jsoniterReflectionDecoder(Blackhole bh) throws IOException {
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
try {
iter.reset(stream);
bh.consume(iter.read(new TypeReference<List<Tweet>>() {
}.getType()));
} finally {
JsonIteratorPool.returnJsonIterator(iter);
stream.close();
}
}
//
// @Benchmark
// public void jsoniterDynamicCodegenDecoder(Blackhole bh) throws IOException {
// bh.consume(JsonIterator.deserialize(gsonCompatibilityMode, json, BagOfPrimitives.class));
// }
public static void main(String[] args) throws Exception {
Main.main(new String[]{
"BenchGson",
"-i", "5",
"-wi", "5",
"-f", "1",
});
}
public static class Tweet {
@JsonProperty
String coordinates;
@JsonProperty
boolean favorited;
@JsonProperty
Date created_at;
@JsonProperty
boolean truncated;
@JsonProperty
Tweet retweeted_status;
@JsonProperty
String id_str;
@JsonProperty
String in_reply_to_id_str;
@JsonProperty
String contributors;
@JsonProperty
String text;
@JsonProperty
long id;
@JsonProperty
String retweet_count;
@JsonProperty
String in_reply_to_status_id_str;
@JsonProperty
Object geo;
@JsonProperty
boolean retweeted;
@JsonProperty
String in_reply_to_user_id;
@JsonProperty
String in_reply_to_screen_name;
@JsonProperty
Object place;
@JsonProperty
User user;
@JsonProperty
String source;
@JsonProperty
String in_reply_to_user_id_str;
}
static class User {
@JsonProperty
String name;
@JsonProperty
String profile_sidebar_border_color;
@JsonProperty
boolean profile_background_tile;
@JsonProperty
String profile_sidebar_fill_color;
@JsonProperty
Date created_at;
@JsonProperty
String location;
@JsonProperty
String profile_image_url;
@JsonProperty
boolean follow_request_sent;
@JsonProperty
String profile_link_color;
@JsonProperty
boolean is_translator;
@JsonProperty
String id_str;
@JsonProperty
int favourites_count;
@JsonProperty
boolean contributors_enabled;
@JsonProperty
String url;
@JsonProperty
boolean default_profile;
@JsonProperty
long utc_offset;
@JsonProperty
long id;
@JsonProperty
boolean profile_use_background_image;
@JsonProperty
int listed_count;
@JsonProperty
String lang;
@JsonProperty("protected")
@SerializedName("protected")
boolean isProtected;
@JsonProperty
int followers_count;
@JsonProperty
String profile_text_color;
@JsonProperty
String profile_background_color;
@JsonProperty
String time_zone;
@JsonProperty
String description;
@JsonProperty
boolean notifications;
@JsonProperty
boolean geo_enabled;
@JsonProperty
boolean verified;
@JsonProperty
String profile_background_image_url;
@JsonProperty
boolean defalut_profile_image;
@JsonProperty
int friends_count;
@JsonProperty
int statuses_count;
@JsonProperty
String screen_name;
@JsonProperty
boolean following;
@JsonProperty
boolean show_all_inline_media;
}
static class Feed {
@JsonProperty
String id;
@JsonProperty
String title;
@JsonProperty
String description;
@JsonProperty("alternate")
@SerializedName("alternate")
List<Link> alternates;
@JsonProperty
long updated;
@JsonProperty
List<Item> items;
@Override
public String toString() {
StringBuilder result = new StringBuilder()
.append(id)
.append("\n").append(title)
.append("\n").append(description)
.append("\n").append(alternates)
.append("\n").append(updated);
int i = 1;
for (Item item : items) {
result.append(i++).append(": ").append(item).append("\n\n");
}
return result.toString();
}
}
static class Link {
@JsonProperty
String href;
@Override
public String toString() {
return href;
}
}
static class Item {
@JsonProperty
List<String> categories;
@JsonProperty
String title;
@JsonProperty
long published;
@JsonProperty
long updated;
@JsonProperty("alternate")
@SerializedName("alternate")
List<Link> alternates;
@JsonProperty
Content content;
@JsonProperty
String author;
@JsonProperty
List<ReaderUser> likingUsers;
@Override
public String toString() {
return title
+ "\nauthor: " + author
+ "\npublished: " + published
+ "\nupdated: " + updated
+ "\n" + content
+ "\nliking users: " + likingUsers
+ "\nalternates: " + alternates
+ "\ncategories: " + categories;
}
}
static class Content {
@JsonProperty
String content;
@Override
public String toString() {
return content;
}
}
static class ReaderUser {
@JsonProperty
String userId;
@Override
public String toString() {
return userId;
}
}
}