Skip to content
Myr

plot_whisker.py

This file

Path

scripts/plot_whisker.py

69 lines · 2.2 KiB · Python

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

History of this file

Take it

1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5#     "matplotlib",6#     "pyqt6",7# ]8# ///910"""This program shows `hyperfine` benchmark results as a box and whisker plot.1112Quoting from the matplotlib documentation:13    The box extends from the lower to upper quartile values of the data, with14    a line at the median. The whiskers extend from the box to show the range15    of the data. Flier points are those past the end of the whiskers.16"""1718import argparse1920import matplotlib.pyplot as plt2122from plot_utils import METRICS, add_metric_argument, load_results2324parser = argparse.ArgumentParser(description=__doc__)25add_metric_argument(parser)26parser.add_argument("file", help="JSON file with benchmark results")27parser.add_argument("--title", help="Plot Title")28parser.add_argument("--sort-by", choices=["median"], help="Sort method")29parser.add_argument(30    "--labels", help="Comma-separated list of entries for the plot legend"31)32parser.add_argument("-o", "--output", help="Save image to the given filename.")3334args = parser.parse_args()35metric, [results] = load_results(parser, [args.file], args.metric)36metric_info = METRICS[metric]3738if args.labels:39    labels = args.labels.split(",")40else:41    labels = [b.get("name", b["command"]) for b in results]42values = [43    [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in results44]4546if args.sort_by == "median":47    medians = [b["summary"][metric]["median"] for b in results]48    indices = sorted(range(len(labels)), key=lambda k: medians[k])49    labels = [labels[i] for i in indices]50    values = [values[i] for i in indices]5152plt.figure(figsize=(10, 6), constrained_layout=True)53boxplot = plt.boxplot(values, vert=True, patch_artist=True)54cmap = plt.get_cmap("rainbow")55colors = [cmap(val / len(values)) for val in range(len(values))]5657for patch, color in zip(boxplot["boxes"], colors):58    patch.set_facecolor(color)5960if args.title:61    plt.title(args.title)62plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")63plt.ylabel(metric_info.axis_label)64plt.ylim(0, None)65plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)66if args.output:67    plt.savefig(args.output)68else:69    plt.show()