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

The walk stopped after 200 commits: lines older than 73f1e14 point at it.

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 1why, one step back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–9why, one step back
2# /// script
3# requires-python = ">=3.10"4# dependencies = [5# "pyqt6",6# "matplotlib",7# "numpy",8# ]9# ///

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

lines 10–40why, one step back
10
11"""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)

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 41–45why, one step back
41parser.add_argument(
42 "--no-moving-average",43 action="store_true",44 help="Do not show moving average curve",45)

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 46why, one step back
46

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

line 47why, one step back
47

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

lines 48–52why, one step back
48args = parser.parse_args()
4950with open(args.file) as f:51 results = json.load(f)["results"]52

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 53–57why, one step back
53for result in results:
54 label = result["command"]55 times = result["times"]56 num = len(times)57 nums = range(num)

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 58why, one step back
58

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 59–61why, one step back
59 plt.scatter(x=nums, y=times, marker=".")
60 plt.ylim([0, None])61 plt.xlim([-1, num])

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 62why, one step back
62

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 63–66why, one step back
63 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 )

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 67why, one step back
67

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 68–69why, one step back
68 average = moving_average(times, moving_average_width)
69 plt.plot(nums, average, "-")

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

lines 70–72why, one step back
70
71if args.title:72 plt.title(args.title)

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

lines 73–80why, one step back
73
74legend = []75for result in results:76 legend.append(result["command"])77 if not args.no_moving_average:78 legend.append("moving average")79plt.legend(legend)80

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

line 81why, one step back
81plt.ylabel("Time [s]")

Improve plot_progression.py scriptDavid Peterbefore adoptionc7be935

line 82why, one step back
82

Add hints to all export optionsEngineer-of-Efficiencybefore adoption73f1e14

lines 83–86why, one step back
83if args.output:
84 plt.savefig(args.output)85else:86 plt.show()