-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtablize.py
More file actions
216 lines (172 loc) · 6.68 KB
/
Copy pathtablize.py
File metadata and controls
216 lines (172 loc) · 6.68 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
# -*- coding: utf-8 -*-
# :Project: python-rapidjson -- reST tables writer tool
# :Author: Lele Gaifax <lele@metapensiero.it>
# :License: MIT License
# :Copyright: © 2017, 2018, 2020, 2021, 2022, 2024 Lele Gaifax
#
# Assume we did::
#
# pytest --benchmark-json=comparison.json --compare-other-engines benchmarks/
#
# Read back that JSON outcome and produce three reST tables summarizing the
# comparison.
#
from collections import defaultdict, namedtuple
import sys
import rapidjson
Benchmark = namedtuple('Benchmark', 'group, name, contender')
class Comparison:
COMPARE_FIELD = 'min' # one of min, max, mean, median...
def __init__(self, contenders, benchmarks):
self.contenders = contenders
self.timings = {}
self.load_data(benchmarks)
def load_data(self, benchmarks):
for bm in benchmarks:
group = bm['group']
desc = bm['name'].split('[', 1)[1][:-1]
contender, name = desc.split('-')
if contender in self.contenders:
bmark = Benchmark(group, name, contender)
self.timings[bmark] = bm['stats'][self.COMPARE_FIELD]
def normalize_and_summarize(self, group):
table = defaultdict(dict)
for bmark, timing in self.timings.items():
if bmark.group == group:
table[bmark.name][bmark.contender] = timing
normalized = {}
sums = defaultdict(float)
by = self.contenders[0]
for name, values in table.items():
for c in self.contenders:
sums[c] += values[c]
normalized[name] = [values[c] / values[by] for c in self.contenders]
normalized_sum = [sums[c] / sums[by] for c in self.contenders]
return normalized, normalized_sum
def tabulate(self, group, headers):
normalized, sum = self.normalize_and_summarize(group)
nc = len(self.contenders)
widths = [max((len(n)+4 for n in normalized))] + \
[max((len(h) for h in headers))] * nc
cells = ['-' * (width+2) for width in widths]
seprow = '+' + '+'.join(cells) + '+'
cells = ['=' * (width+2) for width in widths]
headseprow = '+' + '+'.join(cells) + '+'
cells = [f' {{:^{width}}} ' for width in widths]
rowfmt = '|' + '|'.join(cells) + '|'
print(seprow)
print(rowfmt.format(*([group] + [headers[i]
for i, c in enumerate(self.contenders)])))
print(headseprow)
def printrow(name, row):
best = min(row)
fvalues = [('**%.2f**' if v==best else '%.2f') % v for v in row]
print(rowfmt.format(*([name] + fvalues)))
print(seprow)
for n in sorted(normalized):
printrow(n, normalized[n])
printrow('overall', sum)
def dumps_and_loads(benchmarks):
contenders = ('rapidjson_f',
'rapidjson_c',
'rapidjson_nn_f',
'rapidjson_nn_c',
'rapidjson_p',
'simdjson',
'orjson',
'ujson',
'simplejson',
'stdlib json')
s_headers = (r'``dumps()``\ [1]_',
r'``Encoder()``\ [2]_',
r'``dumps(n)``\ [3]_',
r'``Encoder(n)``\ [4]_',
r'``PyEncoder()``\ [5]_',
r'simdjson\ [6]_',
r'orjson\ [7]_',
r'ujson\ [8]_',
r'simplejson\ [9]_',
r'stdlib\ [10]_')
d_headers = (r'``loads()``\ [11]_',
r'``Decoder()``\ [12]_',
r'``loads(n)``\ [13]_',
r'``Decoder(n)``\ [14]_',
r'``PyDecoder()``\ [15]_',
r'simdjson',
r'orjson',
r'ujson',
r'simplejson',
r'stdlib')
assert len(contenders) == len(s_headers) == len(d_headers)
from csimdjson import VERSION as simdjv
from orjson import __version__ as orjv
from simplejson import __version__ as sjv
from ujson import __version__ as ujv
pyv = '%d.%d.%d' % sys.version_info[:3]
footnotes = [
'.. [1] ``rapidjson.dumps()``',
'.. [2] ``rapidjson.Encoder()``',
'.. [3] ``rapidjson.dumps(number_mode=NM_NATIVE)``',
'.. [4] ``rapidjson.Encoder(number_mode=NM_NATIVE)``',
'.. [5] ``rapidjson.Encoder()`` subclassed in Python with *no-op* hook methods',
f'.. [6] `simdjson {simdjv} <https://pypi.org/project/pysimdjson/>`__',
f'.. [7] `orjson {orjv} <https://pypi.org/project/orjson/{orjv}/>`__',
f'.. [8] `ujson {ujv} <https://pypi.org/project/ujson/{ujv}/>`__',
f'.. [9] `simplejson {sjv} <https://pypi.org/pypi/simplejson/{sjv}>`__',
f'.. [10] Python {pyv} standard library ``json``',
'.. [11] ``rapidjson.loads()``',
'.. [12] ``rapidjson.Decoder()``',
'.. [13] ``rapidjson.loads(number_mode=NM_NATIVE)``',
'.. [14] ``rapidjson.Decoder(number_mode=NM_NATIVE)``',
'.. [15] ``rapidjson.Decoder() subclassed in Python with *no-op* hook methods``']
comparison = Comparison(contenders, benchmarks)
print()
print('Serialization')
print('~~~~~~~~~~~~~')
print()
comparison.tabulate('serialize', s_headers)
print()
print('Deserialization')
print('~~~~~~~~~~~~~~~')
print()
comparison.tabulate('deserialize', d_headers)
return footnotes
def ascii_vs_utf8(benchmarks):
engines = (('rapidjson', 'rj'),
('ujson', 'uj'),
('simplejson', 'sj'),
('stdlib json', 'json'))
contenders = [f'{e[0]} {k}' for e in engines for k in ('ascii', 'utf8')]
i = 15
s_headers = []
footnotes = []
for e in engines:
i += 1
s_headers.append(rf'``{e[1]} ascii``\ [{i}]_')
footnotes.append(f'.. [{i}] ``{e[0]}.dumps(ensure_ascii=True)``')
i += 1
s_headers.append(rf'``{e[1]} utf8``\ [{i}]_')
footnotes.append(f'.. [{i}] ``{e[0]}.dumps(ensure_ascii=False)``')
comparison = Comparison(contenders, benchmarks)
print()
print('ASCII vs UTF-8 Serialization')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print()
comparison.tabulate('serialize', s_headers)
return footnotes
def main():
import sys
if len(sys.argv) > 1:
fname = sys.argv[1]
else:
fname = 'comparison.json'
with open(fname) as f:
benchmarks = rapidjson.loads(f.read())['benchmarks']
footnotes = []
footnotes.extend(dumps_and_loads(benchmarks))
footnotes.extend(ascii_vs_utf8(benchmarks))
print()
for fn in footnotes:
print(fn)
if __name__ == '__main__':
main()