plot_progression.py
This file
Path
scripts/plot_progression.py
90 lines · 2.3 KiB · Python
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake 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 argparse1718import matplotlib.pyplot as plt19import numpy as np2021from plot_utils import METRICS, add_metric_argument, load_results222324def moving_average(values, num_runs):25 values_padded = np.pad(26 values, (num_runs // 2, num_runs - 1 - num_runs // 2), mode="edge"27 )28 kernel = np.ones(num_runs) / num_runs29 return np.convolve(values_padded, kernel, mode="valid")303132parser = argparse.ArgumentParser(description=__doc__)33add_metric_argument(parser)34parser.add_argument("file", help="JSON file with benchmark results")35parser.add_argument("--title", help="Plot Title")36parser.add_argument("-o", "--output", help="Save image to the given filename.")37parser.add_argument(38 "-w",39 "--moving-average-width",40 type=int,41 metavar="num_runs",42 help="Width of the moving-average window (default: N/5)",43)44parser.add_argument(45 "--no-moving-average",46 action="store_true",47 help="Do not show moving average curve",48)495051args = parser.parse_args()52metric, [results] = load_results(parser, [args.file], args.metric)53metric_info = METRICS[metric]5455for result in results:56 label = result.get("name", result["command"])57 values = [m[metric]["value"] / metric_info.scale for m in result["measurements"]]58 num = len(values)59 nums = range(num)6061 plt.scatter(x=nums, y=values, marker=".")62 plt.ylim([0, None])63 plt.xlim([-1, num])6465 if not args.no_moving_average:66 moving_average_width = (67 max(1, num // 5)68 if args.moving_average_width is None69 else args.moving_average_width70 )7172 average = moving_average(values, moving_average_width)73 plt.plot(nums, average, "-")7475if args.title:76 plt.title(args.title)7778legend = []79for result in results:80 legend.append(result.get("name", result["command"]))81 if not args.no_moving_average:82 legend.append("moving average")83plt.legend(legend)8485plt.ylabel(metric_info.axis_label)8687if args.output:88 plt.savefig(args.output)89else:90 plt.show()