plot_whisker.py
This file
Path
scripts/plot_whisker.py
66 lines · 2.1 KiB · Python
Last changed by
Refine JSON schema and command metadataDavid Peterbefore adoption0d86e78
History of this fileTake it
The walk stopped after 200 commits: lines older than e13d0cb point at it.
Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
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# ///Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 9–19why, 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 argparse19import jsonSort importsDavid Peterbefore adoptionbc37eff
line 20why, one step back20Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 21–25why, one step back21import matplotlib.pyplot as plt2223parser = argparse.ArgumentParser(description=__doc__)24parser.add_argument("file", help="JSON file with benchmark results")25parser.add_argument("--title", help="Plot Title")Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 26why, one step back26parser.add_argument("--sort-by", choices=["median"], help="Sort method")Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 27–29why, one step back27parser.add_argument(28 "--labels", help="Comma-separated list of entries for the plot legend"29)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 30why, one step back30parser.add_argument("-o", "--output", help="Save image to the given filename.")Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 31–33why, one step back3132args = parser.parse_args()33Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 34why, one step back34with open(args.file, encoding="utf-8") as f:Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 35–39why, one step back35 results = json.load(f)["results"]3637if args.labels:38 labels = args.labels.split(",")39else:Refine JSON schema and command metadataDavid Peterbefore adoption0d86e78
line 40why, one step back40 labels = [b.get("name", b["command"]) for b in results]Export metric summaries and adapt analysis scriptsDavid Peterbefore adoption1b3ec92
line 41why, one step back41times = [[m["time_wall_clock"]["value"] for m in b["measurements"]] for b in results]Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
line 42why, one step back42Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 43why, one step back43if args.sort_by == "median":Export metric summaries and adapt analysis scriptsDavid Peterbefore adoption1b3ec92
line 44why, one step back44 medians = [b["summary"]["time_wall_clock"]["median"] for b in results]Add --sort-by optionSerpent7776before adoption51d056a
lines 45–47why, one step back45 indices = sorted(range(len(labels)), key=lambda k: medians[k])46 labels = [labels[i] for i in indices]47 times = [times[i] for i in indices]Nicer whiskers plotSerpent7776before adoption5c9d7a8
line 48why, one step back48Fix long labels being cut offSerpent7776before adoption836d173
line 49why, one step back49plt.figure(figsize=(10, 6), constrained_layout=True)Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 50–61why, one step back50boxplot = plt.boxplot(times, vert=True, patch_artist=True)51cmap = plt.get_cmap("rainbow")52colors = [cmap(val / len(times)) for val in range(len(times))]5354for patch, color in zip(boxplot["boxes"], colors):55 patch.set_facecolor(color)5657if args.title:58 plt.title(args.title)59plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")60plt.ylabel("Time [s]")61plt.ylim(0, None)Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
line 62why, one step back62plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)Add --conclude option with testsJack O'Connorbefore adoptione13d0cb
lines 63–66why, one step back63if args.output:64 plt.savefig(args.output)65else:66 plt.show()