Skip to content
Myr

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 file

Take it

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

Update man pageDavid Peterbefore adoption0ce6578

line 1why, one step back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–9why, one step back
2# /// script
3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///

Update man pageDavid Peterbefore adoption0ce6578

lines 10–13why, one step back
10
11"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse

Sort importsDavid Peterbefore adoptionbc37eff

line 14why, one step back
14

Update man pageDavid Peterbefore adoption0ce6578

line 15why, one step back
15import matplotlib.pyplot as plt

Sort importsDavid Peterbefore adoptionbc37eff

line 16why, one step back
16import numpy as np

Update man pageDavid Peterbefore adoption0ce6578

line 17why, one step back
17

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 18why, one step back
18from plot_utils import METRICS, add_metric_argument, load_results

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

line 19why, one step back
19

Update man pageDavid Peterbefore adoption0ce6578

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

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

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

Update man pageDavid Peterbefore adoption0ce6578

lines 22–28why, one step back
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(

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 29–46why, one step back
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)

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 47why, one step back
47parser.add_argument(

Update man pageDavid Peterbefore adoption0ce6578

lines 48–51why, one step back
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(

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

lines 52–55why, one step back
52 "--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 back
56 help="Minimum metric value to display",

Update man pageDavid Peterbefore adoption0ce6578

lines 57–58why, one step back
57)
58parser.add_argument(

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

lines 59–62why, one step back
59 "--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 back
63 help="Maximum metric value to display",

Update man pageDavid Peterbefore adoption0ce6578

lines 64–71why, one step back
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()

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 72–73why, one step back
72metric, [results] = load_results(parser, [args.file], args.metric)
73metric_info = METRICS[metric]

Update man pageDavid Peterbefore adoption0ce6578

lines 74–77why, one step back
74
75if args.labels:76 labels = args.labels.split(",")77else:

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

line 78why, one step back
78 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 back
79all_values = [

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 80why, one step back
80 [m[metric]["value"] / metric_info.scale for m in b["measurements"]] for b in results

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

line 81why, one step back
81]

Update man pageDavid Peterbefore adoption0ce6578

line 82why, one step back
82

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

lines 83–92why, one step back
83value_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 back
93
94bins = int(args.bins) if args.bins else "auto"95histtype = args.type if args.type else "bar"96

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 97why, one step back
97plt.figure(figsize=(10, 5))

Update man pageDavid Peterbefore adoption0ce6578

line 98why, one step back
98plt.hist(

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

line 99why, one step back
99 all_values,

Update man pageDavid Peterbefore adoption0ce6578

lines 100–102why, one step back
100 label=labels,
101 bins=bins,102 histtype=histtype,

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

line 103why, one step back
103 range=(value_min, value_max),

Update man pageDavid Peterbefore adoption0ce6578

line 104why, one step back
104)

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 105why, one step back
105plt.legend(

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 106–110why, one step back
106 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 back
111

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

line 112why, one step back
112plt.xlabel(metric_info.axis_label)

Update man pageDavid Peterbefore adoption0ce6578

lines 113–121why, one step back
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:

add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140

line 122why, one step back
122 plt.savefig(args.output, dpi=600)

Update man pageDavid Peterbefore adoption0ce6578

lines 123–124why, one step back
123else:
124 plt.show()