Skip to content
Myr

plot_histogram.py

This file

Path

scripts/plot_histogram.py

128 lines · 2.9 KiB · Python

Last changed by

Support hardware counters in plotting scripts (#984)David Peterbefore adoptione262cd6

History of this file

Take it

1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5#     "matplotlib",6#     "pyqt6",7#     "numpy",8# ]9# ///1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse14import json1516import matplotlib.pyplot as plt17import numpy as np1819from plot_utils import METRICS, add_metric_argument, validate_metric2021parser = argparse.ArgumentParser(description=__doc__)22add_metric_argument(parser)23parser.add_argument("file", help="JSON file with benchmark results")24parser.add_argument("--title", help="Plot title")25parser.add_argument(26    "--labels", help="Comma-separated list of entries for the plot legend"27)28parser.add_argument("--bins", help="Number of bins (default: auto)")29parser.add_argument(30    "--legend-location",31    help="Location of the legend on plot (default: upper center)",32    choices=[33        "upper center",34        "lower center",35        "right",36        "left",37        "best",38        "upper left",39        "upper right",40        "lower left",41        "lower right",42        "center left",43        "center right",44        "center",45    ],46    default="upper center",47)48parser.add_argument(49    "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"50)51parser.add_argument("-o", "--output", help="Save image to the given filename.")52parser.add_argument(53    "--min",54    "--t-min",55    dest="value_min",56    type=float,57    help="Minimum metric value to display",58)59parser.add_argument(60    "--max",61    "--t-max",62    dest="value_max",63    type=float,64    help="Maximum metric value to display",65)66parser.add_argument(67    "--log-count",68    help="Use a logarithmic y-axis for the event count",69    action="store_true",70)7172args = parser.parse_args()73metric_label, metric_scale = METRICS[args.metric]7475with open(args.file) as f:76    results = json.load(f)["results"]77validate_metric(parser, results, args.metric)7879if args.labels:80    labels = args.labels.split(",")81else:82    labels = [b.get("name", b["command"]) for b in results]83all_values = [84    [m[args.metric]["value"] / metric_scale for m in b["measurements"]] for b in results85]8687value_min = (88    args.value_min89    if args.value_min is not None90    else np.min(list(map(np.min, all_values)))91)92value_max = (93    args.value_max94    if args.value_max is not None95    else np.max(list(map(np.max, all_values)))96)9798bins = int(args.bins) if args.bins else "auto"99histtype = args.type if args.type else "bar"100101plt.figure(figsize=(10, 5))102plt.hist(103    all_values,104    label=labels,105    bins=bins,106    histtype=histtype,107    range=(value_min, value_max),108)109plt.legend(110    loc=args.legend_location,111    fancybox=True,112    shadow=True,113    prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},114)115116plt.xlabel(metric_label)117if args.title:118    plt.title(args.title)119120if args.log_count:121    plt.yscale("log")122else:123    plt.ylim(0, None)124125if args.output:126    plt.savefig(args.output, dpi=600)127else:128    plt.show()