Skip to content
Myr

plot_progression.py

This file

Path

scripts/plot_progression.py

86 lines · 2.0 KiB · Python

Last changed by

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

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 json18import matplotlib.pyplot as plt19import numpy as np202122def moving_average(times, num_runs):23    times_padded = np.pad(24        times, (num_runs // 2, num_runs - 1 - num_runs // 2), mode="edge"25    )26    kernel = np.ones(num_runs) / num_runs27    return np.convolve(times_padded, kernel, mode="valid")282930parser = argparse.ArgumentParser(description=__doc__)31parser.add_argument("file", help="JSON file with benchmark results")32parser.add_argument("--title", help="Plot Title")33parser.add_argument("-o", "--output", help="Save image to the given filename.")34parser.add_argument(35    "-w",36    "--moving-average-width",37    type=int,38    metavar="num_runs",39    help="Width of the moving-average window (default: N/5)",40)41parser.add_argument(42    "--no-moving-average",43    action="store_true",44    help="Do not show moving average curve",45)464748args = parser.parse_args()4950with open(args.file) as f:51    results = json.load(f)["results"]5253for result in results:54    label = result["command"]55    times = result["times"]56    num = len(times)57    nums = range(num)5859    plt.scatter(x=nums, y=times, marker=".")60    plt.ylim([0, None])61    plt.xlim([-1, num])6263    if not args.no_moving_average:64        moving_average_width = (65            num // 5 if args.moving_average_width is None else args.moving_average_width66        )6768        average = moving_average(times, moving_average_width)69        plt.plot(nums, average, "-")7071if args.title:72    plt.title(args.title)7374legend = []75for result in results:76    legend.append(result["command"])77    if not args.no_moving_average:78        legend.append("moving average")79plt.legend(legend)8081plt.ylabel("Time [s]")8283if args.output:84    plt.savefig(args.output)85else:86    plt.show()