Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·262 lines (203 loc) · 6.22 KB
/
Copy pathbenchmark.py
File metadata and controls
executable file
·262 lines (203 loc) · 6.22 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
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
#!/usr/bin/env python3
import gc
import json
import sys
import time
import tracemalloc
import jsonfold
REPEATS = 3
MEM_FRACTION = 0.10
class NullWriter:
def __init__(self, t0=None):
self.t0 = t0 if t0 else time.perf_counter ;
self.first_write = None
self.bytes = 0
self.writes = 0
def write(self, s):
if self.first_write is None:
self.first_write = time.perf_counter()
self.bytes += len(s)
self.writes += 1
return len(s)
def ttfb_ms(self):
if self.first_write is None:
return ""
return round((self.first_write - self.t0) * 1000, 1)
def make_data(rows):
return {
"meta": {"version": 1, "ok": True, "name": "jsonfold benchmark"},
"long_ids": list(range(100)),
"long_obj": {f"k{i}": i for i in range(50)},
"rows": [
{
"id": i,
"name": f"name_{i}",
"active": i % 3 == 0,
"score": i * 1.25,
"tags": ["alpha", "beta", "gamma", "delta"],
"pos": {"x": i, "y": i + 1, "z": i + 2},
"values": [i, i + 1, i + 2, i + 3, i + 4],
"pairs": [ [ i, i+1, [i+2, i+3], [i+4, i+5]]],
}
for i in range(rows)
],
}
def mem_label():
return "kb"
def mem_units(n):
return round(n / 1024, 1)
def run_case(data, name):
if name == "base.dumps.plain":
return lambda t0: write_string(t0, json.dumps(data))
if name == "base.dumps.pretty":
return lambda t0: write_string(t0, json.dumps(data, indent=2))
if name == "base.dump.pretty":
return lambda t0: run_json_dump(data, t0)
if name == "base.dump.plain":
return lambda t0: run_json_dump_plain(data, t0)
kind, func, compact = name.split(".")
if kind == "jf":
if func == "dumps":
return lambda t0: write_string(t0, jsonfold.dumps(data, compact=compact, indent=2))
if func == "dump":
return lambda t0: run_jsonfold_dump(data, t0, compact)
raise ValueError(name)
def write_string(t0, s):
w = NullWriter(t0)
w.write(s)
return w
def run_json_dump(data, t0):
w = NullWriter(t0)
json.dump(data, w, indent=2)
return w
def run_jsonfold_dump(data, t0, compact):
w = NullWriter(t0)
jsonfold.dump(data, w, compact=compact, indent=2)
return w
def run_json_dump_plain(data, t0):
w = NullWriter(t0)
json.dump(data, w)
return w
def time_one(name, data):
best = None
best_dt = 0
for _ in range(REPEATS):
gc.collect()
t0 = time.perf_counter()
p0 = time.process_time()
w = run_case(data, name)(t0)
p1 = time.process_time()
t1 = time.perf_counter()
dt = t1-t0
row = {
"time(ms)": round(dt * 1000, 1),
"CPU(ms)": round((p1 - p0) * 1000, 1),
"ttfb(ms)": w.ttfb_ms(),
f"out({mem_label()})": mem_units(w.bytes),
"writes": w.writes,
}
if best is None or dt < best_dt:
best = row
best_dt = dt
return best_dt, best
def memory_one(name, data):
gc.collect()
tracemalloc.start()
t0 = time.perf_counter()
run_case(data, name)(t0)
_, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
return mem_units(peak)
def print_table(rows):
cols = list(rows[0].keys())
widths = {c: max(len(c), *(len(str(r[c])) for r in rows)) for c in cols}
def is_num(v):
return isinstance(v, (int, float)) and not isinstance(v, bool)
numeric = {
c: all(is_num(r[c]) or r[c] == "" for r in rows)
for c in cols
}
def cell(c, v):
s = str(v)
return " " + (s.rjust(widths[c]) if numeric[c] else s.ljust(widths[c])) + " "
line = "+" + "+".join("-" * (widths[c] + 2) for c in cols) + "+"
print(line)
print("|" + "|".join(cell(c, c) for c in cols) + "|")
print(line)
for r in rows:
print("|" + "|".join(cell(c, r[c]) for c in cols) + "|")
print(line)
def run_one_size(rows, tests):
data = make_data(rows)
if not tests:
tests = [
"base.dump.plain",
"base.dump.pretty",
"jf.dump.off",
"jf.dump.none",
"jf.dump.default",
"jf.dump.low",
"jf.dump.med",
"jf.dump.classic",
"jf.dump.high",
"jf.dump.max",
"jf.dump.pack",
"jf.dump.fold",
"jf.dump.grid",
"jf.dump.join",
"base.dumps.plain",
"base.dumps.pretty",
"jf.dumps.none",
"jf.dumps.default",
"jf.dumps.high",
"jf.dumps.max",
]
results = []
for name in tests:
print(f"{name} ({rows})... ", end="", file=sys.stderr, flush=True)
t0 = time.perf_counter() ;
best_dt, speed = time_one(name, data)
peak_mb = memory_one(name, data)
t1 = time.perf_counter() ;
dt = t1-t0 ;
print(f"{round(dt*1000,0)} ms", file=sys.stderr)
results.append({
"rows": rows,
"name": name,
**speed,
f"peak({mem_label()})": peak_mb,
})
return results
def show_data(rows):
data = make_data(rows)
json.dump(make_data(rows), fp=sys.stdout, indent=2)
def main(argv):
import argparse
p = argparse.ArgumentParser(
description="Read JSON from stdin; write folded JSON to stdout.")
p.add_argument("--show", type=int, default=None)
args, rest = p.parse_known_args(argv)
t0 = time.perf_counter() ;
filter = []
last_sz = None
results = []
if (args.show):
show_data(args.show)
return
for arg in rest[1:]:
if arg == "-":
filter = []
continue
try:
last_sz = int(arg)
except ValueError:
filter.append(arg)
continue
results.extend(run_one_size(last_sz, filter))
if last_sz is None:
results.extend(run_one_size(1_000, filter))
t1 = time.perf_counter() ;
print_table(results)
print(f"completed in: {round(t1-t0, 1)}", file=sys.stderr)
if __name__ == "__main__":
main(sys.argv)