-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cpp
317 lines (263 loc) · 7.57 KB
/
main.cpp
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* JsonBenchmarkCpp
* A small program to compare perfomance of different json libs available
*
* Currently supporting following libs,
*
* 1. Cajun
* 2. json_spirit
* 3. libjson
* 4. json-parser
*
* Copyright Lijo Antony 2011
* Distributed under Apache License, Version 2.0
* see accompanying file LICENSE.txt
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <time.h>
//Cajun headers
#include <json/reader.h>
#include <json/writer.h>
#include <json/elements.h>
//json_spirit headers
#include <json_spirit.h>
//libjson headers
#include <libjson.h>
//json-parser headers
#define json_string __json_string
#include <json.h>
#undef json_string
#include <utilities_js.hpp>
//rapidjson headers
#if defined(__SSE4_2__)
# define RAPIDJSON_SSE42
#elif defined(__SSE2__)
# define RAPIDJSON_SSE2
#endif
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#define ITERATION 10000
/*
* @brief A function to print time duration
*
* @param start starttime
* @param end endtime
* @return none
*/
void printTimeDiff(timespec start, timespec end)
{
timespec temp;
if (end.tv_nsec > start.tv_nsec)
{
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
else
{
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
}
unsigned long long int usecs = (unsigned long long int)(temp.tv_sec) * 1000000 + (unsigned long long int)(temp.tv_nsec / 1000);
std::cout << std::setw(25) << std::left << usecs;
}
/*
* @brief function for cajun benchmark
*
* @param jsonString test data as a string
* @return none
*/
void cajunBenchmark(std::string jsonString)
{
timespec time1, time2;
json::Object obj;
std::cout << std::setw(25) << "cajun";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
std::istringstream buff(jsonString);
obj.Clear();
json::Reader::Read(obj, buff);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
//Serialize to string
std::ostringstream out;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
json::Writer::Write(obj, out);
out.str("");
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
/*
* @brief function for json_spirit benchmark
*
* @param jsonString test data as a string
* @return none
*/
void jsonspiritBenchmark(std::string jsonString)
{
std::istringstream buff(jsonString);
timespec time1, time2;
json_spirit::mValue value;
std::cout << std::setw(25) << "json_spirit";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++)
json_spirit::read( buff, value );
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
//Serialize to string
std::ostringstream out;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
json_spirit::write(value, out);
out.str("");
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
/*
* @brief function for libjson benchmark
*
* @param jsonString test data as a string
* @return none
*/
void libjsonBenchmark(std::string jsonString)
{
timespec time1, time2;
std::cout << std::setw(25) << "libjson";
//Parsing the string
JSONNode n;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++)
n = libjson::parse(jsonString);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
//Serialize to string
std::string out;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++)
out = n.write();
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
/*
* @brief function for json-parser benchmark
*
* @param jsonString test data as a string
* @return none
*/
void jsonparserBenchmark(std::string jsonString)
{
timespec time1, time2;
json_value * value;
std::cout << std::setw(25) << "json_parser";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
value = json_parse(jsonString.c_str());
json_value_free(value);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
/*
* @brief function for json_spirit benchmark
*
* @param jsonString test data as a string
* @return none
*/
void jsonAveryBenchmark(std::string jsonString)
{
std::istringstream buff(jsonString);
timespec time1, time2;
json_spirit::mValue value;
std::cout << std::setw(25) << "Avery";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
Utilities::JS::Node root;
for (int i = 0; i < ITERATION; i++)
Utilities::JS::Node::parse(jsonString.data(), jsonString.data() + jsonString.size(),root);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
//Serialize to string
std::ostringstream out;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
out << root;
out.str("");
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
/*
* @brief function for RapidJSON benchmark
*
* @param jsonString test data as a string
* @return none
*/
void rapidjsonBenchmark(std::string jsonString)
{
timespec time1, time2;
using namespace rapidjson;
Document d;
std::cout << std::setw(25) << "RapidJSON";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++)
d.Parse(jsonString.data());
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
//Serialize to string
StringBuffer sb;
Writer<StringBuffer> writer(sb);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i = 0; i < ITERATION; i++) {
sb.Clear();
d.Accept(writer);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printTimeDiff(time1, time2);
std::cout << std::endl;
}
int main()
{
std::ifstream ifs("data.json", std::ifstream::in);
std::string buff = "";
if(ifs.is_open())
{
while(!ifs.eof())
{
std::string temp;
ifs >> temp;
buff += temp;
}
}
if(buff.empty())
{
std::cout << "No data available for test, exiting!" << std::endl;
exit(1);
}
std::cout << std::setw(25) << std::left << "#library"
<< std::setw(25) << std::left << "parsing"
<< std::setw(25) << std::left << "writing"
<< std::endl;
cajunBenchmark(buff);
jsonspiritBenchmark(buff);
libjsonBenchmark(buff);
jsonAveryBenchmark(buff);
jsonparserBenchmark(buff);
rapidjsonBenchmark(buff);
return 0;
}