|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | + |
| 7 | +from update_benchmarks import update_pre_blocks_under_module |
| 8 | + |
| 9 | +# Ensure the tasks directory is importable when running the test directly |
| 10 | +here = os.path.dirname(__file__) |
| 11 | +if here not in sys.path: |
| 12 | + sys.path.insert(0, here) |
| 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 ( |
| 46 | + "CounterBenchmark", |
| 47 | + "HistogramBenchmark", |
| 48 | + "TextFormatUtilBenchmark", |
| 49 | + ): |
| 50 | + fname = os.path.join(self.module_path, f"{cls}.java") |
| 51 | + with open(fname, "w", encoding="utf-8") as f: |
| 52 | + f.write(javadoc_pre) |
| 53 | + f.write(f"public class {cls} {{}}\n") |
| 54 | + self.files[cls] = fname |
| 55 | + |
| 56 | + def tearDown(self): |
| 57 | + self.tmpdir.cleanup() |
| 58 | + |
| 59 | + def _read_pre_contents(self, path): |
| 60 | + with open(path, "r", encoding="utf-8") as f: |
| 61 | + content = f.read() |
| 62 | + m = re.search(r"<pre>\n([\s\S]*?)</pre>", content) |
| 63 | + return m.group(1) if m else "" |
| 64 | + |
| 65 | + def test_update_only_inserts_matching_class_lines(self): |
| 66 | + updated = update_pre_blocks_under_module(self.module_path, self.table) |
| 67 | + # All three files should be updated |
| 68 | + self.assertEqual( |
| 69 | + set(os.path.basename(p) for p in updated), |
| 70 | + { |
| 71 | + os.path.basename(self.files["CounterBenchmark"]), |
| 72 | + os.path.basename(self.files["HistogramBenchmark"]), |
| 73 | + os.path.basename(self.files["TextFormatUtilBenchmark"]), |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + # Verify CounterBenchmark file contains only CounterBenchmark lines |
| 78 | + cb_pre = self._read_pre_contents(self.files["CounterBenchmark"]) |
| 79 | + self.assertIn("CounterBenchmark.codahaleIncNoLabels", cb_pre) |
| 80 | + self.assertIn("CounterBenchmark.prometheusInc", cb_pre) |
| 81 | + self.assertNotIn("HistogramBenchmark.prometheusNative", cb_pre) |
| 82 | + self.assertNotIn("TextFormatUtilBenchmark.prometheusWriteToNull", cb_pre) |
| 83 | + |
| 84 | + # Verify HistogramBenchmark contains only its line |
| 85 | + hb_pre = self._read_pre_contents(self.files["HistogramBenchmark"]) |
| 86 | + self.assertIn("HistogramBenchmark.prometheusNative", hb_pre) |
| 87 | + self.assertNotIn("CounterBenchmark.codahaleIncNoLabels", hb_pre) |
| 88 | + self.assertNotIn("TextFormatUtilBenchmark.prometheusWriteToNull", hb_pre) |
| 89 | + |
| 90 | + # Verify TextFormatUtilBenchmark contains only its line |
| 91 | + tf_pre = self._read_pre_contents(self.files["TextFormatUtilBenchmark"]) |
| 92 | + self.assertIn("TextFormatUtilBenchmark.prometheusWriteToNull", tf_pre) |
| 93 | + self.assertNotIn("CounterBenchmark.prometheusInc", tf_pre) |
| 94 | + self.assertNotIn("HistogramBenchmark.prometheusNative", tf_pre) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + unittest.main() |
0 commit comments