forked from lijoantony/JsonBenchmarkCpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
184 lines (150 loc) · 4.25 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
/*
* JsonBenchmarkCpp
* A small program to compare perfomance of different json libs available
*
* Currently supporting following libs,
*
* 1. Cajun
* 2. json_spirit
* 3. libjson
*
* 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>
/*
* @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)
{
std::istringstream buff(jsonString);
timespec time1, time2;
json::Object obj;
std::cout << std::setw(25) << "cajun";
//Parsing the string
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
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);
json::Writer::Write(obj, out);
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);
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);
json_spirit::write(value, out);
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
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
JSONNode 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);
out = n.write();
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);
return 0;
}