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
1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse1415import matplotlib.pyplot as plt16import numpy as np1718from plot_utils import METRICS, add_metric_argument, load_results1920parser = argparse.ArgumentParser(description=__doc__)21add_metric_argument(parser)22parser.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(29 "--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)47parser.add_argument(48 "--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(52 "--min",53 "--t-min",54 dest="value_min",55 type=float,56 help="Minimum metric value to display",57)58parser.add_argument(59 "--max",60 "--t-max",61 dest="value_max",62 type=float,63 help="Maximum metric value to display",64)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()72metric, [results] = load_results(parser, [args.file], args.metric)73metric_info = METRICS[metric]7475if args.labels:76 labels = args.labels.split(",")77else:78 labels = [b.get("name", b["command"]) for b in results]79all_values = [80 [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in results81]8283value_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)9394bins = int(args.bins) if args.bins else "auto"95histtype = args.type if args.type else "bar"9697plt.figure(figsize=(10, 5))98plt.hist(99 all_values,100 label=labels,101 bins=bins,102 histtype=histtype,103 range=(value_min, value_max),104)105plt.legend(106 loc=args.legend_location,107 fancybox=True,108 shadow=True,109 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},110)111112plt.xlabel(metric_info.axis_label)113if args.title:114 plt.title(args.title)115116if args.log_count:117 plt.yscale("log")118else:119 plt.ylim(0, None)120121if args.output:122 plt.savefig(args.output, dpi=600)123else:124 plt.show()