1010##===----------------------------------------------------------------------===##
1111
1212# python3 createBenchmark.py MyRegexBenchmark
13- # reference: https://github.com/apple/swift/blob/main/benchmark/scripts/create_benchmark.py
13+ # reference:
14+ # https://github.com/apple/swift/blob/main/benchmark/scripts/create_benchmark.py
1415
1516import argparse
1617import os
1718
18- template = """//===----------------------------------------------------------------------===//
19+ template = """\
20+ //===----------------------------------------------------------------------===//
1921//
2022// This source file is part of the Swift.org open source project
2123//
3436}}
3537"""
3638
39+
3740def main ():
3841 p = argparse .ArgumentParser ()
3942 p .add_argument ("name" , help = "The name of the new benchmark to be created" )
4043 args = p .parse_args ()
41-
44+
4245 # create a file in Sources/RegexBenchmark/Suite with the benchmark template
4346 create_benchmark_file (args .name )
44-
47+
4548 # add to the registration function in BenchmarkRunner
4649 register_benchmark (args .name )
4750
51+
4852def create_benchmark_file (name ):
49- contents = template .format (name = name )
53+ contents = template .format (name = name )
5054 relative_path = create_relative_path ("../Sources/RegexBenchmark/Suite/" )
5155 source_file_path = os .path .join (relative_path , name + ".swift" )
52-
56+
5357 print (f"Creating new benchmark file: { source_file_path } " )
5458 with open (source_file_path , "w" ) as f :
5559 f .write (contents )
5660
61+
5762def register_benchmark (name ):
58- relative_path = create_relative_path ("../Sources/RegexBenchmark/BenchmarkRegistration.swift" )
63+ relative_path = create_relative_path (
64+ "../Sources/RegexBenchmark/BenchmarkRegistration.swift" )
5965
6066 # read current contents into an array
6167 file_contents = []
6268 with open (relative_path , "r" ) as f :
6369 file_contents = f .readlines ()
64-
70+
6571 new_file_contents = []
6672 for line in file_contents :
6773 if "end of registrations" not in line :
@@ -70,14 +76,16 @@ def register_benchmark(name):
7076 # add the newest benchmark
7177 new_file_contents .append (f" self.add{ name } ()\n " )
7278 new_file_contents .append (line )
73-
79+
7480 # write the new contents
7581 with open (relative_path , "w" ) as f :
7682 for line in new_file_contents :
7783 f .write (line )
78-
84+
85+
7986def create_relative_path (file_path ):
8087 return os .path .join (os .path .dirname (__file__ ), file_path )
8188
89+
8290if __name__ == "__main__" :
8391 main ()
0 commit comments