Skip to content
Myr

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 file

Take 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 back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–8why, one step back
2# /// script
3# 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 back
9
10"""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 argparse

Sort importsDavid Peterbefore adoptionbc37eff

line 19why, one step back
19

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

lines 20–21why, one step back
20import matplotlib.pyplot as plt
21

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 22why, one step back
22from plot_utils import METRICS, add_metric_argument, load_results

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 23why, one step back
23

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

line 24why, one step back
24parser = argparse.ArgumentParser(description=__doc__)

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 25why, one step back
25add_metric_argument(parser)

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

lines 26–27why, one step back
26parser.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 back
28parser.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 back
29parser.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 back
32parser.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 back
33
34args = parser.parse_args()

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 35–36why, one step back
35metric, [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 back
37
38if args.labels:39 labels = args.labels.split(",")40else:

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 41why, one step back
41 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 back
42values = [

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 43why, one step back
43 [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in results

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 44why, one step back
44]

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

line 45why, one step back
45

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 46why, one step back
46if args.sort_by == "median":

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 47why, one step back
47 medians = [b["summary"][metric]["median"] for b in results]

Add --sort-by optionSerpent7776before adoption51d056a

lines 48–49why, one step back
48 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 back
50 values = [values[i] for i in indices]

Nicer whiskers plotSerpent7776before adoption5c9d7a8

line 51why, one step back
51

Fix long labels being cut offSerpent7776before adoption836d173

line 52why, one step back
52plt.figure(figsize=(10, 6), constrained_layout=True)

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 53why, one step back
53boxplot = plt.boxplot(values, vert=True, patch_artist=True)

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

line 54why, one step back
54cmap = plt.get_cmap("rainbow")

Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83

line 55why, one step back
55colors = [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 back
56
57for 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 back
63plt.ylabel(metric_info.axis_label)

Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca

line 64why, one step back
64plt.ylim(0, None)

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 65why, one step back
65plt.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 back
66if args.output:
67 plt.savefig(args.output)68else:69 plt.show()