plot_whisker.py
This file
Path
scripts/plot_whisker.py
69 lines · 2.2 KiB · Python
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
The walk stopped after 200 commits: lines older than 9859eca point at it.
Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–8why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# ]8# ///Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 9–18why, one step back910"""This program shows `hyperfine` benchmark results as a box and whisker plot.1112Quoting from the matplotlib documentation:13 The box extends from the lower to upper quartile values of the data, with14 a line at the median. The whiskers extend from the box to show the range15 of the data. Flier points are those past the end of the whiskers.16"""1718import argparseSort importsDavid Peterbefore adoptionbc37eff
line 19why, one step back19Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 20–21why, one step back20import matplotlib.pyplot as plt21Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 22why, one step back22from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 23why, one step back23Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 24why, one step back24parser = argparse.ArgumentParser(description=__doc__)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 25why, one step back25add_metric_argument(parser)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 26–27why, one step back26parser.add_argument("file", help="JSON file with benchmark results")27parser.add_argument("--title", help="Plot Title")Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 28why, one step back28parser.add_argument("--sort-by", choices=["median"], help="Sort method")Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 29–31why, one step back29parser.add_argument(30 "--labels", help="Comma-separated list of entries for the plot legend"31)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 32why, one step back32parser.add_argument("-o", "--output", help="Save image to the given filename.")Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 33–34why, one step back3334args = parser.parse_args()Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 35–36why, one step back35metric, [results] = load_results(parser, [args.file], args.metric)36metric_info = METRICS[metric]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 37–40why, one step back3738if args.labels:39 labels = args.labels.split(",")40else:Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 41why, one step back41 labels = [b.get("name", b["command"]) for b in results]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 42why, one step back42values = [Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 43why, one step back43 [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 44why, one step back44]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 45why, one step back45Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 46why, one step back46if args.sort_by == "median":Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 47why, one step back47 medians = [b["summary"][metric]["median"] for b in results]Add --sort-by optionSerpent7776before adoption51d056a
lines 48–49why, one step back48 indices = sorted(range(len(labels)), key=lambda k: medians[k])49 labels = [labels[i] for i in indices]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 50why, one step back50 values = [values[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 51why, one step back51Fix long labels being cut offSerpent7776before adoption836d173
line 52why, one step back52plt.figure(figsize=(10, 6), constrained_layout=True)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 53why, one step back53boxplot = plt.boxplot(values, vert=True, patch_artist=True)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 54why, one step back54cmap = plt.get_cmap("rainbow")Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 55why, one step back55colors = [cmap(val / len(values)) for val in range(len(values))]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 56–62why, one step back5657for patch, color in zip(boxplot["boxes"], colors):58 patch.set_facecolor(color)5960if args.title:61 plt.title(args.title)62plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 63why, one step back63plt.ylabel(metric_info.axis_label)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 64why, one step back64plt.ylim(0, None)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 65why, one step back65plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 66–69why, one step back66if args.output:67 plt.savefig(args.output)68else:69 plt.show()