Files
hyperfine/scripts/plot_whisker.py
T

51 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""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
import matplotlib.pyplot as plt
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")
2020-04-01 09:35:06 +02:00
parser.add_argument(
"--labels", help="Comma-separated list of entries for the plot legend"
)
2021-02-16 08:02:11 +01:00
parser.add_argument(
"-o", "--output", help="Save image to the given filename."
)
2018-12-12 19:54:57 +01:00
args = parser.parse_args()
with open(args.file) as f:
results = json.load(f)["results"]
2020-04-01 09:31:55 +02:00
if args.labels:
labels = args.labels.split(",")
else:
labels = [b["command"] for b in results]
2018-12-12 19:54:57 +01:00
times = [b["times"] for b in results]
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)
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()