plot_benchmark_comparison.py
This file
Path
scripts/plot_benchmark_comparison.py
91 lines · 2.6 KiB · Python
Last changed by
Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
History of this fileTake it
Address commentsMartin Liskabefore adoption3cedcc3
lines 1–21why, one step back1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///1011"""12This script shows `hyperfine` benchmark results as a bar plot grouped by command.13Note all the input files must contain results for all commands.14"""1516import argparse17import pathlib1819import matplotlib.pyplot as plt20import numpy as np21Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 22why, one step back22from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 23why, one step back23Address commentsMartin Liskabefore adoption3cedcc3
line 24why, one step back24Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 25–35why, one step back25def main():26 parser = argparse.ArgumentParser(description=__doc__)27 add_metric_argument(parser)28 parser.add_argument(29 "files", nargs="+", type=pathlib.Path, help="JSON files with benchmark results"30 )31 parser.add_argument("--title", help="Plot Title")32 parser.add_argument(33 "--benchmark-names", nargs="+", help="Names of the benchmark groups"34 )35 parser.add_argument("-o", "--output", help="Save image to the given filename")Address commentsMartin Liskabefore adoption3cedcc3
line 36why, one step back36Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 37–39why, one step back37 args = parser.parse_args()38 metric, datasets = load_results(parser, args.files, args.metric)39 metric_info = METRICS[metric]Address commentsMartin Liskabefore adoption3cedcc3
line 40why, one step back40Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 41–43why, one step back41 commands = None42 data = []43 inputs = []Address commentsMartin Liskabefore adoption3cedcc3
lines 44–45why, one step back4445 if args.benchmark_names:Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 46–48why, one step back46 assert len(args.files) == len(args.benchmark_names), (47 "Number of benchmark names must match the number of input files."48 )Address commentsMartin Liskabefore adoption3cedcc3
line 49why, one step back49Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 50–62why, one step back50 for i, (filename, results) in enumerate(zip(args.files, datasets)):51 benchmark_commands = [b.get("name", b["command"]) for b in results]52 if commands is None:53 commands = benchmark_commands54 else:55 assert commands == benchmark_commands, (56 f"Unexpected commands in {filename}: {benchmark_commands}, expected: {commands}"57 )58 data.append([b["summary"][metric]["mean"] / metric_info.scale for b in results])59 if args.benchmark_names:60 inputs.append(args.benchmark_names[i])61 else:62 inputs.append(filename.stem)Address commentsMartin Liskabefore adoption3cedcc3
line 63why, one step back63Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 64–66why, one step back64 data = np.transpose(data)65 x = np.arange(len(inputs)) # the label locations66 width = 0.25 # the width of the barsAddress commentsMartin Liskabefore adoption3cedcc3
line 67why, one step back67Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 68–73why, one step back68 fig, ax = plt.subplots(layout="constrained")69 fig.set_figheight(5)70 fig.set_figwidth(10)71 for i, command in enumerate(commands):72 offset = width * (i + 1)73 ax.bar(x + offset, data[i], width, label=command)Address commentsMartin Liskabefore adoption3cedcc3
line 74why, one step back74Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 75–76why, one step back75 ax.set_xticks(x + 0.5, inputs)76 ax.grid(visible=True, axis="y")Address commentsMartin Liskabefore adoption3cedcc3
line 77why, one step back77Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529
lines 78–91why, one step back78 if args.title:79 plt.title(args.title)80 plt.xlabel("Benchmark")81 plt.ylabel(metric_info.axis_label)82 plt.legend(title="Command")8384 if args.output:85 plt.savefig(args.output)86 else:87 plt.show()888990if __name__ == "__main__":91 main()