forked from biojppm/rapidyaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_bm.py
128 lines (94 loc) · 3.03 KB
/
parse_bm.py
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
import ryml
import ruamel.yaml
import yaml
import timeit
import time
import prettytable
from collections import OrderedDict as odict
class RunResults:
__slots__ = ('name', 'count', 'time', 'avg', 'MBps', 'timeit')
def __init__(self, name, time, count, MB, timeit):
self.name = name
self.time = time
self.count = count
self.avg = time / count
self.MBps = MB / self.time / 1000.0
self.timeit = timeit
def __str__(self):
fmt = "{}: count={} time={:.3f}ms avg={:.3f}ms MB/s={:.3f}"
fmt = fmt.format(self.name, self.count, self.time, self.avg, self.MBps)
return fmt
class BmCase:
def __init__(self, filename):
with open(filename, "r") as f:
src = f.read()
self.src_as_str = src
self.src_as_bytes = bytes(src, "utf8")
self.src_as_bytearray = bytearray(src, "utf8")
def run(self, bm_name, cls):
obj = cls()
method = getattr(obj, bm_name)
self.count = 0
self.MB = 0
def fn():
method(self)
self.count += 1
self.MB += len(self.src_as_str)
t = timeit.Timer(fn)
delta = time.time()
result = t.autorange()
delta = 1000. * (time.time() - delta)
name = bm_name + ":" + cls.__name__
return RunResults(name, delta, self.count, self.MB, result)
class RymlRo:
def parse(self, case):
r = ryml.parse(case.src_as_bytearray)
class RymlRoReuse:
def __init__(self):
self.tree = ryml.Tree()
def parse(self, case):
ryml.parse(case.src_as_bytearray, tree=ryml.Tree())
class RymlInSitu:
def parse(self, case):
r = ryml.parse_in_situ(case.src_as_bytearray)
class RymlInSituReuse:
def __init__(self):
self.tree = ryml.Tree()
def parse(self, case):
self.tree.clear()
self.tree.clear_arena()
ryml.parse_in_situ(case.src_as_bytearray, tree=self.tree)
class RuamelYaml:
def parse(self, case):
r = ruamel.yaml.load(case.src_as_str, Loader=ruamel.yaml.Loader)
class PyYaml:
def parse(self, case):
r = yaml.safe_load(case.src_as_str)
def run(filename):
case = BmCase(filename)
approaches = (RuamelYaml,
PyYaml,
RymlRo,
RymlRoReuse,
RymlInSitu,
RymlInSituReuse)
benchmarks = ('parse', )
for bm in benchmarks:
results = odict()
for cls in approaches:
r = case.run(bm, cls)
results[r.name] = r
print(r)
table = prettytable.PrettyTable()
table.field_names = ["case", "count", "time(ms)", "avg(ms)", "avg_read(MB/s)"]
table.align["case"] = "l"
def f(v): return "{:.3f}".format(v)
for v in results.values():
table.add_row([v.name, v.count, f(v.time), f(v.avg), f(v.MBps)])
print(table)
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
raise Exception("")
filename = sys.argv[1]
run(filename)