Skip to content
Myr

plot_progression.py

This file

Path

scripts/plot_progression.py

94 lines · 2.4 KiB · Python

Last changed by

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

History of this file

Take it

1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5#     "pyqt6",6#     "matplotlib",7#     "numpy",8# ]9# ///1011"""This program shows `hyperfine` benchmark results in a sequential way12in order to debug possible background interference, caching effects,13thermal throttling and similar effects.14"""1516import argparse17import json1819import matplotlib.pyplot as plt20import numpy as np2122from plot_utils import METRICS, add_metric_argument, validate_metric232425def moving_average(values, num_runs):26    values_padded = np.pad(27        values, (num_runs // 2, num_runs - 1 - num_runs // 2), mode="edge"28    )29    kernel = np.ones(num_runs) / num_runs30    return np.convolve(values_padded, kernel, mode="valid")313233parser = argparse.ArgumentParser(description=__doc__)34add_metric_argument(parser)35parser.add_argument("file", help="JSON file with benchmark results")36parser.add_argument("--title", help="Plot Title")37parser.add_argument("-o", "--output", help="Save image to the given filename.")38parser.add_argument(39    "-w",40    "--moving-average-width",41    type=int,42    metavar="num_runs",43    help="Width of the moving-average window (default: N/5)",44)45parser.add_argument(46    "--no-moving-average",47    action="store_true",48    help="Do not show moving average curve",49)505152args = parser.parse_args()53metric_label, metric_scale = METRICS[args.metric]5455with open(args.file) as f:56    results = json.load(f)["results"]57validate_metric(parser, results, args.metric)5859for result in results:60    label = result.get("name", result["command"])61    values = [m[args.metric]["value"] / metric_scale for m in result["measurements"]]62    num = len(values)63    nums = range(num)6465    plt.scatter(x=nums, y=values, marker=".")66    plt.ylim([0, None])67    plt.xlim([-1, num])6869    if not args.no_moving_average:70        moving_average_width = (71            max(1, num // 5)72            if args.moving_average_width is None73            else args.moving_average_width74        )7576        average = moving_average(values, moving_average_width)77        plt.plot(nums, average, "-")7879if args.title:80    plt.title(args.title)8182legend = []83for result in results:84    legend.append(result.get("name", result["command"]))85    if not args.no_moving_average:86        legend.append("moving average")87plt.legend(legend)8889plt.ylabel(metric_label)9091if args.output:92    plt.savefig(args.output)93else:94    plt.show()