Skip to content
Myr

plot_utils.py

This file

Path

scripts/plot_utils.py

74 lines · 2.3 KiB · Python

Last changed by

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

History of this file

Take it

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

line 1why, one step back
1"""Shared metric selection and units for analysis and plotting scripts."""

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

line 2why, one step back
2

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

lines 3–16why, one step back
3import json
4from typing import NamedTuple567class Metric(NamedTuple):8 label: str9 unit: str = ""10 scale: float = 11112 @property13 def axis_label(self):14 return f"{self.label} [{self.unit}]" if self.unit else self.label1516

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

line 17why, one step back
17METRICS = {

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

lines 18–27why, one step back
18 "time_wall_clock": Metric("Wall-clock time", "s"),
19 "time_cpu": Metric("Total CPU time", "s"),20 "time_user": Metric("User CPU time", "s"),21 "time_system": Metric("System CPU time", "s"),22 "memory_peak_resident": Metric("Peak resident memory", "MiB", 1024**2),23 "cpu_cycles": Metric("CPU cycles"),24 "instructions": Metric("Completed CPU instructions"),25 "cache_references": Metric("Cache references"),26 "cache_misses": Metric("Cache misses"),27 "branch_misses": Metric("Mispredicted branches"),

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

lines 28–34why, one step back
28}
293031def add_metric_argument(parser):32 parser.add_argument(33 "--metric",34 choices=METRICS,

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

lines 35–36why, one step back
35 help="Metric to analyze (default: primary_metric from the JSON export, "
36 "or time_wall_clock if unspecified). "

Support hardware counters in plotting scripts (#984)David Peterbefore adoptione262cd6

line 37why, one step back
37 "Times are in seconds; memory is in MiB; hardware counters are unscaled counts.",

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

lines 38–47why, one step back
38 )
394041def validate_metric(parser, results, metric):42 for result in results:43 if metric not in result["summary"]:44 parser.error(45 f"metric {metric!r} is unavailable for "46 f"{result.get('name', result['command'])!r}"47 )

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

lines 48–74why, one step back
48
4950def load_results(parser, filenames, metric=None):51 exports = []52 for filename in filenames:53 with open(filename, encoding="utf-8") as f:54 export = json.load(f)55 if not export["results"]:56 parser.error(f"no benchmark results in {filename}")57 exports.append(export)5859 if metric is None:60 primary_metrics = {61 export.get("primary_metric", "time_wall_clock") for export in exports62 }63 if len(primary_metrics) != 1:64 parser.error(65 "input files use different primary metrics; select one with --metric"66 )67 metric = primary_metrics.pop()68 if metric not in METRICS:69 parser.error(f"unknown metric {metric!r}; select one with --metric")7071 results = [export["results"] for export in exports]72 for benchmarks in results:73 validate_metric(parser, benchmarks, metric)74 return metric, results