44import os
55import re
66
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-
307def main ():
318 p = argparse .ArgumentParser ()
329 p .add_argument ('name' , help = 'The name of the new benchmark to be created' )
@@ -42,38 +19,57 @@ def main():
4219 add_register_benchmark (args .name )
4320
4421def 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+ """
4625 relative_path = create_relative_path ('../CMakeLists.txt' )
4726
4827 file_contents = []
4928 with open (relative_path , 'r' ) as f :
5029 file_contents = f .readlines ()
5130
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+ )
5337 with open (relative_path , 'w' ) as f :
5438 for line in file_new_contents :
5539 f .write (line )
5640
5741def 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+ """
6045
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.
6152 formatted_template = benchmark_template .format (name = name )
53+
54+ relative_path = create_relative_path ('../single-source/' )
6255 source_file_path = os .path .join (relative_path , name + '.swift' )
6356 with open (source_file_path , 'w' ) as f :
6457 f .write (formatted_template )
6558
6659def 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+ """
6863 relative_path = create_relative_path ('../utils/main.swift' )
6964
7065 # read current contents into an array
7166 file_contents = []
7267 with open (relative_path , 'r' ) as f :
7368 file_contents = f .readlines ()
7469
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.
7773 read_test_dependencies = False
7874 previous_benchmark_name = None
7975 file_new_contents = []
@@ -82,40 +78,52 @@ def add_import_benchmark(name):
8278 match = re .search (r"import ([a-zA-Z]+)" , line )
8379 if match and match .group (1 ):
8480 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 )):
8785 if read_test_dependencies :
8886 file_new_contents .append ('import ' + name + '\n ' + line )
8987 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.
9190 read_test_dependencies = True
9291 file_new_contents .append (line )
9392 else :
9493 file_new_contents .append (line )
9594 previous_benchmark_name = benchmark_name
9695 else :
9796 file_new_contents .append (line )
98-
9997 with open (relative_path , 'w' ) as f :
10098 for line in file_new_contents :
10199 f .write (line )
102100
103101def 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+ """
105105 relative_path = create_relative_path ('../utils/main.swift' )
106106
107107 file_contents = []
108108 with open (relative_path , 'r' ) as f :
109109 file_contents = f .readlines ()
110110
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+ )
112117 with open (relative_path , 'w' ) as f :
113118 for line in file_new_contents :
114119 f .write (line )
115120
116121def 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
119127 previous_benchmark_name = None
120128 # the new contents of the file
121129 updated_lines = []
@@ -124,8 +132,10 @@ def insert_line_alphabetically(name, new_line, lines, regex):
124132 match = re .search (regex , line )
125133 if match and match .group (1 ):
126134 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 )):
129139 updated_lines .append (new_line + line )
130140 else :
131141 updated_lines .append (line )
@@ -138,4 +148,4 @@ def create_relative_path(file_path):
138148 return os .path .join (os .path .dirname (__file__ ), file_path )
139149
140150if __name__ == "__main__" :
141- main ()
151+ main ()
0 commit comments