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 fileTake it
1"""Shared metric selection and units for analysis and plotting scripts."""23import json4from 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.label151617METRICS = {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"),28}293031def add_metric_argument(parser):32 parser.add_argument(33 "--metric",34 choices=METRICS,35 help="Metric to analyze (default: primary_metric from the JSON export, "36 "or time_wall_clock if unspecified). "37 "Times are in seconds; memory is in MiB; hardware counters are unscaled counts.",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 )484950def 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