2020-10-08 22:51:31 +02:00
|
|
|
#!/usr/bin/env python
|
2024-08-28 16:52:02 +02:00
|
|
|
# /// script
|
|
|
|
|
# requires-python = ">=3.10"
|
|
|
|
|
# dependencies = [
|
|
|
|
|
# "matplotlib",
|
|
|
|
|
# "pyqt6",
|
|
|
|
|
# ]
|
|
|
|
|
# ///
|
2019-10-07 19:04:33 +02:00
|
|
|
|
|
|
|
|
"""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
|
|
|
|
|
|
2019-10-07 19:04:33 +02:00
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
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()
|
|
|
|
|
|
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"]
|
|
|
|
|
|
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]
|
2026-10-05 16:59:35 +02:00
|
|
|
times = [[m["time_wall_clock"]["value"] 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":
|
2026-10-05 16:59:35 +02:00
|
|
|
medians = [b["summary"]["time_wall_clock"]["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]
|
|
|
|
|
times = [times[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)
|
2020-03-20 15:08:26 +02:00
|
|
|
boxplot = plt.boxplot(times, vert=True, patch_artist=True)
|
2023-04-16 16:06:47 -04:00
|
|
|
cmap = plt.get_cmap("rainbow")
|
2020-03-20 15:08:26 +02:00
|
|
|
colors = [cmap(val / len(times)) for val in range(len(times))]
|
|
|
|
|
|
|
|
|
|
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")
|
2018-12-12 19:54:57 +01:00
|
|
|
plt.ylabel("Time [s]")
|
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()
|