-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathperftest.py
162 lines (138 loc) · 4.1 KB
/
perftest.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
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
#!/usr/bin/env python
"""Generate performance diagnostics.
The purpose of this script is to generate performance diagnostics for
various jmespath expressions to be able to track the performance
over time. The test files are data driven similar to the
compliance tests.
"""
import argparse
import time
import os
import json
import sys
import timeit
_clock = timeit.default_timer
from jmespath.parser import Parser
from jmespath.lexer import Lexer
BENCHMARK_FILE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'tests',
'compliance',
'benchmarks.json')
APPROX_RUN_TIME = 0.5
def run_tests(tests):
times = []
for test in tests:
given = test['given']
expression = test['expression']
result = test['result']
should_search = test['bench_type'] == 'full'
lex_time = _lex_time(expression)
parse_time = _parse_time(expression)
if should_search:
search_time = _search_time(expression, given)
combined_time = _combined_time(expression, given, result)
else:
search_time = 0
combined_time = 0
sys.stdout.write(
"lex_time: %10.5fus, parse_time: %10.5fus, search_time: %10.5fus "
"combined_time: %10.5fus " % (1000000 * lex_time,
1000000 * parse_time,
1000000 * search_time,
1000000 * combined_time))
sys.stdout.write("name: %s\n" % test['name'])
def _lex_time(expression, clock=_clock):
lex = Lexer()
duration = 0
i = 0
while True:
i += 1
start = clock()
list(lex.tokenize(expression))
end = clock()
total = end - start
duration += total
if duration >= APPROX_RUN_TIME:
break
return duration / i
def _search_time(expression, given, clock=_clock):
p = Parser()
parsed = p.parse(expression)
duration = 0
i = 0
while True:
i += 1
start = clock()
parsed.search(given)
end = clock()
total = end - start
duration += total
if duration >= APPROX_RUN_TIME:
break
return duration / i
def _parse_time(expression, clock=_clock):
best = float('inf')
p = Parser()
duration = 0
i = 0
while True:
i += 1
p.purge()
start = clock()
p.parse(expression)
end = clock()
total = end - start
duration += total
if duration >= APPROX_RUN_TIME:
break
return duration / i
def _combined_time(expression, given, result, clock=_clock):
best = float('inf')
p = Parser()
duration = 0
i = 0
while True:
i += 1
p.purge()
start = clock()
r = p.parse(expression).search(given)
end = clock()
total = end - start
if r != result:
raise RuntimeError("Unexpected result, received: %s, "
"expected: %s" % (r, result))
duration += total
if duration >= APPROX_RUN_TIME:
break
return duration / i
def load_tests(filename):
loaded = []
with open(filename) as f:
data = json.load(f)
if isinstance(data, list):
for i, d in enumerate(data):
_add_cases(d, loaded, '%s-%s' % (filename, i))
else:
_add_cases(data, loaded, filename)
return loaded
def _add_cases(data, loaded, filename):
for case in data['cases']:
current = {
'given': data['given'],
'name': case.get('comment', case['expression']),
'expression': case['expression'],
'result': case.get('result'),
'bench_type': case['bench'],
}
loaded.append(current)
return loaded
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', default=BENCHMARK_FILE)
args = parser.parse_args()
collected_tests = []
collected_tests.extend(load_tests(args.filename))
run_tests(collected_tests)
if __name__ == '__main__':
main()