-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
62 lines (47 loc) · 1.48 KB
/
test.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
from argparse import ArgumentParser
from pathlib import Path
import json
import subprocess
import sys
from welch import Welch
class Sleep:
def __init__(self, result_dir, flags=''):
self.flags = flags
self.results_dir = Path(result_dir)
def init(self):
if not self.results_dir.exists():
self.results_dir.mkdir(parents=True)
def bench(self, time):
file = Path(self.results_dir, 'tmp.json')
# *nix only
command = f'sleep {time}'
cmd =\
f'hyperfine'\
f' {self.flags}'\
f" '{command}'"\
f' --export-json {file}'
print(cmd)
if subprocess.call(cmd, shell=True) != 0:
raise RuntimeError(f'bench error: {cmd}')
return json.loads(file.read_text(encoding='UTF8'))['results'][0]
def execute(time_a, time_b, result_dir='my_results', flags='--warmup 1',
threshold=0.05):
sleep = Sleep(result_dir, flags)
sleep.init()
res_a, res_b = sleep.bench(time_a), sleep.bench(time_b)
welch = Welch(time_a, time_b, threshold)
welch.header()
welch.test('sleep', res_a, res_b)
print('')
welch.summary()
def main():
"""Sleep comparison test.
$ python3 test.py 1 2
"""
parser = ArgumentParser()
parser.add_argument('times', nargs=2, help='sleep times in seconds')
args = parser.parse_args()
times = args.times
execute(times[0], times[1])
if __name__ == '__main__':
sys.exit(main())