plot_progression.py
This file
Path
scripts/plot_progression.py
78 lines · 1.9 KiB · Python
Last changed by
Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935
History of this fileTake it
1#!/usr/bin/env python23"""This program shows `hyperfine` benchmark results in a sequential way4in order to debug possible background interference, caching effects,5thermal throttling and similar effects.6"""78import argparse9import json10import matplotlib.pyplot as plt11import numpy as np121314def moving_average(times, num_runs):15 times_padded = np.pad(16 times, (num_runs // 2, num_runs - 1 - num_runs // 2), mode="edge"17 )18 kernel = np.ones(num_runs) / num_runs19 return np.convolve(times_padded, kernel, mode="valid")202122parser = argparse.ArgumentParser(description=__doc__)23parser.add_argument("file", help="JSON file with benchmark results")24parser.add_argument("--title", help="Plot Title")25parser.add_argument("-o", "--output", help="Save image to the given filename.")26parser.add_argument(27 "-w",28 "--moving-average-width",29 type=int,30 metavar="num_runs",31 help="Width of the moving-average window (default: N/5)",32)33parser.add_argument(34 "--no-moving-average",35 action="store_true",36 help="Do not show moving average curve",37)383940args = parser.parse_args()4142with open(args.file) as f:43 results = json.load(f)["results"]4445for result in results:46 label = result["command"]47 times = result["times"]48 num = len(times)49 nums = range(num)5051 plt.scatter(x=nums, y=times, marker=".")52 plt.ylim([0, None])53 plt.xlim([-1, num])5455 if not args.no_moving_average:56 moving_average_width = (57 num // 5 if args.moving_average_width is None else args.moving_average_width58 )5960 average = moving_average(times, moving_average_width)61 plt.plot(nums, average, "-")6263if args.title:64 plt.title(args.title)6566legend = []67for result in results:68 legend.append(result["command"])69 if not args.no_moving_average:70 legend.append("moving average")71plt.legend(legend)7273plt.ylabel("Time [s]")7475if args.output:76 plt.savefig(args.output)77else:78 plt.show()