forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_benchmark.py
executable file
·167 lines (140 loc) · 5.46 KB
/
create_benchmark.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
#!/usr/bin/env python3
import argparse
import os
import re
def main():
p = argparse.ArgumentParser()
p.add_argument("name", help="The name of the new benchmark to be created")
args = p.parse_args()
# adds benchmark to `CMakeLists.txt`
update_cmakelists(args.name)
# create benchmark Swift file
create_benchmark_file(args.name)
# imports the benchmark module in `main.swift`
add_import_benchmark(args.name)
# registers the benchmark with the driver in `main.swift`
add_register_benchmark(args.name)
def update_cmakelists(name):
"""Adds a new entry to the `CMakeLists.txt` file with the given
benchmark name.
"""
relative_path = create_relative_path("../CMakeLists.txt")
file_contents = []
with open(relative_path, "r") as f:
file_contents = f.readlines()
file_new_contents = insert_line_alphabetically(
name,
" single-source/" + name + "\n",
file_contents,
r" single-source\/([a-zA-Z]+)",
)
with open(relative_path, "w") as f:
for line in file_new_contents:
f.write(line)
def create_benchmark_file(name):
"""Creates a new Swift file with the given name based on the template
and places it in the `single-source` directory.
"""
template_path = create_relative_path("Template.swift")
benchmark_template = ""
with open(template_path, "r") as f:
benchmark_template = "".join(f.readlines())
# fill in template with benchmark name.
formatted_template = benchmark_template.format(name=name)
relative_path = create_relative_path("../single-source/")
source_file_path = os.path.join(relative_path, name + ".swift")
with open(source_file_path, "w") as f:
f.write(formatted_template)
def add_import_benchmark(name):
"""Adds an `import` statement to the `main.swift` file for the new
benchmark.
"""
relative_path = create_relative_path("../utils/main.swift")
# read current contents into an array
file_contents = []
with open(relative_path, "r") as f:
file_contents = f.readlines()
# the test dependencies are placed before all benchmarks, so we have to
# insert the benchmark in the right alphabetical order after we have seen
# all test dependencies.
read_test_dependencies = False
previous_benchmark_name = None
file_new_contents = []
for line in file_contents:
# check if this line is a definition of a benchmark and get its name
match = re.search(r"import ([a-zA-Z]+)", line)
if match and match.group(1):
benchmark_name = match.group(1)
# find where to insert the new benchmark in the right alphabetical
# order.
if (
name < benchmark_name
and previous_benchmark_name is None
or name < benchmark_name
and name > previous_benchmark_name
):
if read_test_dependencies:
file_new_contents.append("import " + name + "\n" + line)
else:
# all test dependencies are first specified, so from now
# on we can look where to insert the new benchmark.
read_test_dependencies = True
file_new_contents.append(line)
else:
file_new_contents.append(line)
previous_benchmark_name = benchmark_name
else:
file_new_contents.append(line)
with open(relative_path, "w") as f:
for line in file_new_contents:
f.write(line)
def add_register_benchmark(name):
"""Adds an `import` statement to the `main.swift` file for the new
benchmark.
"""
relative_path = create_relative_path("../utils/main.swift")
file_contents = []
with open(relative_path, "r") as f:
file_contents = f.readlines()
file_new_contents = insert_line_alphabetically(
name,
"registerBenchmark(" + name + ")\n",
file_contents,
r"registerBenchmark\(([a-zA-Z]+)\)",
)
with open(relative_path, "w") as f:
for line in file_new_contents:
f.write(line)
def insert_line_alphabetically(name, new_line, lines, regex):
"""Iterates through the given lines and executes the regex on each line to
find where the new benchmark should be inserted with the given `new_line`.
"""
# the name of the previous seen benchmark in order to insert the new
# one at the correct position
previous_benchmark_name = None
# the new contents of the file
updated_lines = []
for line in lines:
# apply regex and get name of benchmark on this line
match = re.search(regex, line)
if match and match.group(1):
benchmark_name = match.group(1)
# check if we're at the line where we have to insert the new
# benchmark in the correct alphabetical order
if (
name < benchmark_name
and previous_benchmark_name is None
or name < benchmark_name
and name > previous_benchmark_name
):
updated_lines.append(new_line + line)
else:
updated_lines.append(line)
previous_benchmark_name = benchmark_name
else:
updated_lines.append(line)
return updated_lines
def create_relative_path(file_path):
return os.path.join(os.path.dirname(__file__), file_path)
if __name__ == "__main__":
main()