Files
hyperfine/scripts/plot_progression.py
T

94 lines
2.4 KiB
Python
Raw Normal View History

2022-03-05 12:03:29 +01:00
#!/usr/bin/env python
2024-08-28 16:52:02 +02:00
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "pyqt6",
# "matplotlib",
# "numpy",
# ]
# ///
2022-03-05 12:03:29 +01:00
"""This program shows `hyperfine` benchmark results in a sequential way
in order to debug possible background interference, caching effects,
thermal throttling and similar effects.
"""
import argparse
import json
2024-08-28 17:01:09 +02:00
2022-03-05 12:03:29 +01:00
import matplotlib.pyplot as plt
import numpy as np
from plot_utils import METRICS, add_metric_argument, validate_metric
2022-03-05 12:03:29 +01:00
def moving_average(values, num_runs):
values_padded = np.pad(
values, (num_runs // 2, num_runs - 1 - num_runs // 2), mode="edge"
2022-03-05 12:03:29 +01:00
)
kernel = np.ones(num_runs) / num_runs
return np.convolve(values_padded, kernel, mode="valid")
2022-03-05 12:03:29 +01:00
parser = argparse.ArgumentParser(description=__doc__)
add_metric_argument(parser)
2022-03-05 12:03:29 +01:00
parser.add_argument("file", help="JSON file with benchmark results")
parser.add_argument("--title", help="Plot Title")
parser.add_argument("-o", "--output", help="Save image to the given filename.")
parser.add_argument(
"-w",
"--moving-average-width",
type=int,
metavar="num_runs",
help="Width of the moving-average window (default: N/5)",
)
2024-02-02 17:51:31 +01:00
parser.add_argument(
"--no-moving-average",
action="store_true",
help="Do not show moving average curve",
)
2022-03-05 12:03:29 +01:00
args = parser.parse_args()
metric_label, metric_scale = METRICS[args.metric]
2022-03-05 12:03:29 +01:00
with open(args.file) as f:
results = json.load(f)["results"]
validate_metric(parser, results, args.metric)
2022-03-05 12:03:29 +01:00
2024-02-02 17:51:31 +01:00
for result in results:
2026-10-05 18:11:10 +02:00
label = result.get("name", result["command"])
values = [m[args.metric]["value"] / metric_scale for m in result["measurements"]]
num = len(values)
2024-02-02 17:51:31 +01:00
nums = range(num)
2022-03-05 12:03:29 +01:00
plt.scatter(x=nums, y=values, marker=".")
2024-02-02 17:51:31 +01:00
plt.ylim([0, None])
plt.xlim([-1, num])
2022-03-05 12:03:29 +01:00
2024-02-02 17:51:31 +01:00
if not args.no_moving_average:
moving_average_width = (
max(1, num // 5)
if args.moving_average_width is None
else args.moving_average_width
2024-02-02 17:51:31 +01:00
)
2022-03-05 12:03:29 +01:00
average = moving_average(values, moving_average_width)
2024-02-02 17:51:31 +01:00
plt.plot(nums, average, "-")
2022-03-05 12:03:29 +01:00
if args.title:
plt.title(args.title)
2024-02-02 17:51:31 +01:00
legend = []
for result in results:
2026-10-05 18:11:10 +02:00
legend.append(result.get("name", result["command"]))
2024-02-02 17:51:31 +01:00
if not args.no_moving_average:
legend.append("moving average")
plt.legend(legend)
plt.ylabel(metric_label)
2024-02-02 17:51:31 +01:00
2022-03-05 12:03:29 +01:00
if args.output:
plt.savefig(args.output)
else:
plt.show()