Files

59 lines
1.6 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
2024-08-28 16:52:02 +02:00
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "scipy",
# ]
# ///
2019-10-07 14:57:42 +09:00
2019-10-07 18:44:55 +02:00
"""This script performs Welch's t-test on a JSON export file with two
benchmark results to test for a difference in their population means."""
2019-10-07 18:44:55 +02:00
2019-10-07 14:57:42 +09:00
import argparse
import math
2024-08-28 17:01:09 +02:00
2019-10-07 14:57:42 +09:00
from scipy import stats
from plot_utils import METRICS, add_metric_argument, load_results
2019-10-07 14:57:42 +09:00
2026-10-07 11:24:14 +02:00
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("file", help="JSON file with two benchmark results")
add_metric_argument(parser)
args = parser.parse_args()
2019-10-07 14:57:42 +09:00
2026-10-07 11:24:14 +02:00
metric, [results] = load_results(parser, [args.file], args.metric)
2019-10-07 18:36:00 +02:00
2026-10-07 11:24:14 +02:00
if len(results) != 2:
parser.error("the input file must contain exactly two benchmarks")
2019-10-07 14:57:42 +09:00
2026-10-07 11:24:14 +02:00
a, b = (x.get("name", x["command"]) for x in results[:2])
X, Y = ([m[metric]["value"] for m in x["measurements"]] for x in results[:2])
if min(len(X), len(Y)) < 2:
parser.error("Welch's t-test requires at least two measurements per benchmark")
2019-10-07 14:57:42 +09:00
2026-10-07 11:24:14 +02:00
print(f"Metric: {METRICS[metric].label}")
print(f"Command 1: {a}")
print(f"Command 2: {b}\n")
2019-10-07 18:58:07 +02:00
2026-10-07 11:24:14 +02:00
t, p = stats.ttest_ind(X, Y, equal_var=False)
if not math.isfinite(p):
parser.error("Welch's t-test is undefined for these measurements")
th = 0.05
dispose = p < th
print(f"t = {t:.3}, p = {p:.3}")
print()
if dispose:
print(
f"A statistically significant difference in mean value was detected (p < {th})."
)
else:
print(
f"No statistically significant difference in mean value was detected (p >= {th})."
)
if __name__ == "__main__":
main()