Skip to content

Commit d74e3c9

Browse files
committed
update benchmark
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent dd5f9c3 commit d74e3c9

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

.mise/tasks/run_benchmarks.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ def extract_first_table(jmh_output: str) -> str:
8282
return table
8383

8484

85+
def filter_table_for_class(table: str, class_name: str) -> Optional[str]:
86+
"""
87+
Return a table string that contains only the header and the lines belonging to `class_name`.
88+
If no matching lines are found, return None.
89+
"""
90+
lines = table.splitlines()
91+
# find header line index (starts with 'Benchmark' and contains 'Mode')
92+
header_idx = None
93+
for i, ln in enumerate(lines):
94+
if ln.strip().startswith('Benchmark') and 'Mode' in ln:
95+
header_idx = i
96+
break
97+
header = lines[header_idx] if header_idx is not None else 'Benchmark Mode Cnt Score Error Units'
98+
99+
matched = []
100+
pattern = re.compile(r'^\s*' + re.escape(class_name) + r'\.')
101+
for ln in lines[header_idx + 1 if header_idx is not None else 0:]:
102+
if 'thrpt' in ln and pattern.search(ln):
103+
matched.append(ln)
104+
105+
if not matched:
106+
return None
107+
return header + '\n' + '\n'.join(matched)
108+
109+
85110
def update_pre_blocks_under_module(module: str, table: str) -> List[str]:
86111
# Find files under module and update any <pre>...</pre> block that contains 'thrpt'
87112
updated_files = []
@@ -100,14 +125,24 @@ def update_pre_blocks_under_module(module: str, table: str) -> List[str]:
100125
original = content
101126
new_content = content
102127

128+
# Determine the class name from the filename (e.g. TextFormatUtilBenchmark.java -> TextFormatUtilBenchmark)
129+
base = os.path.basename(path)
130+
class_name = os.path.splitext(base)[0]
131+
132+
# Build a filtered table for this class; if no matching lines, skip updating this file
133+
filtered_table = filter_table_for_class(table, class_name)
134+
if filtered_table is None:
135+
# nothing to update for this class
136+
continue
137+
103138
# Regex to find any line-starting Javadoc prefix like " * " before <pre>
104139
# This will match patterns like: " * <pre>... </pre>" and capture the prefix (e.g. " * ")
105140
pattern = re.compile(r'(?m)^(?P<prefix>[ \t]*\*[ \t]*)<pre>[\s\S]*?</pre>')
106141

107142
def repl(m: re.Match) -> str:
108143
prefix = m.group('prefix')
109144
# Build the new block with the same prefix on each line
110-
lines = table.splitlines()
145+
lines = filtered_table.splitlines()
111146
replaced = prefix + '<pre>\n'
112147
for ln in lines:
113148
replaced += prefix + ln.rstrip() + '\n'

.mise/tasks/test_run_benchmarks.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import re
3+
import sys
4+
import tempfile
5+
import unittest
6+
7+
# Ensure the tasks directory is importable when running the test directly
8+
here = os.path.dirname(__file__)
9+
if here not in sys.path:
10+
sys.path.insert(0, here)
11+
12+
from run_benchmarks import update_pre_blocks_under_module
13+
14+
15+
class TestRunBenchmarksFiltering(unittest.TestCase):
16+
def setUp(self):
17+
# sample JMH table with mixed-class lines
18+
self.table = (
19+
'Benchmark Mode Cnt Score Error Units\n'
20+
'CounterBenchmark.codahaleIncNoLabels thrpt 57881.585 ops/s\n'
21+
'HistogramBenchmark.prometheusNative thrpt 2385.134 ops/s\n'
22+
'TextFormatUtilBenchmark.prometheusWriteToNull thrpt 885331.328 ops/s\n'
23+
'CounterBenchmark.prometheusInc thrpt 54090.469 ops/s\n'
24+
)
25+
26+
# create temp dir to act as module path
27+
self.tmpdir = tempfile.TemporaryDirectory()
28+
self.module_path = self.tmpdir.name
29+
30+
# Create three files with a javadoc <pre> block that contains mixed results
31+
self.files = {}
32+
javadoc_pre = (
33+
'/**\n'
34+
' * Example javadoc\n'
35+
' * <pre>\n'
36+
' * Benchmark Mode Cnt Score Error Units\n'
37+
' * CounterBenchmark.codahaleIncNoLabels thrpt 57881.585 ops/s\n'
38+
' * HistogramBenchmark.prometheusNative thrpt 2385.134 ops/s\n'
39+
' * TextFormatUtilBenchmark.prometheusWriteToNull thrpt 885331.328 ops/s\n'
40+
' * CounterBenchmark.prometheusInc thrpt 54090.469 ops/s\n'
41+
' * </pre>\n'
42+
' */\n'
43+
)
44+
45+
for cls in ('CounterBenchmark', 'HistogramBenchmark', 'TextFormatUtilBenchmark'):
46+
fname = os.path.join(self.module_path, f'{cls}.java')
47+
with open(fname, 'w', encoding='utf-8') as f:
48+
f.write(javadoc_pre)
49+
f.write(f'public class {cls} {{}}\n')
50+
self.files[cls] = fname
51+
52+
def tearDown(self):
53+
self.tmpdir.cleanup()
54+
55+
def _read_pre_contents(self, path):
56+
with open(path, 'r', encoding='utf-8') as f:
57+
content = f.read()
58+
m = re.search(r'<pre>\n([\s\S]*?)</pre>', content)
59+
return m.group(1) if m else ''
60+
61+
def test_update_only_inserts_matching_class_lines(self):
62+
updated = update_pre_blocks_under_module(self.module_path, self.table)
63+
# All three files should be updated
64+
self.assertEqual(set(os.path.basename(p) for p in updated),
65+
{os.path.basename(self.files['CounterBenchmark']),
66+
os.path.basename(self.files['HistogramBenchmark']),
67+
os.path.basename(self.files['TextFormatUtilBenchmark'])})
68+
69+
# Verify CounterBenchmark file contains only CounterBenchmark lines
70+
cb_pre = self._read_pre_contents(self.files['CounterBenchmark'])
71+
self.assertIn('CounterBenchmark.codahaleIncNoLabels', cb_pre)
72+
self.assertIn('CounterBenchmark.prometheusInc', cb_pre)
73+
self.assertNotIn('HistogramBenchmark.prometheusNative', cb_pre)
74+
self.assertNotIn('TextFormatUtilBenchmark.prometheusWriteToNull', cb_pre)
75+
76+
# Verify HistogramBenchmark contains only its line
77+
hb_pre = self._read_pre_contents(self.files['HistogramBenchmark'])
78+
self.assertIn('HistogramBenchmark.prometheusNative', hb_pre)
79+
self.assertNotIn('CounterBenchmark.codahaleIncNoLabels', hb_pre)
80+
self.assertNotIn('TextFormatUtilBenchmark.prometheusWriteToNull', hb_pre)
81+
82+
# Verify TextFormatUtilBenchmark contains only its line
83+
tf_pre = self._read_pre_contents(self.files['TextFormatUtilBenchmark'])
84+
self.assertIn('TextFormatUtilBenchmark.prometheusWriteToNull', tf_pre)
85+
self.assertNotIn('CounterBenchmark.prometheusInc', tf_pre)
86+
self.assertNotIn('HistogramBenchmark.prometheusNative', tf_pre)
87+
88+
89+
if __name__ == '__main__':
90+
unittest.main()

0 commit comments

Comments
 (0)