plot_histogram.py
This file
Path
scripts/plot_histogram.py
124 lines · 2.8 KiB · Python
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ce6578 point at it.
Update man pageDavid Peterbefore adoption0ce6578
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–9why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///Update man pageDavid Peterbefore adoption0ce6578
lines 10–13why, one step back1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparseSort importsDavid Peterbefore adoptionbc37eff
line 14why, one step back14Update man pageDavid Peterbefore adoption0ce6578
line 15why, one step back15import matplotlib.pyplot as pltSort importsDavid Peterbefore adoptionbc37eff
line 16why, one step back16import numpy as npUpdate man pageDavid Peterbefore adoption0ce6578
line 17why, one step back17Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 18why, one step back18from plot_utils import METRICS, add_metric_argument, load_resultsSupport all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 19why, one step back19Update man pageDavid Peterbefore adoption0ce6578
line 20why, one step back20parser = argparse.ArgumentParser(description=__doc__)Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 21why, one step back21add_metric_argument(parser)Update man pageDavid Peterbefore adoption0ce6578
lines 22–28why, one step back22parser.add_argument("file", help="JSON file with benchmark results")23parser.add_argument("--title", help="Plot title")24parser.add_argument(25 "--labels", help="Comma-separated list of entries for the plot legend"26)27parser.add_argument("--bins", help="Number of bins (default: auto)")28parser.add_argument(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 29–46why, one step back29 "--legend-location",30 help="Location of the legend on plot (default: upper center)",31 choices=[32 "upper center",33 "lower center",34 "right",35 "left",36 "best",37 "upper left",38 "upper right",39 "lower left",40 "lower right",41 "center left",42 "center right",43 "center",44 ],45 default="upper center",46)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 47why, one step back47parser.add_argument(Update man pageDavid Peterbefore adoption0ce6578
lines 48–51why, one step back48 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"49)50parser.add_argument("-o", "--output", help="Save image to the given filename.")51parser.add_argument(Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 52–55why, one step back52 "--min",53 "--t-min",54 dest="value_min",55 type=float,Support hardware counters in plotting scripts (#984)David Peterbefore adoptione262cd6
line 56why, one step back56 help="Minimum metric value to display",Update man pageDavid Peterbefore adoption0ce6578
lines 57–58why, one step back57)58parser.add_argument(Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 59–62why, one step back59 "--max",60 "--t-max",61 dest="value_max",62 type=float,Support hardware counters in plotting scripts (#984)David Peterbefore adoptione262cd6
line 63why, one step back63 help="Maximum metric value to display",Update man pageDavid Peterbefore adoption0ce6578
lines 64–71why, one step back64)65parser.add_argument(66 "--log-count",67 help="Use a logarithmic y-axis for the event count",68 action="store_true",69)7071args = parser.parse_args()Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 72–73why, one step back72metric, [results] = load_results(parser, [args.file], args.metric)73metric_info = METRICS[metric]Update man pageDavid Peterbefore adoption0ce6578
lines 74–77why, one step back7475if args.labels:76 labels = args.labels.split(",")77else:Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 78why, one step back78 labels = [b.get("name", b["command"]) for b in results]Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 79why, one step back79all_values = [Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 80why, one step back80 [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in resultsMerge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 81why, one step back81]Update man pageDavid Peterbefore adoption0ce6578
line 82why, one step back82Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
lines 83–92why, one step back83value_min = (84 args.value_min85 if args.value_min is not None86 else np.min(list(map(np.min, all_values)))87)88value_max = (89 args.value_max90 if args.value_max is not None91 else np.max(list(map(np.max, all_values)))92)Update man pageDavid Peterbefore adoption0ce6578
lines 93–96why, one step back9394bins = int(args.bins) if args.bins else "auto"95histtype = args.type if args.type else "bar"96add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 97why, one step back97plt.figure(figsize=(10, 5))Update man pageDavid Peterbefore adoption0ce6578
line 98why, one step back98plt.hist(Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 99why, one step back99 all_values,Update man pageDavid Peterbefore adoption0ce6578
lines 100–102why, one step back100 label=labels,101 bins=bins,102 histtype=histtype,Support all exported metrics in plotting scripts (#981)David Peterbefore adoptionef86c83
line 103why, one step back103 range=(value_min, value_max),Update man pageDavid Peterbefore adoption0ce6578
line 104why, one step back104)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 105why, one step back105plt.legend(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 106–110why, one step back106 loc=args.legend_location,107 fancybox=True,108 shadow=True,109 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},110)Update man pageDavid Peterbefore adoption0ce6578
line 111why, one step back111Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 112why, one step back112plt.xlabel(metric_info.axis_label)Update man pageDavid Peterbefore adoption0ce6578
lines 113–121why, one step back113if args.title:114 plt.title(args.title)115116if args.log_count:117 plt.yscale("log")118else:119 plt.ylim(0, None)120121if args.output:add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 122why, one step back122 plt.savefig(args.output, dpi=600)Update man pageDavid Peterbefore adoption0ce6578
lines 123–124why, one step back123else:124 plt.show()