Skip to content
Myr

plot_progression.py

This file

Path

scripts/plot_progression.py

95 lines · 2.5 KiB · Python

Last changed by

Make plotting scripts runnable with uvDavid Peterbefore adoptionce49529

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 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")303132def main():33    parser = argparse.ArgumentParser(description=__doc__)34    add_metric_argument(parser)35    parser.add_argument("file", help="JSON file with benchmark results")36    parser.add_argument("--title", help="Plot Title")37    parser.add_argument("-o", "--output", help="Save image to the given filename.")38    parser.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    )45    parser.add_argument(46        "--no-moving-average",47        action="store_true",48        help="Do not show moving average curve",49    )5051    args = parser.parse_args()52    metric, [results] = load_results(parser, [args.file], args.metric)53    metric_info = METRICS[metric]5455    for result in results:56        values = [57            m[metric]["value"] / metric_info.scale for m in result["measurements"]58        ]59        num = len(values)60        nums = range(num)6162        plt.scatter(x=nums, y=values, marker=".")63        plt.ylim([0, None])64        plt.xlim([-1, num])6566        if not args.no_moving_average:67            moving_average_width = (68                max(1, num // 5)69                if args.moving_average_width is None70                else args.moving_average_width71            )7273            average = moving_average(values, moving_average_width)74            plt.plot(nums, average, "-")7576    if args.title:77        plt.title(args.title)7879    legend = []80    for result in results:81        legend.append(result.get("name", result["command"]))82        if not args.no_moving_average:83            legend.append("moving average")84    plt.legend(legend)8586    plt.ylabel(metric_info.axis_label)8788    if args.output:89        plt.savefig(args.output)90    else:91        plt.show()929394if __name__ == "__main__":95    main()