-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.py
executable file
·90 lines (71 loc) · 2.91 KB
/
perf.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
#!/usr/bin/env python3
import time
import subprocess
import argparse
from matplotlib import pyplot as plt
def timeexe(path, threads, size):
start = time.time()
# time.sleep(0.1)
subprocess.run([f"{path}", str(threads), str(size)])
end = time.time()
return float(end - start)
def make_plot(table):
fig = plt.figure()
lines = []
for t in table:
line, = plt.plot(t["problem size"], t["time"], label=t["name"])
lines.append(line)
plt.xlabel('Problem size', fontsize=18)
plt.ylabel('Time[s]', fontsize=16)
fig.legend(handles=lines)
fig.show()
fig.savefig("stats.png")
def run_tests(executable_path, threads, psizes, runs):
table = []
print("Running tests: ")
for t in threads:
times = []
for s in psizes:
print(f"threads: {t}, problem size: {s}")
avg = 0.0
for r in range(runs):
dt = timeexe(executable_path, t, s)
print(f"run {r}: {dt}ms")
avg += dt
if runs > 1:
avg /= runs
print(f"average time: {avg}s")
print("")
times.append(avg)
print("")
print("")
table.append({"name": f"threads_{t}", "problem size": psizes, "time": times})
return table
def main():
parser = argparse.ArgumentParser(prog='python perf.py', description='Autorun program with parameters and create '
'performance graph and table. please provide -t '
'and one of -s or -r')
parser.add_argument('executable_path', type=str, help='The path to the executable you want to time, '
'it must accept 2 parameters: thread_num and '
'problem_size')
parser.add_argument('-t', '--threads', nargs='+', type=int, help='A list of thread numbers')
parser.add_argument('-s', '--sizes', nargs='+', type=int, help='A list of problem sizes')
parser.add_argument('-sr', '--range', nargs='+', type=int,
help='3 numbers signifying start step end of the problem sizes')
parser.add_argument('-r', '--runs', nargs='?', type=int, default=1, help='How many times to sample tests')
args = parser.parse_args()
print(args)
if args.sizes is not None:
psizes = args.sizes
elif args.range is not None:
psizes = range(args.range[0], args.range[1] + 1, args.range[2])
else:
print("Please provise a range or a list of problem sizes")
if args.threads is not None:
threads = args.threads
else:
print("Please provise a range or a list of problem sizes")
table = run_tests(args.executable_path, threads, psizes, args.runs)
make_plot(table)
if __name__ == "__main__":
main()