@@ -38,7 +38,9 @@ def run_cmd(cmd: List[str], cwd: Optional[str] = None) -> str:
3838 progress in real time while the command runs.
3939 """
4040 try :
41- proc = subprocess .Popen (cmd , cwd = cwd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True )
41+ proc = subprocess .Popen (
42+ cmd , cwd = cwd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True
43+ )
4244 except FileNotFoundError :
4345 # Helpful message if the executable is not found
4446 print (f"Command not found: { cmd [0 ]} " )
@@ -50,45 +52,49 @@ def run_cmd(cmd: List[str], cwd: Optional[str] = None) -> str:
5052 # Stream lines as they appear and capture them for returning
5153 for line in proc .stdout :
5254 # Print immediately so callers (and CI) can observe progress
53- print (line , end = '' )
55+ print (line , end = "" )
5456 output_lines .append (line )
5557 proc .wait ()
5658 except KeyboardInterrupt :
5759 # If the user interrupts, ensure the child process is terminated
5860 proc .kill ()
5961 proc .wait ()
60- print (' \n Command interrupted by user.' )
62+ print (" \n Command interrupted by user." )
6163 raise
6264
63- output = '' .join (output_lines )
65+ output = "" .join (output_lines )
6466 if proc .returncode != 0 :
65- print (f"Command failed: { ' ' .join (cmd )} \n Exit: { proc .returncode } \n Output:\n { output } " )
67+ print (
68+ f"Command failed: { ' ' .join (cmd )} \n Exit: { proc .returncode } \n Output:\n { output } "
69+ )
6670 raise SystemExit (proc .returncode )
6771 return output
6872
6973
7074def build_benchmarks (mvnw : str , module : str ) -> None :
7175 print (f"Building Maven module '{ module } ' using { mvnw } (this may take a while)..." )
72- cmd = [mvnw , ' -pl' , module , ' -am' , ' -DskipTests' , ' package' ]
76+ cmd = [mvnw , " -pl" , module , " -am" , " -DskipTests" , " package" ]
7377 run_cmd (cmd )
7478 print ("Build completed." )
7579
7680
7781def find_benchmarks_jar (module : str ) -> str :
78- pattern = os .path .join (module , ' target' , ' *.jar' )
79- jars = [p for p in glob .glob (pattern ) if ' original' not in p and p .endswith (' .jar' )]
82+ pattern = os .path .join (module , " target" , " *.jar" )
83+ jars = [p for p in glob .glob (pattern ) if " original" not in p and p .endswith (" .jar" )]
8084 # prefer jar whose basename contains module name
8185 jars_pref = [j for j in jars if module in os .path .basename (j )]
8286 chosen = (jars_pref or jars )[:1 ]
8387 if not chosen :
84- raise FileNotFoundError (f"No jar found in { os .path .join (module , 'target' )} (tried: { pattern } )" )
88+ raise FileNotFoundError (
89+ f"No jar found in { os .path .join (module , 'target' )} (tried: { pattern } )"
90+ )
8591 jar = chosen [0 ]
8692 print (f"Using jar: { jar } " )
8793 return jar
8894
8995
9096def run_jmh (jar : str , java_cmd : str , extra_args : Optional [str ]) -> str :
91- args = [java_cmd , ' -jar' , jar , ' -rf' , ' text' ]
97+ args = [java_cmd , " -jar" , jar , " -rf" , " text" ]
9298 if extra_args :
9399 args += shlex .split (extra_args )
94100 print (f"Running JMH: { ' ' .join (args )} " )
@@ -99,16 +105,23 @@ def run_jmh(jar: str, java_cmd: str, extra_args: Optional[str]) -> str:
99105
100106def extract_first_table (jmh_output : str ) -> str :
101107 # Try to extract the first table that starts with "Benchmark" header and continues until a blank line
102- m = re .search (r' (\nBenchmark\s+Mode[\s\S]*?)(?:\n\s*\n|\Z)' , jmh_output )
108+ m = re .search (r" (\nBenchmark\s+Mode[\s\S]*?)(?:\n\s*\n|\Z)" , jmh_output )
103109 if not m :
104110 # fallback: collect all lines that contain 'thrpt' plus a header if present
105- lines = [l for l in jmh_output .splitlines () if ' thrpt' in l ]
111+ lines = [l for l in jmh_output .splitlines () if " thrpt" in l ]
106112 if not lines :
107113 raise ValueError ('Could not find any "thrpt" lines in JMH output' )
108114 # try to find header
109- header = next ((l for l in jmh_output .splitlines () if l .startswith ('Benchmark' ) and 'Mode' in l ), 'Benchmark Mode Cnt Score Error Units' )
110- return header + '\n ' + '\n ' .join (lines )
111- table = m .group (1 ).strip ('\n ' )
115+ header = next (
116+ (
117+ l
118+ for l in jmh_output .splitlines ()
119+ if l .startswith ("Benchmark" ) and "Mode" in l
120+ ),
121+ "Benchmark Mode Cnt Score Error Units" ,
122+ )
123+ return header + "\n " + "\n " .join (lines )
124+ table = m .group (1 ).strip ("\n " )
112125 # Ensure we return only the table lines (remove any leading iteration info lines that JMH sometimes prints)
113126 # Normalize spaces: keep as-is
114127 return table
@@ -123,35 +136,39 @@ def filter_table_for_class(table: str, class_name: str) -> Optional[str]:
123136 # find header line index (starts with 'Benchmark' and contains 'Mode')
124137 header_idx = None
125138 for i , ln in enumerate (lines ):
126- if ln .strip ().startswith (' Benchmark' ) and ' Mode' in ln :
139+ if ln .strip ().startswith (" Benchmark" ) and " Mode" in ln :
127140 header_idx = i
128141 break
129- header = lines [header_idx ] if header_idx is not None else 'Benchmark Mode Cnt Score Error Units'
142+ header = (
143+ lines [header_idx ]
144+ if header_idx is not None
145+ else "Benchmark Mode Cnt Score Error Units"
146+ )
130147
131148 matched = []
132- pattern = re .compile (r' ^\s*' + re .escape (class_name ) + r'\.' )
133- for ln in lines [header_idx + 1 if header_idx is not None else 0 :]:
134- if ' thrpt' in ln and pattern .search (ln ):
149+ pattern = re .compile (r" ^\s*" + re .escape (class_name ) + r"\." )
150+ for ln in lines [header_idx + 1 if header_idx is not None else 0 :]:
151+ if " thrpt" in ln and pattern .search (ln ):
135152 matched .append (ln )
136153
137154 if not matched :
138155 return None
139- return header + ' \n ' + ' \n ' .join (matched )
156+ return header + " \n " + " \n " .join (matched )
140157
141158
142159def update_pre_blocks_under_module (module : str , table : str ) -> List [str ]:
143160 # Find files under module and update any <pre>...</pre> block that contains 'thrpt'
144161 updated_files = []
145- for path in glob .glob (os .path .join (module , '**' ), recursive = True ):
162+ for path in glob .glob (os .path .join (module , "**" ), recursive = True ):
146163 if os .path .isdir (path ):
147164 continue
148165 try :
149- with open (path , 'r' , encoding = ' utf-8' ) as f :
166+ with open (path , "r" , encoding = " utf-8" ) as f :
150167 content = f .read ()
151168 except Exception :
152169 continue
153170 # quick filter
154- if ' <pre>' not in content or ' thrpt' not in content :
171+ if " <pre>" not in content or " thrpt" not in content :
155172 continue
156173
157174 original = content
@@ -169,21 +186,21 @@ def update_pre_blocks_under_module(module: str, table: str) -> List[str]:
169186
170187 # Regex to find any line-starting Javadoc prefix like " * " before <pre>
171188 # This will match patterns like: " * <pre>... </pre>" and capture the prefix (e.g. " * ")
172- pattern = re .compile (r' (?m)^(?P<prefix>[ \t]*\*[ \t]*)<pre>[\s\S]*?</pre>' )
189+ pattern = re .compile (r" (?m)^(?P<prefix>[ \t]*\*[ \t]*)<pre>[\s\S]*?</pre>" )
173190
174191 def repl (m : re .Match ) -> str :
175- prefix = m .group (' prefix' )
192+ prefix = m .group (" prefix" )
176193 # Build the new block with the same prefix on each line
177194 lines = filtered_table .splitlines ()
178- replaced = prefix + ' <pre>\n '
195+ replaced = prefix + " <pre>\n "
179196 for ln in lines :
180- replaced += prefix + ln .rstrip () + ' \n '
181- replaced += prefix + ' </pre>'
197+ replaced += prefix + ln .rstrip () + " \n "
198+ replaced += prefix + " </pre>"
182199 return replaced
183200
184201 new_content , nsubs = pattern .subn (repl , content )
185202 if nsubs > 0 and new_content != original :
186- with open (path , 'w' , encoding = ' utf-8' ) as f :
203+ with open (path , "w" , encoding = " utf-8" ) as f :
187204 f .write (new_content )
188205 updated_files .append (path )
189206 print (f"Updated { path } : replaced { nsubs } <pre> block(s)" )
@@ -192,34 +209,41 @@ def repl(m: re.Match) -> str:
192209
193210def main (argv : List [str ]):
194211 parser = argparse .ArgumentParser ()
195- parser .add_argument ('--mvnw' , default = './mvnw' , help = 'Path to maven wrapper' )
196- parser .add_argument ('--module' , default = 'benchmarks' , help = 'Module directory to build/run' )
197- parser .add_argument ('--java' , default = 'java' , help = 'Java command' )
198- parser .add_argument ('--jmh-args' , default = '' , help = 'Extra arguments to pass to the JMH main (e.g. "-f 1 -wi 0 -i 1")' )
212+ parser .add_argument ("--mvnw" , default = "./mvnw" , help = "Path to maven wrapper" )
213+ parser .add_argument (
214+ "--module" , default = "benchmarks" , help = "Module directory to build/run"
215+ )
216+ parser .add_argument ("--java" , default = "java" , help = "Java command" )
217+ parser .add_argument (
218+ "--jmh-args" ,
219+ default = "" ,
220+ help = 'Extra arguments to pass to the JMH main (e.g. "-f 1 -wi 0 -i 1")' ,
221+ )
199222 args = parser .parse_args (argv )
200223
201224 build_benchmarks (args .mvnw , args .module )
202225 jar = find_benchmarks_jar (args .module )
203226 output = run_jmh (jar , args .java , args .jmh_args )
204227
205228 # Print a short preview of the JMH output
206- preview = ' \n ' .join (output .splitlines ()[:120 ])
207- print (' \n --- JMH output preview ---' )
229+ preview = " \n " .join (output .splitlines ()[:120 ])
230+ print (" \n --- JMH output preview ---" )
208231 print (preview )
209- print (' --- end preview ---\n ' )
232+ print (" --- end preview ---\n " )
210233
211234 table = extract_first_table (output )
212235
213236 updated = update_pre_blocks_under_module (args .module , table )
214237
215238 if not updated :
216- print ('No files were updated (no <pre> blocks with "thrpt" found under the module).' )
239+ print (
240+ 'No files were updated (no <pre> blocks with "thrpt" found under the module).'
241+ )
217242 else :
218- print (' \n Updated files:' )
243+ print (" \n Updated files:" )
219244 for p in updated :
220- print (' -' , p )
245+ print (" -" , p )
221246
222247
223- if __name__ == ' __main__' :
248+ if __name__ == " __main__" :
224249 main (sys .argv [1 :])
225-
0 commit comments