File size: 1,494 Bytes
37ff7c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import annotations

import json
import time

import joblib
import pandas as pd

from credexp.config import settings
from credexp.data.io import processed_dir


def load_data():
    holdout_path = processed_dir() / "api_holdout.parquet"
    df = pd.read_parquet(holdout_path)
    if "TARGET" in df.columns:
        df = df.drop(columns=["TARGET"])
    return df.head(100).copy()


def main() -> None:
    pipe = joblib.load(settings.artifacts_dir / "models" / "pipeline.joblib")
    df = load_data()

    single = df.head(1).copy()
    batch = df.head(50).copy()

    # 50 single-row calls
    t0 = time.perf_counter()
    for _ in range(50):
        _ = pipe.predict_proba(single)
    single_total_ms = (time.perf_counter() - t0) * 1000

    # 1 batch call of 50 rows
    t0 = time.perf_counter()
    _ = pipe.predict_proba(batch)
    batch_total_ms = (time.perf_counter() - t0) * 1000

    result = {
        "single_total_ms_for_50_calls": single_total_ms,
        "single_avg_ms_per_row": single_total_ms / 50,
        "batch_total_ms_for_50_rows": batch_total_ms,
        "batch_avg_ms_per_row": batch_total_ms / 50,
        "speedup_factor_per_row": (single_total_ms / 50) / (batch_total_ms / 50),
    }

    out_path = "reports/performance/batching_benchmark.json"
    with open(out_path, "w", encoding="utf-8") as f:
        json.dump(result, f, indent=2)

    print(json.dumps(result, indent=2))
    print(f"Saved to {out_path}")


if __name__ == "__main__":
    main()