-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
113 lines (95 loc) · 2.77 KB
/
benchmark.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
#include "json.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
using namespace std::chrono;
class ElapsedTimer
{
public:
ElapsedTimer()
{
reset();
}
void reset()
{
t1 = high_resolution_clock::now();
}
double value(bool bReset = false)
{
duration<double> time_span = duration_cast<duration<double>>(high_resolution_clock::now() - t1);
double ret = time_span.count();
if (bReset) {
reset();
}
return ret;
}
private:
high_resolution_clock::time_point t1;
};
void iterate(json::document & jDoc, json::document & jDoc2)
{
for (auto & row : jDoc["rows"]) {
auto & doc = row["doc"];
if (doc.exists("crash_data")) {
json::value jTemp;
jTemp["siteID"] = doc["siteID"];
jTemp["version"] = doc["version"];
jTemp["eventGroup"] = doc["eventGroup"];
jTemp["eventType"] = doc["eventType"];
jTemp["eventName"] = doc["eventName"];
jTemp["eventTime"] = doc["eventTime"];
jTemp["crash_data"] = doc["crash_data"];
jDoc2.push_back(jTemp);
}
}
}
int main(int argc, char ** argv)
{
ElapsedTimer timer;
json::document jDoc;
json::document jDoc2;
std::cout << "json::document size: " << sizeof(json::document) << std::endl;
std::cout << "json::value size: " << sizeof(json::value) << std::endl;
std::cout << "json::object size: " << sizeof(json::object) << std::endl;
std::cout << "json::array size: " << sizeof(json::array) << std::endl;
std::cout << "json::iterator size: " << sizeof(json::iterator) << std::endl;
std::cout << "Parsing " << argv[1] << "... ";
std::cout.flush();
bool bParsedOk = jDoc.parseFile(argv[1]);
std::cout << "Took " << timer.value(true) << "s" << std::endl;
if (!bParsedOk) {
std::cout << "FAILED!" << std::endl;
std::cerr << "Could not parse " << argv[1] << "." << std::endl;
return -1;
}
std::cout << "Writing temp1.json... ";
std::cout.flush();
bool bWroteOk = jDoc.writeFile("temp1.json", true);
if (!bWroteOk) {
std::cout << "FAILED!" << std::endl;
std::cerr << "Could not write temp1.json." << std::endl;
return -1;
}
std::cout << "Took " << timer.value(true) << "s" << std::endl;
std::cout << "Iterating " << argv[1] << "... ";
std::cout.flush();
iterate(jDoc, jDoc2);
std::cout << "Took " << timer.value(true) << "s" << std::endl;
std::cout << "Writing temp2.json... ";
std::cout.flush();
bWroteOk = jDoc2.writeFile("temp2.json", true);
if (!bWroteOk) {
std::cout << "FAILED!" << std::endl;
std::cerr << "Could not write temp2.json." << std::endl;
return -1;
}
std::cout << "Took " << timer.value(true) << "s" << std::endl;
std::cout << "Writing temp3.json... ";
std::cout.flush();
std::ofstream out3("temp3.json");
out3 << std::noskipws;
out3 << jDoc;
out3.close();
std::cout << "Took " << timer.value(true) << "s" << std::endl;
return 0;
}