2020-10-08 23:51:31 +03:00
|
|
|
#!/usr/bin/env python
|
2019-11-25 21:32:45 +03:00
|
|
|
|
|
|
|
"""This program shows parametrized `hyperfine` benchmark results as an
|
|
|
|
errorbar plot."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import matplotlib.pyplot as plt
|
2020-09-17 03:16:07 +03:00
|
|
|
import sys
|
2019-11-25 21:32:45 +03:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
parser.add_argument("file", help="JSON file with benchmark results", nargs="+")
|
2020-04-01 10:35:06 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"--parameter-name",
|
|
|
|
metavar="name",
|
|
|
|
type=str,
|
2020-09-17 03:16:07 +03:00
|
|
|
help="Deprecated; parameter names are now inferred from benchmark files",
|
2020-04-01 10:35:06 +03:00
|
|
|
)
|
2020-04-13 09:19:58 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"--log-x", help="Use a logarithmic x (parameter) axis", action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--log-time", help="Use a logarithmic time axis", action="store_true"
|
|
|
|
)
|
2020-04-13 09:28:29 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"--titles", help="Comma-separated list of titles for the plot legend"
|
|
|
|
)
|
2021-02-16 10:02:11 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--output", help="Save image to the given filename."
|
|
|
|
)
|
2020-04-13 09:19:58 +03:00
|
|
|
|
2019-11-25 21:32:45 +03:00
|
|
|
args = parser.parse_args()
|
2020-09-17 03:16:07 +03:00
|
|
|
if args.parameter_name is not None:
|
|
|
|
sys.stderr.write(
|
|
|
|
"warning: --parameter-name is deprecated; names are inferred from "
|
|
|
|
"benchmark results\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def die(msg):
|
|
|
|
sys.stderr.write("fatal: %s\n" % (msg,))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def extract_parameters(results):
|
|
|
|
"""Return `(parameter_name: str, parameter_values: List[float])`."""
|
|
|
|
if not results:
|
|
|
|
die("no benchmark data to plot")
|
|
|
|
(names, values) = zip(*(unique_parameter(b) for b in results))
|
|
|
|
names = frozenset(names)
|
|
|
|
if len(names) != 1:
|
|
|
|
die(
|
|
|
|
"benchmarks must all have the same parameter name, but found: %s"
|
|
|
|
% sorted(names)
|
|
|
|
)
|
2020-09-17 21:31:27 +03:00
|
|
|
return (next(iter(names)), list(values))
|
2020-09-17 03:16:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
def unique_parameter(benchmark):
|
2020-09-17 21:31:27 +03:00
|
|
|
"""Return the unique parameter `(name: str, value: float)`, or die."""
|
2020-09-17 03:16:07 +03:00
|
|
|
params_dict = benchmark.get("parameters", {})
|
|
|
|
if not params_dict:
|
|
|
|
die("benchmarks must have exactly one parameter, but found none")
|
|
|
|
if len(params_dict) > 1:
|
|
|
|
die(
|
|
|
|
"benchmarks must have exactly one parameter, but found multiple: %s"
|
|
|
|
% sorted(params_dict)
|
|
|
|
)
|
2020-09-17 21:31:27 +03:00
|
|
|
[(name, value)] = params_dict.items()
|
|
|
|
return (name, float(value))
|
2020-09-17 03:16:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
parameter_name = None
|
2019-11-25 21:32:45 +03:00
|
|
|
|
|
|
|
for filename in args.file:
|
|
|
|
with open(filename) as f:
|
|
|
|
results = json.load(f)["results"]
|
|
|
|
|
2020-09-17 03:16:07 +03:00
|
|
|
(this_parameter_name, parameter_values) = extract_parameters(results)
|
|
|
|
if parameter_name is not None and this_parameter_name != parameter_name:
|
|
|
|
die(
|
|
|
|
"files must all have the same parameter name, but found %r vs. %r"
|
|
|
|
% (parameter_name, this_parameter_name)
|
|
|
|
)
|
|
|
|
parameter_name = this_parameter_name
|
|
|
|
|
2019-11-25 21:32:45 +03:00
|
|
|
times_mean = [b["mean"] for b in results]
|
|
|
|
times_stddev = [b["stddev"] for b in results]
|
|
|
|
|
2020-04-01 10:35:06 +03:00
|
|
|
plt.errorbar(x=parameter_values, y=times_mean, yerr=times_stddev, capsize=2)
|
2019-11-25 21:32:45 +03:00
|
|
|
|
2020-09-17 03:16:07 +03:00
|
|
|
plt.xlabel(parameter_name)
|
2019-11-25 21:32:45 +03:00
|
|
|
plt.ylabel("Time [s]")
|
2020-04-13 09:19:58 +03:00
|
|
|
|
|
|
|
if args.log_time:
|
|
|
|
plt.yscale("log")
|
|
|
|
else:
|
|
|
|
plt.ylim(0, None)
|
|
|
|
|
|
|
|
if args.log_x:
|
|
|
|
plt.xscale("log")
|
|
|
|
|
2020-04-13 09:28:29 +03:00
|
|
|
if args.titles:
|
|
|
|
plt.legend(args.titles.split(","))
|
|
|
|
|
2021-02-16 10:02:11 +03:00
|
|
|
if args.output:
|
|
|
|
plt.savefig(args.output)
|
2020-11-17 11:00:20 +03:00
|
|
|
else:
|
|
|
|
plt.show()
|