plot_parametrized.py
This file
Path
scripts/plot_parametrized.py
109 lines · 3.0 KiB · Python
Last changed by
Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b
History of this fileTake it
The walk stopped after 200 commits: lines older than 8fff864 point at it.
scripts: modify shebang to use "/usr/bin/env python"muxatorbefore adoption5a80a23
line 1why, one step back1#!/usr/bin/env pythonAuto-format Python scriptssharkdpbefore adoption8fff864
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 sysAuto-format Python scriptssharkdpbefore adoption8fff864
lines 10–16why, one step back1011parser = argparse.ArgumentParser(description=__doc__)12parser.add_argument("file", help="JSON file with benchmark results", nargs="+")13parser.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)Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b
lines 28–30why, one step back28parser.add_argument(29 "-o", "--output", help="Save image to the given filename."30)Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
line 31why, one step back31Auto-format Python scriptssharkdpbefore adoption8fff864
line 32why, one step back32args = parser.parse_args()[update patch]William Charginbefore adoptiona8a75a3
lines 33–37why, one step back33if args.parameter_name is not None:34 sys.stderr.write(35 "warning: --parameter-name is deprecated; names are inferred from "36 "benchmark results\n"37 )Auto-format Python scriptssharkdpbefore adoption8fff864
line 38why, one step back38[update patch]William Charginbefore adoptiona8a75a3
lines 39–55why, one step back3940def die(msg):41 sys.stderr.write("fatal: %s\n" % (msg,))42 sys.exit(1)434445def extract_parameters(results):46 """Return `(parameter_name: str, parameter_values: List[float])`."""47 if not results:48 die("no benchmark data to plot")49 (names, values) = zip(*(unique_parameter(b) for b in results))50 names = frozenset(names)51 if len(names) != 1:52 die(53 "benchmarks must all have the same parameter name, but found: %s"54 % sorted(names)55 )[update patch]William Charginbefore adoption8973778
line 56why, one step back56 return (next(iter(names)), list(values))[update patch]William Charginbefore adoptiona8a75a3
lines 57–59why, one step back575859def unique_parameter(benchmark):[update patch]William Charginbefore adoption8973778
line 60why, one step back60 """Return the unique parameter `(name: str, value: float)`, or die."""[update patch]William Charginbefore adoptiona8a75a3
lines 61–68why, one step back61 params_dict = benchmark.get("parameters", {})62 if not params_dict:63 die("benchmarks must have exactly one parameter, but found none")64 if len(params_dict) > 1:65 die(66 "benchmarks must have exactly one parameter, but found multiple: %s"67 % sorted(params_dict)68 )[update patch]William Charginbefore adoption8973778
lines 69–70why, one step back69 [(name, value)] = params_dict.items()70 return (name, float(value))[update patch]William Charginbefore adoptiona8a75a3
lines 71–74why, one step back717273parameter_name = None74Auto-format Python scriptssharkdpbefore adoption8fff864
lines 75–78why, one step back75for filename in args.file:76 with open(filename) as f:77 results = json.load(f)["results"]78[update patch]William Charginbefore adoptiona8a75a3
lines 79–86why, one step back79 (this_parameter_name, parameter_values) = extract_parameters(results)80 if parameter_name is not None and this_parameter_name != parameter_name:81 die(82 "files must all have the same parameter name, but found %r vs. %r"83 % (parameter_name, this_parameter_name)84 )85 parameter_name = this_parameter_name86Auto-format Python scriptssharkdpbefore adoption8fff864
lines 87–91why, one step back87 times_mean = [b["mean"] for b in results]88 times_stddev = [b["stddev"] for b in results]8990 plt.errorbar(x=parameter_values, y=times_mean, yerr=times_stddev, capsize=2)91[update patch]William Charginbefore adoptiona8a75a3
line 92why, one step back92plt.xlabel(parameter_name)Auto-format Python scriptssharkdpbefore adoption8fff864
line 93why, one step back93plt.ylabel("Time [s]")Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
lines 94–101why, one step back9495if args.log_time:96 plt.yscale("log")97else:98 plt.ylim(0, None)99100if args.log_x:101 plt.xscale("log")Add --titles optionsharkdpbefore adoptionca81ab2
lines 102–104why, one step back102103if args.titles:104 plt.legend(args.titles.split(","))Add --log-time and --log-x flagssharkdpbefore adoptionc649d8d
line 105why, one step back105Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b
lines 106–107why, one step back106if args.output:107 plt.savefig(args.output)Add parameters to plotting scriptsBenjamin Rosemannbefore adoptionb04a9ec
lines 108–109why, one step back108else:109 plt.show()