plot_parametrized.py
This file
Path
scripts/plot_parametrized.py
103 lines · 2.8 KiB · Python
Last changed by
[update patch]William Charginbefore adoptiona8a75a3
History of this fileTake it
scripts: modify shebang to use "/usr/bin/env python"muxatorbefore adoption5a80a23
line 1why, one step back1#!/usr/bin/env pythonAdd plot_parametrized.py scriptsharkdpbefore adoption0d22a96
lines 2–8why, one step back23"""This program shows parametrized `hyperfine` benchmark results as an4errorbar plot."""56import argparse7import json8import matplotlib.pyplot as plt[update patch]William Charginbefore adoptiona8a75a3
line 9why, one step back9import sysAdd plot_parametrized.py scriptsharkdpbefore adoption0d22a96
lines 10–12why, one step back1011parser = argparse.ArgumentParser(description=__doc__)12parser.add_argument("file", help="JSON file with benchmark results", nargs="+")Auto-format Python scriptssharkdpbefore adoption8fff864
lines 13–16why, one step back13parser.add_argument(14 "--parameter-name",15 metavar="name",16 type=str,[update patch]William Charginbefore adoptiona8a75a3
line 17why, one step back17 help="Deprecated; parameter names are now inferred from benchmark files",Auto-format Python scriptssharkdpbefore adoption8fff864
line 18why, one step back18)Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
lines 19–24why, one step back19parser.add_argument(20 "--log-x", help="Use a logarithmic x (parameter) axis", action="store_true"21)22parser.add_argument(23 "--log-time", help="Use a logarithmic time axis", action="store_true"24)Add --titles optionsharkdpbefore adoptionca81ab2
lines 25–27why, one step back25parser.add_argument(26 "--titles", help="Comma-separated list of titles for the plot legend"27)Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
line 28why, one step back28Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
line 29why, one step back29args = parser.parse_args()[update patch]William Charginbefore adoptiona8a75a3
lines 30–34why, one step back30if args.parameter_name is not None:31 sys.stderr.write(32 "warning: --parameter-name is deprecated; names are inferred from "33 "benchmark results\n"34 )Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
line 35why, one step back35[update patch]William Charginbefore adoptiona8a75a3
lines 36–70why, one step back3637def die(msg):38 sys.stderr.write("fatal: %s\n" % (msg,))39 sys.exit(1)404142def extract_parameters(results):43 """Return `(parameter_name: str, parameter_values: List[float])`."""44 if not results:45 die("no benchmark data to plot")46 (names, values) = zip(*(unique_parameter(b) for b in results))47 names = frozenset(names)48 if len(names) != 1:49 die(50 "benchmarks must all have the same parameter name, but found: %s"51 % sorted(names)52 )53 return (next(iter(names)), values)545556def unique_parameter(benchmark):57 """Return the unique parameter `(name: str, value: float)`, or dies."""58 params_dict = benchmark.get("parameters", {})59 if not params_dict:60 die("benchmarks must have exactly one parameter, but found none")61 if len(params_dict) > 1:62 die(63 "benchmarks must have exactly one parameter, but found multiple: %s"64 % sorted(params_dict)65 )66 return next(iter(params_dict.items()))676869parameter_name = None70Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
lines 71–74why, one step back71for filename in args.file:72 with open(filename) as f:73 results = json.load(f)["results"]74[update patch]William Charginbefore adoptiona8a75a3
lines 75–83why, one step back75 (this_parameter_name, parameter_values) = extract_parameters(results)76 if parameter_name is not None and this_parameter_name != parameter_name:77 die(78 "files must all have the same parameter name, but found %r vs. %r"79 % (parameter_name, this_parameter_name)80 )81 parameter_name = this_parameter_name8283 parameter_values = [float(pv) for pv in parameter_values]Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
lines 84–86why, one step back84 times_mean = [b["mean"] for b in results]85 times_stddev = [b["stddev"] for b in results]86Auto-format Python scriptssharkdpbefore adoption8fff864
line 87why, one step back87 plt.errorbar(x=parameter_values, y=times_mean, yerr=times_stddev, capsize=2)Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
line 88why, one step back88[update patch]William Charginbefore adoptiona8a75a3
line 89why, one step back89plt.xlabel(parameter_name)Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
line 90why, one step back90plt.ylabel("Time [s]")Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
lines 91–98why, one step back9192if args.log_time:93 plt.yscale("log")94else:95 plt.ylim(0, None)9697if args.log_x:98 plt.xscale("log")Add --titles optionsharkdpbefore adoptionca81ab2
lines 99–101why, one step back99100if args.titles:101 plt.legend(args.titles.split(","))Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
line 102why, one step back102Add plot_parametrized.py scriptsharkdpbefore adoption0d22a96
line 103why, one step back103plt.show()