Add --time-unit option to 'advanced_statistics.py'

This commit is contained in:
David Peter committed 2024-12-01 21:13:31 +01:00
1 parent 4e1e9f2e48
commit f78f482d8f
1 file changed
+43 -7
+43 -7
View File
@@ -8,13 +8,45 @@
import argparse
import json
from enum import Enum
import numpy as np
class Unit(Enum):
SECOND = 1
MILLISECOND = 2
def factor(self):
match self:
case Unit.SECOND:
return 1
case Unit.MILLISECOND:
return 1e3
def __str__(self):
match self:
case Unit.SECOND:
return "s"
case Unit.MILLISECOND:
return "ms"
parser = argparse.ArgumentParser()
parser.add_argument("file", help="JSON file with benchmark results")
parser.add_argument(
"--time-unit",
help="The unit of time.",
default="second",
action="store",
choices=["second", "millisecond"],
dest="unit",
)
args = parser.parse_args()
unit = Unit.MILLISECOND if args.unit == "millisecond" else Unit.SECOND
unit_str = str(unit)
with open(args.file) as f:
results = json.load(f)["results"]
@@ -22,6 +54,8 @@ commands = [b["command"] for b in results]
times = [b["times"] for b in results]
for command, ts in zip(commands, times):
ts = [t * unit.factor() for t in ts]
p05 = np.percentile(ts, 5)
p25 = np.percentile(ts, 25)
p75 = np.percentile(ts, 75)
@@ -31,13 +65,15 @@ for command, ts in zip(commands, times):
print(f"Command '{command}'")
print(f" runs: {len(ts):8d}")
print(f" mean: {np.mean(ts):8.3f} s")
print(f" stddev: {np.std(ts, ddof=1):8.3f} s")
print(f" median: {np.median(ts):8.3f} s")
print(f" min: {np.min(ts):8.3f} s")
print(f" max: {np.max(ts):8.3f} s")
print(f" mean: {np.mean(ts):8.3f} {unit_str}")
print(f" stddev: {np.std(ts, ddof=1):8.3f} {unit_str}")
print(f" median: {np.median(ts):8.3f} {unit_str}")
print(f" min: {np.min(ts):8.3f} {unit_str}")
print(f" max: {np.max(ts):8.3f} {unit_str}")
print()
print(" percentiles:")
print(f" P_05 .. P_95: {p05:.3f} s .. {p95:.3f} s")
print(f" P_25 .. P_75: {p25:.3f} s .. {p75:.3f} s (IQR = {iqr:.3f} s)")
print(f" P_05 .. P_95: {p05:.3f} {unit_str} .. {p95:.3f} {unit_str}")
print(
f" P_25 .. P_75: {p25:.3f} {unit_str} .. {p75:.3f} {unit_str} (IQR = {iqr:.3f} {unit_str})"
)
print()