|
1 | 1 | import argparse |
2 | 2 | import json |
| 3 | +import os |
3 | 4 | import pathlib |
4 | 5 | import statistics |
5 | 6 |
|
|
19 | 20 | def main(): |
20 | 21 | parser = argparse.ArgumentParser() |
21 | 22 | parser.add_argument("--criterion-root", type=pathlib.Path, required=True) |
22 | | - parser.add_argument("--plot-output", type=pathlib.Path, required=True) |
23 | | - parser.add_argument("--readme", type=pathlib.Path, required=True) |
24 | | - parser.add_argument("--update", action="store_true", default=False) |
| 23 | + parser.add_argument( |
| 24 | + "--plot-output", type=pathlib.Path, default=pathlib.Path("timings.png") |
| 25 | + ) |
| 26 | + parser.add_argument("--update", type=pathlib.Path) |
| 27 | + parser.add_argument("--update-github-summary", action="store_true", default=False) |
25 | 28 | analyze_benchmark(parser.parse_args()) |
26 | 29 |
|
27 | 30 |
|
28 | 31 | def analyze_benchmark(args): |
29 | 32 | root = resolve_path(args.criterion_root) |
30 | | - readme = resolve_path(args.readme) |
| 33 | + update = resolve_path(args.update) if args.update else None |
31 | 34 | plot_output = resolve_path(args.plot_output) |
32 | 35 |
|
33 | 36 | mean_times = load_times(root) |
34 | | - print(format_benchmark(mean_times, ignore_groups=README_BENCHMARK_IGNORE_GROUPS)) |
| 37 | + benchmark = format_benchmark( |
| 38 | + mean_times, |
| 39 | + ignore_groups=README_BENCHMARK_IGNORE_GROUPS, |
| 40 | + ) |
35 | 41 |
|
36 | | - if args.update: |
37 | | - update_readme( |
38 | | - readme, |
39 | | - mean_times, |
40 | | - ignore_groups=README_BENCHMARK_IGNORE_GROUPS, |
41 | | - ) |
42 | | - plot_times( |
43 | | - mean_times, |
44 | | - benchmark_baseline=BENCHMARK_BASELINE, |
45 | | - ignore_groups=README_BENCHMARK_IGNORE_GROUPS, |
46 | | - output=plot_output, |
47 | | - ) |
| 42 | + print(benchmark) |
| 43 | + |
| 44 | + if update is not None: |
| 45 | + update_marked_output(update, benchmark) |
| 46 | + |
| 47 | + if args.update_github_summary: |
| 48 | + update_github_summary(benchmark) |
| 49 | + |
| 50 | + plot_times( |
| 51 | + mean_times, |
| 52 | + benchmark_baseline=BENCHMARK_BASELINE, |
| 53 | + ignore_groups=README_BENCHMARK_IGNORE_GROUPS, |
| 54 | + output=plot_output, |
| 55 | + ) |
48 | 56 |
|
49 | 57 |
|
50 | 58 | def resolve_path(path): |
@@ -131,21 +139,35 @@ def _parts(): |
131 | 139 | return "\n".join(_parts()) |
132 | 140 |
|
133 | 141 |
|
134 | | -def update_readme(readme, mean_times, ignore_groups=()): |
135 | | - print("Update readme") |
136 | | - with open(readme, "rt", encoding="utf8") as fobj: |
| 142 | +def update_marked_output(output, content): |
| 143 | + print(f"Update markers in {output}") |
| 144 | + with open(output, "rt", encoding="utf8") as fobj: |
137 | 145 | lines = [line.rstrip() for line in fobj] |
138 | 146 |
|
139 | | - with open(readme, "wt", encoding="utf8", newline="\n") as fobj: |
| 147 | + with open(output, "wt", encoding="utf8", newline="\n") as fobj: |
140 | 148 | for line in replace_marked_section( |
141 | 149 | lines, |
142 | 150 | start_marker="<!-- start:benchmarks -->", |
143 | 151 | end_marker="<!-- end:benchmarks -->", |
144 | | - content=format_benchmark(mean_times, ignore_groups=ignore_groups), |
| 152 | + content=content, |
145 | 153 | ): |
146 | 154 | print(line, file=fobj) |
147 | 155 |
|
148 | 156 |
|
| 157 | +def update_github_summary(content): |
| 158 | + path = os.environ.get("GITHUB_STEP_SUMMARY") |
| 159 | + if path is None: |
| 160 | + return |
| 161 | + |
| 162 | + append_output(pathlib.Path(path), content) |
| 163 | + |
| 164 | + |
| 165 | +def append_output(path, content): |
| 166 | + print(f"Append summary to {path}") |
| 167 | + with open(path, "at", encoding="utf8", newline="\n") as fobj: |
| 168 | + print(content, file=fobj) |
| 169 | + |
| 170 | + |
149 | 171 | def replace_marked_section(lines, *, start_marker, end_marker, content): |
150 | 172 | start = None |
151 | 173 | end = None |
|
0 commit comments