-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_rapidjson.cpp
More file actions
103 lines (87 loc) · 2.94 KB
/
bench_rapidjson.cpp
File metadata and controls
103 lines (87 loc) · 2.94 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
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "rapidjson/document.h"
static double now_sec() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
static int cmp_double(const void *a, const void *b) {
double da = *(const double *)a, db = *(const double *)b;
return (da > db) - (da < db);
}
static void bench_file(const char *path, int iters, int warmup) {
FILE *f = fopen(path, "rb");
if (!f) { printf("%-25s LOAD ERROR\n", path); return; }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
char *buf = (char *)malloc((size_t)sz);
fread(buf, 1, (size_t)sz, f);
fclose(f);
rapidjson::Document doc;
doc.Parse(buf, (size_t)sz);
if (doc.HasParseError()) {
printf("%-25s PARSE ERROR at %zu\n", path, doc.GetErrorOffset());
free(buf);
return;
}
for (int i = 0; i < warmup; i++)
doc.Parse(buf, (size_t)sz);
double *samples = (double *)malloc((size_t)iters * sizeof(double));
for (int i = 0; i < iters; i++) {
double t0 = now_sec();
doc.Parse(buf, (size_t)sz);
double t1 = now_sec();
samples[i] = t1 - t0;
}
qsort(samples, (size_t)iters, sizeof(double), cmp_double);
double best = samples[0];
double median = samples[iters / 2];
double worst = samples[iters - 1];
double sum = 0;
for (int i = 0; i < iters; i++) sum += samples[i];
double mean = sum / iters;
double var = 0;
for (int i = 0; i < iters; i++) {
double d = samples[i] - mean;
var += d * d;
}
double stddev = sqrt(var / iters);
double mb = (double)sz / (1024.0 * 1024.0);
double gb = (double)sz / (1024.0 * 1024.0 * 1024.0);
printf("%-25s %7.2f MB %8.3f %8.3f %8.3f %7.3f %8.3f\n",
path, mb,
best * 1000.0, median * 1000.0, worst * 1000.0,
stddev * 1000.0, gb / median);
free(samples);
free(buf);
}
int main(int argc, char **argv) {
int iters = 10;
int warmup = 3;
while (argc > 1 && argv[1][0] == '-') {
if (argv[1][1] == 'n')
iters = atoi(argv[1] + 2);
else if (argv[1][1] == 'w')
warmup = atoi(argv[1] + 2);
argc--; argv++;
}
printf("rapidjson benchmark (%d iterations, %d warmup)\n", iters, warmup);
printf("%-25s %9s %8s %8s %8s %7s %8s\n",
"file", "size", "min", "median", "max", "stddev", "GB/s");
printf("----------------------------"
"------------------------------------------------------\n");
if (argc > 1) {
for (int i = 1; i < argc; i++)
bench_file(argv[i], iters, warmup);
} else {
bench_file("data/canada.json", iters, warmup);
bench_file("data/twitter.json", iters, warmup);
bench_file("data/citm_catalog.json", iters, warmup);
bench_file("data/large.json", iters, warmup);
}
return 0;
}