-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_perfs.py
194 lines (157 loc) · 5.74 KB
/
run_perfs.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import json
import subprocess
import matplotlib.pyplot as plt
import argparse
import numpy as np
from glob import glob
from tqdm import tqdm
parser = argparse.ArgumentParser(
description="Run and graph benchmarks between lulz, lci, and python."
)
subparsers = parser.add_subparsers(dest="command_type")
graph = subparsers.add_parser(
"graph",
help="only generate the graph based on previous benchmark data in ./_perfs/",
)
graph.add_argument(
"benchmark",
metavar="<benchmark>",
type=str,
nargs=1,
help="the benchmark in ./perfs/ to run",
)
run = subparsers.add_parser(
"run",
help="run the same benchmark (in ./perfs/) multiple times, with \
different input sizes, and composite into a line graph",
)
run.add_argument(
"benchmark",
metavar="<benchmark>",
type=str,
nargs=1,
help="the benchmark in ./perfs/ to run",
)
runall = subparsers.add_parser(
"runall",
help="run all benchmarks in ./perfs/, and composite into a bar graph",
)
opts = parser.parse_args()
exts = [".lol", ".py", ".lci.lol",]
commands = ["./target/release/lulz", "python", "lci"]
perfs_dir = "_perfs"
if not os.path.isdir(perfs_dir):
os.makedirs(perfs_dir)
def get_filenames_from_name(name, folder_name):
filenames = [os.path.join(folder_name, name + ext) for ext in exts]
temp_filenames = [filename + ".temp" for filename in filenames]
values = json.load(open(os.path.join(folder_name, "values.json"), "r"))
ns = values["<<N>>"]
return (filenames, temp_filenames, ns)
json_name = os.path.join(perfs_dir, "data.json")
def get_results(filenames, temp_filenames, n):
for (file, temp) in zip(filenames, temp_filenames):
# copy file to temp location, to overwrite the <<N>> values
with open(file, "r") as original:
old_string = original.read()
with open(temp, "w+") as temp_file:
temp_file.write(old_string.replace("<<N>>", str(n)))
process = subprocess.Popen(
[
"hyperfine",
"-s",
"full",
"--export-json",
json_name,
]
+ [
f'{command} "{filename}"'
for (command, filename) in zip(commands, temp_filenames)
],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
# write the piped stdout from hyperfine through tqdm
for line in process.stdout:
tqdm.write(line.decode("utf-8"), end="")
# store useful information
results = json.load(open(json_name, "r"))
return results
if opts.command_type == "runall":
bar_width = 0.25
fig, ax = plt.subplots()
data = {command: [] for command in commands}
benches = []
for value_filename in glob("./perfs/*/values.json"):
value_json = json.load(open(value_filename, "r"))
folder_name = os.path.dirname(value_filename)
name = os.path.basename(folder_name)
(filenames, temp_filenames, ns) = get_filenames_from_name(name, folder_name)
results = get_results(filenames, temp_filenames, value_json["one"])
benches.append(name)
for program in results["results"]:
command = program["command"].split(" ")[0]
data[command].append((name, program["mean"], program["stddev"]))
current_xs = np.arange(len(benches))
for (name, series) in data.items():
ax.bar(
current_xs,
[point[1] for point in series],
width=bar_width,
label=name.split("/")[-1],
)
current_xs = [x + bar_width for x in current_xs]
ax.set_xlabel("benchmark", fontweight="bold")
ax.set_ylabel("time (s)", fontweight="bold")
plt.xticks([r + bar_width for r in range(len(benches))], benches)
ax.legend()
fig.suptitle(f"benchmark results", fontsize=18)
ax.set_title("lower is better")
plt.savefig(os.path.join(perfs_dir, f"benches.png"), dpi=300, bbox_inches="tight", pad_inches=0.2)
else:
name = opts.benchmark[0]
folder_name = os.path.join("perfs", name)
if not os.path.isdir(folder_name):
print(f"invalid program to bench `{name}`")
exit(1)
perf_backup = os.path.join(perfs_dir, f"{name}_results.json")
(filenames, temp_filenames, ns) = get_filenames_from_name(name, folder_name)
fig, ax = plt.subplots()
def regen_single_graph(ns, name, data):
# plot the data and write to file
fig, ax = plt.subplots()
for (file, points) in data.items():
ax.errorbar(
ns,
[point[0] for point in points],
[point[1] for point in points],
label=file.split("/")[-1],
marker=".",
capsize=3,
)
ax.set_xlabel("input size")
ax.set_ylabel("time (s)")
ax.legend()
fig.suptitle(f"{name} benchmark results", fontsize=18)
ax.set_title("lower is better (log scale)")
ax.set_yscale("log")
plt.savefig(os.path.join(perfs_dir, f"{name}.png"), dpi=300)
if opts.command_type == "graph":
data = json.load(open(os.path.join(perf_backup), "r"))
regen_single_graph(ns, name, data)
elif opts.command_type == "run":
data = {command: [] for command in commands}
for n in tqdm(ns):
tqdm.write(f"=== Size {n} ===")
results = get_results(filenames, temp_filenames, n)
for program in results["results"]:
command = program["command"].split(" ")[0]
data[command].append((program["mean"], program["stddev"]))
# clean up generated files
for temp in temp_filenames:
os.remove(temp)
os.remove(json_name)
json.dump(data, open(perf_backup, "w"))
regen_single_graph(ns, name, data)