Skip to content

Commit 849ba35

Browse files
committed
Change perf test to be time based
This is similar to how go's benchmarks run. The benchmark is run until a particular duration goal is hit rather than number of iterations. That way you can know how long test runs are going to take based on the number of tests running. Right now, each phase of the benchmark (lex,parse,search,combined) runs for 0.5 seconds.
1 parent 14cdfb8 commit 849ba35

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

perf/perftest.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,17 @@
1111
import os
1212
import json
1313
import sys
14+
import timeit
1415

15-
try:
16-
_clock = time.process_time
17-
except AttributeError:
18-
# Python 2.x does not have time.process_time
19-
_clock = time.clock
16+
_clock = timeit.default_timer
2017

2118

2219
from jmespath.parser import Parser
2320
from jmespath.lexer import Lexer
2421

2522

2623
DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cases')
27-
DEFAULT_NUM_LOOP = 100
24+
APPROX_RUN_TIME = 0.5
2825

2926

3027
def run_tests(tests):
@@ -47,50 +44,63 @@ def run_tests(tests):
4744

4845

4946
def _lex_time(expression, clock=_clock):
50-
best = float('inf')
5147
lex = Lexer()
52-
for i in range(DEFAULT_NUM_LOOP):
48+
duration = 0
49+
i = 0
50+
while True:
51+
i += 1
5352
start = clock()
5453
list(lex.tokenize(expression))
5554
end = clock()
5655
total = end - start
57-
if total < best:
58-
best = total
59-
return best
56+
duration += total
57+
if duration >= APPROX_RUN_TIME:
58+
break
59+
return duration / i
6060

6161

6262
def _search_time(expression, given, clock=_clock):
6363
p = Parser()
6464
parsed = p.parse(expression)
65-
best = float('inf')
66-
for i in range(DEFAULT_NUM_LOOP):
65+
duration = 0
66+
i = 0
67+
while True:
68+
i += 1
6769
start = clock()
6870
parsed.search(given)
6971
end = clock()
7072
total = end - start
71-
if total < best:
72-
best = total
73-
return best
73+
duration += total
74+
if duration >= APPROX_RUN_TIME:
75+
break
76+
return duration / i
7477

7578

7679
def _parse_time(expression, clock=_clock):
7780
best = float('inf')
7881
p = Parser()
79-
for i in range(DEFAULT_NUM_LOOP):
82+
duration = 0
83+
i = 0
84+
while True:
85+
i += 1
8086
p.purge()
8187
start = clock()
8288
p.parse(expression)
8389
end = clock()
8490
total = end - start
85-
if total < best:
86-
best = total
87-
return best
91+
duration += total
92+
if duration >= APPROX_RUN_TIME:
93+
break
94+
return duration / i
8895

8996

9097
def _combined_time(expression, given, result, clock=_clock):
9198
best = float('inf')
9299
p = Parser()
93-
for i in range(DEFAULT_NUM_LOOP):
100+
duration = 0
101+
i = 0
102+
while True:
103+
i += 1
94104
p.purge()
95105
start = clock()
96106
r = p.parse(expression).search(given)
@@ -99,9 +109,10 @@ def _combined_time(expression, given, result, clock=_clock):
99109
if r != result:
100110
raise RuntimeError("Unexpected result, received: %s, "
101111
"expected: %s" % (r, result))
102-
if total < best:
103-
best = total
104-
return best
112+
duration += total
113+
if duration >= APPROX_RUN_TIME:
114+
break
115+
return duration / i
105116

106117

107118
def load_tests(filename):

0 commit comments

Comments
 (0)