Debian Codegraph @

benchmarks/gen_md_tables.py

raw
1 """
2 This script generates the markdown tables for the benchmark run.
3 """
4
5 import argparse
6 import dataclasses
7 import json
8 import pathlib
9 import re
10 import sys
11
12
13 def output_md_tables(in_file: pathlib.Path) -> None:
14 """
15 Output the markdown tables for the benchmark.
16 """
17 run_info = json.loads(in_file.read_text())
18
19 python_ver = run_info['machine_info']['python_version']
20 impl_ver = run_info['machine_info']['python_implementation_version']
21 python = "{python} {version}".format(
22 python=run_info['machine_info']['python_implementation'],
23 version=python_ver,
24 )
25 if impl_ver != python_ver:
26 python += f" ({impl_ver})"
27
28 machine = run_info['machine_info']['cpu']['brand_raw']
29
30 table_to_times: dict[TableKey, dict[int, dict[str, float]]] = {}
31 for benchmark in run_info['benchmarks']:
32 ops = benchmark['stats']['ops']
33 method = benchmark['group'].split(':')[0]
34 file_count = re.search('\\b(\\S+) files\\b', benchmark['group']).group(1)
35 line_count = int(re.search('\\b(\\S+) lines?\\b', benchmark['group']).group(1))
36 backend = re.search('^bench_([a-z0-9]+)_', benchmark['name']).group(1)
37
38 table_key = TableKey(method=method, file_count=file_count)
39 table_to_times.setdefault(table_key, {}).setdefault(line_count, {})[backend] = ops
40
41 print()
42 print(f"{python} on {machine}")
43 print("----------")
44 print()
45 for table_key, table_rows in table_to_times.items():
46 print(f"{table_key.method}: {table_key.file_count} files ")
47 print()
48 print("| Patterns | simple<br>ops | hyperscan<br>ops | <br>x | re2<br>ops | <br>x |")
49 print("| --: | --: | --: | --: | --: | --: |")
50
51 for line_count, backend_ops in sorted(table_rows.items()):
52 sm_ops = backend_ops['sm']
53 hs_ops = backend_ops.get('hs')
54 re2_ops = backend_ops.get('re2')
55 print("| " + " | ".join([
56 str(line_count),
57 format(sm_ops, '.1f'),
58 format(hs_ops, '.1f') if hs_ops else "-",
59 format(hs_ops / sm_ops, '.2f') if hs_ops else "-",
60 format(re2_ops, '.1f') if re2_ops else "-",
61 format(re2_ops / sm_ops, '.2f') if re2_ops else "-",
62 ]) + " |")
63
64 print()
65
66
67 def main() -> int:
68 """
69 Run the script.
70 """
71 # Parse command-line arguments.
72 parser = argparse.ArgumentParser(description=__doc__)
73 parser.add_argument('-i', type=pathlib.Path, required=True, help=(
74 "The saved benchmark run."
75 ), metavar="<json>")
76 args = parser.parse_args()
77
78 output_md_tables(args.i)
79
80 return 0
81
82
83 @dataclasses.dataclass(frozen=True)
84 class TableKey(object):
85 file_count: str
86 method: str
87
88
89 if __name__ == '__main__':
90 sys.exit(main())