4
4
import os
5
5
import re
6
6
7
- benchmark_template = """//===--- {name}.swift -------------------------------------------===//
8
- //
9
- // This source file is part of the Swift.org open source project
10
- //
11
- // Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
12
- // Licensed under Apache License v2.0 with Runtime Library Exception
13
- //
14
- // See https://swift.org/LICENSE.txt for license information
15
- // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
16
- //
17
- //===----------------------------------------------------------------------===//
18
-
19
- import TestsUtils
20
-
21
- public let {name} = [
22
- BenchmarkInfo(name: "{name}", runFunction: run_{name}, tags: [.validation, .api]),
23
- ]
24
-
25
- @inline(never)
26
- public func run_{name}(N: Int) {{
27
- // TODO
28
- }}"""
29
-
30
7
def main ():
31
8
p = argparse .ArgumentParser ()
32
9
p .add_argument ('name' , help = 'The name of the new benchmark to be created' )
@@ -42,38 +19,57 @@ def main():
42
19
add_register_benchmark (args .name )
43
20
44
21
def update_cmakelists (name ):
45
- """Adds a new entry to the `CMakeLists.txt` file with the given benchmark name."""
22
+ """Adds a new entry to the `CMakeLists.txt` file with the given
23
+ benchmark name.
24
+ """
46
25
relative_path = create_relative_path ('../CMakeLists.txt' )
47
26
48
27
file_contents = []
49
28
with open (relative_path , 'r' ) as f :
50
29
file_contents = f .readlines ()
51
30
52
- file_new_contents = insert_line_alphabetically (name , ' single-source/' + name + '\n ' , file_contents , r" single-source\/([a-zA-Z]+)" )
31
+ file_new_contents = insert_line_alphabetically (
32
+ name ,
33
+ ' single-source/' + name + '\n ' ,
34
+ file_contents ,
35
+ r" single-source\/([a-zA-Z]+)"
36
+ )
53
37
with open (relative_path , 'w' ) as f :
54
38
for line in file_new_contents :
55
39
f .write (line )
56
40
57
41
def create_benchmark_file (name ):
58
- """Creates a new Swift file with the given name based on the template and places it in the `single-source` directory."""
59
- relative_path = create_relative_path ('../single-source/' )
42
+ """Creates a new Swift file with the given name based on the template
43
+ and places it in the `single-source` directory.
44
+ """
60
45
46
+ template_path = create_relative_path ('Template.swift' )
47
+ benchmark_template = ''
48
+ with open (template_path , 'r' ) as f :
49
+ benchmark_template = '' .join (f .readlines ())
50
+
51
+ # fill in template with benchmark name.
61
52
formatted_template = benchmark_template .format (name = name )
53
+
54
+ relative_path = create_relative_path ('../single-source/' )
62
55
source_file_path = os .path .join (relative_path , name + '.swift' )
63
56
with open (source_file_path , 'w' ) as f :
64
57
f .write (formatted_template )
65
58
66
59
def add_import_benchmark (name ):
67
- """Adds an `import` statement to the `main.swift` file for the new benchmark."""
60
+ """Adds an `import` statement to the `main.swift` file for the new
61
+ benchmark.
62
+ """
68
63
relative_path = create_relative_path ('../utils/main.swift' )
69
64
70
65
# read current contents into an array
71
66
file_contents = []
72
67
with open (relative_path , 'r' ) as f :
73
68
file_contents = f .readlines ()
74
69
75
- inserted = False
76
- # 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.
70
+ # the test dependencies are placed before all benchmarks, so we have to
71
+ # insert the benchmark in the right alphabetical order after we have seen
72
+ # all test dependencies.
77
73
read_test_dependencies = False
78
74
previous_benchmark_name = None
79
75
file_new_contents = []
@@ -82,40 +78,52 @@ def add_import_benchmark(name):
82
78
match = re .search (r"import ([a-zA-Z]+)" , line )
83
79
if match and match .group (1 ):
84
80
benchmark_name = match .group (1 )
85
- # find where to insert the new benchmark in the right alphabetical order
86
- if (name < benchmark_name and previous_benchmark_name == None ) or (name < benchmark_name and name > previous_benchmark_name ):
81
+ # find where to insert the new benchmark in the right alphabetical
82
+ # order.
83
+ if ((name < benchmark_name and previous_benchmark_name is None ) or
84
+ (name < benchmark_name and name > previous_benchmark_name )):
87
85
if read_test_dependencies :
88
86
file_new_contents .append ('import ' + name + '\n ' + line )
89
87
else :
90
- # all test dependencies are first specified, so from now on we can look where to insert the new benchmark.
88
+ # all test dependencies are first specified, so from now
89
+ # on we can look where to insert the new benchmark.
91
90
read_test_dependencies = True
92
91
file_new_contents .append (line )
93
92
else :
94
93
file_new_contents .append (line )
95
94
previous_benchmark_name = benchmark_name
96
95
else :
97
96
file_new_contents .append (line )
98
-
99
97
with open (relative_path , 'w' ) as f :
100
98
for line in file_new_contents :
101
99
f .write (line )
102
100
103
101
def add_register_benchmark (name ):
104
- """Adds an `import` statement to the `main.swift` file for the new benchmark."""
102
+ """Adds an `import` statement to the `main.swift` file for the new
103
+ benchmark.
104
+ """
105
105
relative_path = create_relative_path ('../utils/main.swift' )
106
106
107
107
file_contents = []
108
108
with open (relative_path , 'r' ) as f :
109
109
file_contents = f .readlines ()
110
110
111
- file_new_contents = insert_line_alphabetically (name , 'registerBenchmark(' + name + ')\n ' , file_contents , r"registerBenchmark\(([a-zA-Z]+)\)" )
111
+ file_new_contents = insert_line_alphabetically (
112
+ name ,
113
+ 'registerBenchmark(' + name + ')\n ' ,
114
+ file_contents ,
115
+ r"registerBenchmark\(([a-zA-Z]+)\)"
116
+ )
112
117
with open (relative_path , 'w' ) as f :
113
118
for line in file_new_contents :
114
119
f .write (line )
115
120
116
121
def insert_line_alphabetically (name , new_line , lines , regex ):
117
- """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`."""
118
- # the name of the previous seen benchmark in order to insert the new one at the correct position
122
+ """Iterates through the given lines and executes the regex on each line
123
+ to find where the new benchmark should be inserted with the given `new_line`.
124
+ """
125
+ # the name of the previous seen benchmark in order to insert the new
126
+ # one at the correct position
119
127
previous_benchmark_name = None
120
128
# the new contents of the file
121
129
updated_lines = []
@@ -124,8 +132,10 @@ def insert_line_alphabetically(name, new_line, lines, regex):
124
132
match = re .search (regex , line )
125
133
if match and match .group (1 ):
126
134
benchmark_name = match .group (1 )
127
- # check if we're at the line where we have to insert the new benchmark in the correct alphabetical order
128
- if (name < benchmark_name and previous_benchmark_name == None ) or (name < benchmark_name and name > previous_benchmark_name ):
135
+ # check if we're at the line where we have to insert the new
136
+ # benchmark in the correct alphabetical order
137
+ if ((name < benchmark_name and previous_benchmark_name is None ) or
138
+ (name < benchmark_name and name > previous_benchmark_name )):
129
139
updated_lines .append (new_line + line )
130
140
else :
131
141
updated_lines .append (line )
@@ -138,4 +148,4 @@ def create_relative_path(file_path):
138
148
return os .path .join (os .path .dirname (__file__ ), file_path )
139
149
140
150
if __name__ == "__main__" :
141
- main ()
151
+ main ()
0 commit comments