Skip to content
Myr

plot_whisker.py

This file

Path

scripts/plot_whisker.py

73 lines · 2.3 KiB · Python

Last changed by

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

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–19why, 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 argparse19import json

Sort importsDavid Peterbefore adoptionbc37eff

line 20why, one step back
20

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

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

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

lines 23–24why, one step back
23from plot_utils import METRICS, add_metric_argument, validate_metric
24

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

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

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

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

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

lines 27–28why, one step back
27parser.add_argument("file", help="JSON file with benchmark results")
28parser.add_argument("--title", help="Plot Title")

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 29why, one step back
29parser.add_argument("--sort-by", choices=["median"], help="Sort method")

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

lines 30–32why, one step back
30parser.add_argument(
31 "--labels", help="Comma-separated list of entries for the plot legend"32)

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 33why, one step back
33parser.add_argument("-o", "--output", help="Save image to the given filename.")

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

lines 34–35why, one step back
34
35args = parser.parse_args()

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

line 36why, one step back
36metric_label, metric_scale = METRICS[args.metric]

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

line 37why, one step back
37

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 38why, one step back
38with open(args.file, encoding="utf-8") as f:

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

line 39why, one step back
39 results = json.load(f)["results"]

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

line 40why, one step back
40validate_metric(parser, results, args.metric)

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

lines 41–44why, one step back
41
42if args.labels:43 labels = args.labels.split(",")44else:

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

line 45why, one step back
45 labels = [b.get("name", b["command"]) for b in results]

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

lines 46–48why, one step back
46values = [
47 [m[args.metric]["value"] / metric_scale for m in b["measurements"]] for b in results48]

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

line 49why, one step back
49

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

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

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

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

Add --sort-by optionSerpent7776before adoption51d056a

lines 52–53why, one step back
52 indices = sorted(range(len(labels)), key=lambda k: medians[k])
53 labels = [labels[i] for i in indices]

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

line 54why, one step back
54 values = [values[i] for i in indices]

Nicer whiskers plotSerpent7776before adoption5c9d7a8

line 55why, one step back
55

Fix long labels being cut offSerpent7776before adoption836d173

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

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

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

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

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

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

line 59why, one step back
59colors = [cmap(val / len(values)) for val in range(len(values))]

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

lines 60–66why, one step back
60
61for patch, color in zip(boxplot["boxes"], colors):62 patch.set_facecolor(color)6364if args.title:65 plt.title(args.title)66plt.legend(handles=boxplot["boxes"], labels=labels, loc="best", fontsize="medium")

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

line 67why, one step back
67plt.ylabel(metric_label)

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

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

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 69why, one step back
69plt.xticks(list(range(1, len(labels) + 1)), labels, rotation=45)

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

lines 70–73why, one step back
70if args.output:
71 plt.savefig(args.output)72else:73 plt.show()