Skip to content

Commit a9d411d

Browse files
committed
graph
1 parent 530cb23 commit a9d411d

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

graph

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
3+
4+
import collections
5+
import json
6+
import os
7+
import sys
8+
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
12+
LIBRARIES = ('orjson', 'ujson', 'rapidjson', 'json')
13+
14+
COLOR = ('blue', 'green', 'red', 'blue')
15+
16+
def aggregate():
17+
benchmarks_dir = os.path.join('.benchmarks', os.listdir('.benchmarks')[0])
18+
res = collections.defaultdict(dict)
19+
for filename in os.listdir(benchmarks_dir):
20+
with open(os.path.join(benchmarks_dir, filename), 'r') as fileh:
21+
data = json.loads(fileh.read())
22+
23+
for each in data['benchmarks']:
24+
res[each['group']][each['extra_info']['lib']] = {
25+
'data': [
26+
val * 1000 for val in each['stats']['data']
27+
],
28+
'ops': each['stats']['ops'],
29+
}
30+
return res
31+
32+
def box(obj):
33+
for group, val in sorted(obj.items()):
34+
data = []
35+
for lib in LIBRARIES:
36+
data.append(val[lib]['data'])
37+
fig = plt.figure(1, figsize=(9, 6))
38+
ax = fig.add_subplot(111)
39+
bp = ax.boxplot(data, vert=False, labels=LIBRARIES)
40+
ax.set_xlim(left=0)
41+
ax.set_xlabel('milliseconds')
42+
plt.title(group)
43+
plt.show()
44+
45+
def bar(obj):
46+
res = {key: [] for key in LIBRARIES}
47+
groups = set()
48+
for group, val in sorted(obj.items()):
49+
for lib in LIBRARIES:
50+
res[lib].append(np.median(val[lib]['data']))
51+
groups.add(group)
52+
53+
groups = list(reversed(sorted(list(groups))))
54+
55+
fig, ax = plt.subplots()
56+
index = np.arange(len(groups))
57+
bar_width = 0.2
58+
opacity = 0.8
59+
60+
plt.title('Deserialization')
61+
plt.ylabel('milliseconds')
62+
plt.xticks(index + bar_width, [each.split(' ')[0] for each in groups])
63+
plt.legend()
64+
65+
for (idx, lib) in enumerate(LIBRARIES):
66+
plt.bar(
67+
index + (idx * bar_width),
68+
res[lib],
69+
bar_width,
70+
alpha=opacity,
71+
color=COLOR[idx],
72+
label=lib,
73+
)
74+
plt.tight_layout()
75+
plt.show()
76+
77+
try:
78+
locals()[sys.argv[1]](aggregate())
79+
except KeyError:
80+
sys.stderr.write("usage: graph (box|bar)\n")
81+
sys.exit(1)

0 commit comments

Comments
 (0)