Files
hyperfine/scripts/plot_whisker.py
T

73 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2024-08-28 16:52:02 +02:00
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "matplotlib",
# "pyqt6",
# ]
# ///
"""This program shows `hyperfine` benchmark results as a box and whisker plot.
Quoting from the matplotlib documentation:
The box extends from the lower to upper quartile values of the data, with
a line at the median. The whiskers extend from the box to show the range
of the data. Flier points are those past the end of the whiskers.
"""
2018-12-12 19:54:57 +01:00
import argparse
import json
2024-08-28 17:01:09 +02:00
2018-12-12 19:54:57 +01:00
import matplotlib.pyplot as plt
from plot_utils import METRICS, add_metric_argument, validate_metric
parser = argparse.ArgumentParser(description=__doc__)
add_metric_argument(parser)
2018-12-12 19:54:57 +01:00
parser.add_argument("file", help="JSON file with benchmark results")
2020-03-20 15:08:26 +02:00
parser.add_argument("--title", help="Plot Title")
2024-08-28 16:41:58 +02:00
parser.add_argument("--sort-by", choices=["median"], help="Sort method")
2020-04-01 09:35:06 +02:00
parser.add_argument(
"--labels", help="Comma-separated list of entries for the plot legend"
)
2024-08-28 16:41:58 +02:00
parser.add_argument("-o", "--output", help="Save image to the given filename.")
2021-02-16 08:02:11 +01:00
2018-12-12 19:54:57 +01:00
args = parser.parse_args()
metric_label, metric_scale = METRICS[args.metric]
2018-12-12 19:54:57 +01:00
2024-08-28 16:41:58 +02:00
with open(args.file, encoding="utf-8") as f:
2018-12-12 19:54:57 +01:00
results = json.load(f)["results"]
validate_metric(parser, results, args.metric)
2018-12-12 19:54:57 +01:00
2020-04-01 09:31:55 +02:00
if args.labels:
labels = args.labels.split(",")
else:
2026-10-05 18:11:10 +02:00
labels = [b.get("name", b["command"]) for b in results]
values = [
[m[args.metric]["value"] / metric_scale for m in b["measurements"]] for b in results
]
2024-03-02 16:32:39 +01:00
2024-08-28 16:41:58 +02:00
if args.sort_by == "median":
medians = [b["summary"][args.metric]["median"] for b in results]
2024-03-13 21:33:15 +01:00
indices = sorted(range(len(labels)), key=lambda k: medians[k])
labels = [labels[i] for i in indices]
values = [values[i] for i in indices]
2018-12-12 19:54:57 +01:00
2024-03-13 21:26:57 +01:00
plt.figure(figsize=(10, 6), constrained_layout=True)
boxplot = plt.boxplot(values, vert=True, patch_artist=True)
2023-04-16 16:06:47 -04:00
cmap = plt.get_cmap("rainbow")
colors = [cmap(val / len(values)) for val in range(len(values))]
2020-03-20 15:08:26 +02:00
for patch, color in zip(boxplot["boxes"], colors):
patch.set_facecolor(color)
2020-11-03 13:46:16 +01:00
if args.title:
plt.title(args.title)
2020-04-01 09:31:55 +02:00
plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")
plt.ylabel(metric_label)
2020-03-21 09:01:43 +02:00
plt.ylim(0, None)
2024-08-28 16:41:58 +02:00
plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)
2021-02-16 08:02:11 +01:00
if args.output:
plt.savefig(args.output)
2020-11-17 09:00:20 +01:00
else:
plt.show()