Skip to content
Myr

advanced_statistics.py

This file

Path

scripts/advanced_statistics.py

79 lines · 1.9 KiB · Python

Last changed by

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

History of this file

Take it

1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5#     "numpy",6# ]7# ///89import argparse10import json11from enum import Enum1213import numpy as np141516class Unit(Enum):17    SECOND = 118    MILLISECOND = 21920    def factor(self):21        match self:22            case Unit.SECOND:23                return 124            case Unit.MILLISECOND:25                return 1e32627    def __str__(self):28        match self:29            case Unit.SECOND:30                return "s"31            case Unit.MILLISECOND:32                return "ms"333435parser = argparse.ArgumentParser()36parser.add_argument("file", help="JSON file with benchmark results")37parser.add_argument(38    "--time-unit",39    help="The unit of time.",40    default="second",41    action="store",42    choices=["second", "millisecond"],43    dest="unit",44)45args = parser.parse_args()4647unit = Unit.MILLISECOND if args.unit == "millisecond" else Unit.SECOND48unit_str = str(unit)4950with open(args.file) as f:51    results = json.load(f)["results"]5253commands = [b["command"] for b in results]54times = [b["times"] for b in results]5556for command, ts in zip(commands, times):57    ts = [t * unit.factor() for t in ts]5859    p05 = np.percentile(ts, 5)60    p25 = np.percentile(ts, 25)61    p75 = np.percentile(ts, 75)62    p95 = np.percentile(ts, 95)6364    iqr = p75 - p256566    print(f"Command '{command}'")67    print(f"  runs:   {len(ts):8d}")68    print(f"  mean:   {np.mean(ts):8.3f} {unit_str}")69    print(f"  stddev: {np.std(ts, ddof=1):8.3f} {unit_str}")70    print(f"  median: {np.median(ts):8.3f} {unit_str}")71    print(f"  min:    {np.min(ts):8.3f} {unit_str}")72    print(f"  max:    {np.max(ts):8.3f} {unit_str}")73    print()74    print("  percentiles:")75    print(f"     P_05 .. P_95:    {p05:.3f} {unit_str} .. {p95:.3f} {unit_str}")76    print(77        f"     P_25 .. P_75:    {p25:.3f} {unit_str} .. {p75:.3f} {unit_str}  (IQR = {iqr:.3f} {unit_str})"78    )79    print()