plot_histogram.py
This file
Path
scripts/plot_histogram.py
96 lines · 2.4 KiB · Python
Last changed by
Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
History of this fileTake it
The walk stopped after 200 commits: lines older than ec43213 point at it.
Fix clippy warningsDavid Peterbefore adoptionec43213
lines 1–17why, one step back1#!/usr/bin/env python23"""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(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 18–35why, one step back18 "--legend-location",19 help="Location of the legend on plot (default: upper center)",20 choices=[21 "upper center",22 "lower center",23 "right",24 "left",25 "best",26 "upper left",27 "upper right",28 "lower left",29 "lower right",30 "center left",31 "center right",32 "center",33 ],34 default="upper center",35)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 36why, one step back36parser.add_argument(Fix clippy warningsDavid Peterbefore adoptionec43213
lines 37–38why, one step back37 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"38)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
line 39why, one step back39parser.add_argument("-o", "--output", help="Save image to the given filename.")Fix clippy warningsDavid Peterbefore adoptionec43213
lines 40–45why, one step back40parser.add_argument(41 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"42)43parser.add_argument(44 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"45)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 46–50why, one step back46parser.add_argument(47 "--log-count",48 help="Use a logarithmic y-axis for the event count",49 action="store_true",50)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 51–68why, one step back5152args = parser.parse_args()5354with open(args.file) as f:55 results = json.load(f)["results"]5657if args.labels:58 labels = args.labels.split(",")59else:60 labels = [b["command"] for b in results]61all_times = [b["times"] for b in results]6263t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))64t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))6566bins = int(args.bins) if args.bins else "auto"67histtype = args.type if args.type else "bar"68add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 69why, one step back69plt.figure(figsize=(10, 5))Fix clippy warningsDavid Peterbefore adoptionec43213
line 70why, one step back70plt.hist(Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 71–75why, one step back71 all_times,72 label=labels,73 bins=bins,74 histtype=histtype,75 range=(t_min, t_max),Fix clippy warningsDavid Peterbefore adoptionec43213
line 76why, one step back76)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 77why, one step back77plt.legend(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 78–82why, one step back78 loc=args.legend_location,79 fancybox=True,80 shadow=True,81 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},82)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 83–86why, one step back8384plt.xlabel("Time [s]")85if args.title:86 plt.title(args.title)Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c
lines 87–91why, one step back8788if args.log_count:89 plt.yscale("log")90else:91 plt.ylim(0, None)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 92–93why, one step back9293if args.output:add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 94why, one step back94 plt.savefig(args.output, dpi=600)Fix clippy warningsDavid Peterbefore adoptionec43213
lines 95–96why, one step back95else:96 plt.show()