-
Notifications
You must be signed in to change notification settings - Fork 4
/
json_benchmark.cpp
175 lines (144 loc) · 5.03 KB
/
json_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
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
//------------------------------------------------------------------------------
// This file is a full working example of using Matcheroni to build a JSON
// parser.
// Example usage:
// bin/json_parser test_file.json
// SPDX-FileCopyrightText: 2023 Austin Appleby <[email protected]>
// SPDX-License-Identifier: MIT License
#include "json.hpp"
#include "matcheroni/Utilities.hpp"
#include <stdio.h>
#include <algorithm>
using namespace matcheroni;
#if 0
// Debug config
constexpr bool verbose = true;
constexpr bool dump_tree = true;
const int reps = 1;
#else
// Benchmark config
constexpr bool verbose = false;
constexpr bool dump_tree = false;
const int reps = 100;
#endif
#define MATCH
#define PARSE
//------------------------------------------------------------------------------
int main(int argc, char** argv) {
printf("Matcheroni JSON matching/parsing benchmark\n");
const char* paths[] = {
"data/canada.json",
"data/citm_catalog.json",
"data/twitter.json",
"data/rapidjson_sample.json",
};
double all_byte_accum = 0;
double all_line_accum = 0;
double all_match_time = 0;
double all_parse_time = 0;
JsonMatchContext ctx1;
JsonParseContext ctx2;
for (auto path : paths) {
double byte_accum = 0;
double line_accum = 0;
double match_time = 0;
double parse_time = 0;
printf("----------------------------------------\n");
printf("Parsing %s\n", path);
std::string buf;
utils::read(path, buf);
if (buf.size() == 0) {
printf("Could not load %s\n", path);
continue;
}
byte_accum += buf.size();
for (size_t i = 0; i < buf.size(); i++) if (buf[i] == '\n') line_accum++;
TextSpan text = utils::to_span(buf);
//----------------------------------------
TextSpan match_end = text;
double best_match = 1.0e100;
std::vector<double> match_times;
match_times.reserve(reps);
for (int rep = 0; rep < reps; rep++) {
double time = -utils::timestamp_ms();
#ifdef MATCH
match_end = match_json(ctx1, text);
#endif
time += utils::timestamp_ms();
match_times.push_back(time);
}
std::sort(match_times.begin(), match_times.end());
match_time += match_times[reps/2];
#ifdef MATCH
if (match_end.begin < text.end) {
printf("Match failed!\n");
printf("Failure near `");
printf("%50.50s", match_end.begin);
printf("`\n");
exit(-1);
}
#endif
//----------------------------------------
TextSpan parse_end = text;
std::vector<double> parse_times;
parse_times.reserve(reps);
for (int rep = 0; rep < reps; rep++) {
double time = -utils::timestamp_ms();
ctx2.reset();
#ifdef PARSE
parse_end = parse_json(ctx2, text);
#endif
time += utils::timestamp_ms();
parse_times.push_back(time);
}
std::sort(parse_times.begin(), parse_times.end());
parse_time += parse_times[reps/2];
#ifdef PARSE
if (parse_end.begin < text.end) {
printf("Parse failed!\n");
printf("Failure near `");
printf("%50.50s\n", ctx2._highwater);
printf("`\n");
exit(-1);
}
#endif
//----------------------------------------
if (dump_tree) {
printf("Parse tree:\n");
utils::print_summary(ctx2, text, parse_end, 40);
}
if (verbose) {
//printf("Slab current %d\n", LifoAlloc::inst().current_size);
//printf("Slab max %d\n", LifoAlloc::inst().max_size);
//printf("Sizeof(node) == %ld\n", sizeof(JsonNode));
}
printf("\n");
printf("Tree nodes %ld\n", ctx2.node_count());
printf("Byte total %f\n", byte_accum);
printf("Line total %f\n", line_accum);
printf("Match time %f\n", match_time);
printf("Parse time %f\n", parse_time);
printf("Match byte rate %f megabytes per second\n", (byte_accum / 1e6) / (match_time / 1e3));
printf("Match line rate %f megalines per second\n", (line_accum / 1e6) / (match_time / 1e3));
printf("Parse byte rate %f megabytes per second\n", (byte_accum / 1e6) / (parse_time / 1e3));
printf("Parse line rate %f megalines per second\n", (line_accum / 1e6) / (parse_time / 1e3));
all_byte_accum += byte_accum;
all_line_accum += line_accum;
all_match_time += match_time;
all_parse_time += parse_time;
}
printf("----------------------------------------\n");
printf("Results averaged over all test files:\n");
printf("\n");
printf("Byte total %f\n", all_byte_accum);
printf("Line total %f\n", all_line_accum);
printf("Match time %f\n", all_match_time);
printf("Parse time %f\n", all_parse_time);
printf("Match byte rate %f megabytes per second\n", (all_byte_accum / 1e6) / (all_match_time / 1e3));
printf("Match line rate %f megalines per second\n", (all_line_accum / 1e6) / (all_match_time / 1e3));
printf("Parse byte rate %f megabytes per second\n", (all_byte_accum / 1e6) / (all_parse_time / 1e3));
printf("Parse line rate %f megalines per second\n", (all_line_accum / 1e6) / (all_parse_time / 1e3));
printf("\n");
return 0;
}
//------------------------------------------------------------------------------