Skip to content
Myr

plot_histogram.py

This file

Path

scripts/plot_histogram.py

59 lines · 1.7 KiB · Python

Last changed by

Add --t-min/--t-max to histogram scriptDavid Peterbefore adoption0601a19

History of this file

Take it

The walk stopped after 200 commits: lines older than eb72702 point at it.

Update LICENSEDavid Peterbefore adoptioneb72702

lines 1–22why, one step back
1#!/usr/bin/env python
23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import json7import numpy as np8import matplotlib.pyplot as plt910parser = argparse.ArgumentParser(description=__doc__)11parser.add_argument("file", help="JSON file with benchmark results")12parser.add_argument("--title", help="Plot title")13parser.add_argument(14 "--labels", help="Comma-separated list of entries for the plot legend"15)16parser.add_argument("--bins", help="Number of bins (default: auto)")17parser.add_argument(18 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"19)20parser.add_argument(21 "-o", "--output", help="Save image to the given filename."22)

Add --t-min/--t-max to histogram scriptDavid Peterbefore adoption0601a19

lines 23–28why, one step back
23parser.add_argument(
24 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"25)26parser.add_argument(27 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"28)

Update LICENSEDavid Peterbefore adoptioneb72702

lines 29–40why, one step back
29
30args = parser.parse_args()3132with open(args.file) as f:33 results = json.load(f)["results"]3435if args.labels:36 labels = args.labels.split(",")37else:38 labels = [b["command"] for b in results]39all_times = [b["times"] for b in results]40

Add --t-min/--t-max to histogram scriptDavid Peterbefore adoption0601a19

lines 41–42why, one step back
41t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))
42t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))

Update LICENSEDavid Peterbefore adoptioneb72702

lines 43–59why, one step back
43
44bins = int(args.bins) if args.bins else "auto"45histtype = args.type if args.type else "bar"4647plt.hist(48 all_times, label=labels, bins=bins, histtype=histtype, range=(t_min, t_max),49)50plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})5152plt.xlabel("Time [s]")53if args.title:54 plt.title(args.title)5556if args.output:57 plt.savefig(args.output)58else:59 plt.show()