Skip to content

Commit 5522faa

Browse files
alexwlchanatrick
authored andcommitted
[utils] Minor Python tidies in utils/swift-bench.py (#4618)
* [utils] Minor Python tidies in utils/swift-bench.py * Track the line number where benchmarks are found * Tidy up the argument parsing code for the default `--compiler` value * Pull the regex for finding benchmarks out into a top-level constant, and add comments for improved readability * Fix Python lint issue
1 parent caa173b commit 5522faa

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

utils/swift-bench.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@
4242
import sys
4343

4444

45+
# This regular expression is looking for Swift functions named `bench_*`
46+
# that take no arguments and return an Int. The Swift code for such
47+
# a function is:
48+
#
49+
# func bench_myname() {
50+
# // function body goes here
51+
# }
52+
BENCH_RE = re.compile(
53+
r"^\s*" # whitespace at the start of the line
54+
r"func\s+" # 'func' keyword, which must be followed by
55+
# at least one space
56+
r"bench_([a-zA-Z0-9_]+)\s*"
57+
# name of the function
58+
r"\s*\(\s*\)" # argument list
59+
r"\s*->\s*Int\s*" # return type
60+
r"({)?" # opening brace of the function body
61+
r"\s*$" # whitespace ot the end of the line
62+
)
63+
64+
4565
def pstdev(sample):
4666
"""Given a list of numbers, return the population standard deviation.
4767
@@ -85,7 +105,9 @@ def parse_arguments(self):
85105
"-v", "--verbosity",
86106
help="increase output verbosity", type=int)
87107
parser.add_argument("files", help="input files", nargs='+')
88-
parser.add_argument('-c', '--compiler', help="compiler to use")
108+
parser.add_argument(
109+
'-c', '--compiler',
110+
help="compiler to use", default="swiftc")
89111
parser.add_argument(
90112
'-t', '--timelimit',
91113
help="Time limit for every test", type=int)
@@ -98,12 +120,9 @@ def parse_arguments(self):
98120
if args.verbosity:
99121
self.verbose_level = args.verbosity
100122
self.sources = args.files
123+
self.compiler = args.compiler
101124
if args.flags:
102125
self.opt_flags = args.flags
103-
if args.compiler:
104-
self.compiler = args.compiler
105-
else:
106-
self.compiler = 'swiftc'
107126
if args.timelimit and args.timelimit > 0:
108127
self.time_limit = args.timelimit
109128
if args.sampletime and args.sampletime > 0:
@@ -171,15 +190,12 @@ def process_source(self, name):
171190
main()
172191
"""
173192

174-
bench_re = re.compile(
175-
"^\s*func\s\s*bench_([a-zA-Z0-9_]+)" +
176-
"\s*\(\s*\)\s*->\s*Int\s*({)?\s*$")
177193
with open(name) as f:
178194
lines = list(f)
179195
output = header
180196
looking_for_curly_brace = False
181197
test_names = []
182-
for l in lines:
198+
for lineno, l in enumerate(lines, start=1):
183199
if looking_for_curly_brace:
184200
output += l
185201
if "{" not in l:
@@ -188,13 +204,13 @@ def process_source(self, name):
188204
output += into_bench
189205
continue
190206

191-
m = bench_re.match(l)
207+
m = BENCH_RE.match(l)
192208
if m:
193209
output += before_bench
194210
output += l
195211
bench_name = m.group(1)
196-
# TODO: Keep track of the line number as well
197-
self.log("Benchmark found: %s" % bench_name, 3)
212+
self.log("Benchmark found: %s (line %d)" %
213+
(bench_name, lineno), 3)
198214
self.tests[
199215
name + ":" +
200216
bench_name] = Test(bench_name, name, "", "")

0 commit comments

Comments
 (0)