Skip to content
Myr

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 file

Take it

1#!/usr/bin/env python23"""This program shows parametrized `hyperfine` benchmark results as an4errorbar plot."""56import argparse7import json8import matplotlib.pyplot as plt9import sys1011parser = 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,17    help="Deprecated; parameter names are now inferred from benchmark files",18)19parser.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)25parser.add_argument(26    "--titles", help="Comma-separated list of titles for the plot legend"27)2829args = parser.parse_args()30if 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    )353637def 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 = None7071for filename in args.file:72    with open(filename) as f:73        results = json.load(f)["results"]7475    (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]84    times_mean = [b["mean"] for b in results]85    times_stddev = [b["stddev"] for b in results]8687    plt.errorbar(x=parameter_values, y=times_mean, yerr=times_stddev, capsize=2)8889plt.xlabel(parameter_name)90plt.ylabel("Time [s]")9192if args.log_time:93    plt.yscale("log")94else:95    plt.ylim(0, None)9697if args.log_x:98    plt.xscale("log")99100if args.titles:101    plt.legend(args.titles.split(","))102103plt.show()