Download backend/data/benchmark_latency.py from interactAi-inc/Pit-Lane-Project: direct link, hf CLI and curl.
- Browser
- Download file 9.01 kB
-
https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/data/benchmark_latency.py
- Command line
-
hf download hf://interactAi-inc/Pit-Lane-Project/backend/data/benchmark_latency.py
-
curl -L -o benchmark_latency.py https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/data/benchmark_latency.py
9.01 kB
| # ============================================================ | |
| # F1 PIT LANE - MODEL LATENCY BENCHMARK | |
| # | |
| # Measures: | |
| # 1. XGBoost inference only | |
| # 2. Preprocessing + XGBoost inference | |
| # 3. Batch prediction for 20 drivers | |
| # | |
| # IMPORTANT: | |
| # The benchmark loads the SAME .pkl used by the backend. | |
| # ============================================================ | |
| import time | |
| from pathlib import Path | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| # ============================================================ | |
| # CONFIGURATION | |
| # ============================================================ | |
| MODEL_PATH = ( | |
| "C:\\Users\\Admin\\Desktop\\PitLane\\F1_Model\\f1_live_winner_model.pkl" | |
| ) | |
| DATASET_PATH = ( | |
| "C:\\Users\\Admin\\Desktop\\PitLane\\F1_Model\\F1_2026_model_training_data.csv" | |
| ) | |
| # Number of measured repetitions | |
| N_RUNS = 1000 | |
| # Warm-up predictions are NOT included in the measurements. | |
| WARMUP_RUNS = 100 | |
| # ============================================================ | |
| # LOAD MODEL | |
| # ============================================================ | |
| print("=" * 80) | |
| print("F1 PIT LANE - MODEL LATENCY BENCHMARK") | |
| print("=" * 80) | |
| if not Path(MODEL_PATH).exists(): | |
| raise FileNotFoundError( | |
| f"Model not found:\n{MODEL_PATH}" | |
| ) | |
| print("\nLoading model bundle...") | |
| bundle = joblib.load( | |
| MODEL_PATH | |
| ) | |
| model = bundle["model"] | |
| preprocessor = bundle["preprocessor"] | |
| feature_columns = bundle["feature_columns"] | |
| print( | |
| f"Model loaded successfully." | |
| ) | |
| print( | |
| f"Expected features: " | |
| f"{len(feature_columns)}" | |
| ) | |
| # ============================================================ | |
| # LOAD REAL DATA | |
| # | |
| # We use your actual model-training dataset rather than | |
| # synthetic/random values. | |
| # ============================================================ | |
| if not Path(DATASET_PATH).exists(): | |
| raise FileNotFoundError( | |
| f"Dataset not found:\n{DATASET_PATH}" | |
| ) | |
| print("\nLoading real feature data...") | |
| df = pd.read_csv( | |
| DATASET_PATH, | |
| low_memory=False | |
| ) | |
| # ============================================================ | |
| # PREPARE ONE REAL MODEL INPUT ROW | |
| # ============================================================ | |
| # ------------------------------------------------------------ | |
| # Remove target / metadata exactly as the training pipeline | |
| # does. | |
| # ------------------------------------------------------------ | |
| DROP_COLUMNS = [ | |
| "Winner", | |
| "RaceDate", | |
| "winning_driver_number", | |
| "WinningDriver", | |
| "WinningDriverName", | |
| "RaceID", | |
| "session_key", | |
| "meeting_key", | |
| "location", | |
| "country_name", | |
| "RoundNumber", | |
| "EventName", | |
| "_RaceDate" | |
| ] | |
| X = df.drop( | |
| columns=[ | |
| column | |
| for column in DROP_COLUMNS | |
| if column in df.columns | |
| ], | |
| errors="ignore" | |
| ) | |
| # ------------------------------------------------------------ | |
| # Ensure exact feature order | |
| # ------------------------------------------------------------ | |
| for column in feature_columns: | |
| if column not in X.columns: | |
| X[column] = np.nan | |
| X = X[ | |
| feature_columns | |
| ] | |
| # Take one real driver/lap row | |
| one_row = X.iloc[[0]].copy() | |
| print( | |
| f"\nOne-row shape: " | |
| f"{one_row.shape}" | |
| ) | |
| # ============================================================ | |
| # PREPROCESS ONCE | |
| # | |
| # This allows us to measure pure XGBoost inference separately. | |
| # ============================================================ | |
| one_row_processed = ( | |
| preprocessor.transform( | |
| one_row | |
| ) | |
| ) | |
| # ============================================================ | |
| # 1. XGBOOST-ONLY LATENCY | |
| # ============================================================ | |
| print("\n") | |
| print("=" * 80) | |
| print("1. XGBOOST INFERENCE ONLY") | |
| print("=" * 80) | |
| # Warm-up | |
| for _ in range(WARMUP_RUNS): | |
| model.predict_proba( | |
| one_row_processed | |
| ) | |
| latencies_ms = [] | |
| for _ in range(N_RUNS): | |
| start = time.perf_counter_ns() | |
| model.predict_proba( | |
| one_row_processed | |
| ) | |
| end = time.perf_counter_ns() | |
| latency = ( | |
| end - start | |
| ) / 1_000_000 | |
| latencies_ms.append( | |
| latency | |
| ) | |
| latencies_ms = np.array( | |
| latencies_ms | |
| ) | |
| print( | |
| f"Mean : " | |
| f"{latencies_ms.mean():.4f} ms" | |
| ) | |
| print( | |
| f"Min : " | |
| f"{latencies_ms.min():.4f} ms" | |
| ) | |
| print( | |
| f"Max : " | |
| f"{latencies_ms.max():.4f} ms" | |
| ) | |
| print( | |
| f"P50 : " | |
| f"{np.percentile(latencies_ms, 50):.4f} ms" | |
| ) | |
| print( | |
| f"P95 : " | |
| f"{np.percentile(latencies_ms, 95):.4f} ms" | |
| ) | |
| print( | |
| f"P99 : " | |
| f"{np.percentile(latencies_ms, 99):.4f} ms" | |
| ) | |
| # ============================================================ | |
| # 2. PREPROCESSING + XGBOOST | |
| # ============================================================ | |
| print("\n") | |
| print("=" * 80) | |
| print("2. PREPROCESSING + XGBOOST") | |
| print("=" * 80) | |
| # Warm-up | |
| for _ in range(WARMUP_RUNS): | |
| processed = ( | |
| preprocessor.transform( | |
| one_row | |
| ) | |
| ) | |
| model.predict_proba( | |
| processed | |
| ) | |
| latencies_ms = [] | |
| for _ in range(N_RUNS): | |
| start = time.perf_counter_ns() | |
| processed = ( | |
| preprocessor.transform( | |
| one_row | |
| ) | |
| ) | |
| model.predict_proba( | |
| processed | |
| ) | |
| end = time.perf_counter_ns() | |
| latency = ( | |
| end - start | |
| ) / 1_000_000 | |
| latencies_ms.append( | |
| latency | |
| ) | |
| latencies_ms = np.array( | |
| latencies_ms | |
| ) | |
| print( | |
| f"Mean : " | |
| f"{latencies_ms.mean():.4f} ms" | |
| ) | |
| print( | |
| f"Min : " | |
| f"{latencies_ms.min():.4f} ms" | |
| ) | |
| print( | |
| f"Max : " | |
| f"{latencies_ms.max():.4f} ms" | |
| ) | |
| print( | |
| f"P50 : " | |
| f"{np.percentile(latencies_ms, 50):.4f} ms" | |
| ) | |
| print( | |
| f"P95 : " | |
| f"{np.percentile(latencies_ms, 95):.4f} ms" | |
| ) | |
| print( | |
| f"P99 : " | |
| f"{np.percentile(latencies_ms, 99):.4f} ms" | |
| ) | |
| # ============================================================ | |
| # 3. 20-DRIVER BATCH LATENCY | |
| # | |
| # This is much more relevant to your actual Pit Lane system. | |
| # | |
| # We take 20 real rows and predict all 20 together. | |
| # ============================================================ | |
| print("\n") | |
| print("=" * 80) | |
| print("3. 20-DRIVER BATCH LATENCY") | |
| print("=" * 80) | |
| # ------------------------------------------------------------ | |
| # Take up to 20 real rows | |
| # ------------------------------------------------------------ | |
| batch_size = min( | |
| 20, | |
| len(X) | |
| ) | |
| batch = X.iloc[ | |
| :batch_size | |
| ].copy() | |
| # Warm-up | |
| for _ in range(WARMUP_RUNS): | |
| processed = ( | |
| preprocessor.transform( | |
| batch | |
| ) | |
| ) | |
| model.predict_proba( | |
| processed | |
| ) | |
| latencies_ms = [] | |
| for _ in range(N_RUNS): | |
| start = time.perf_counter_ns() | |
| processed = ( | |
| preprocessor.transform( | |
| batch | |
| ) | |
| ) | |
| model.predict_proba( | |
| processed | |
| ) | |
| end = time.perf_counter_ns() | |
| latency = ( | |
| end - start | |
| ) / 1_000_000 | |
| latencies_ms.append( | |
| latency | |
| ) | |
| latencies_ms = np.array( | |
| latencies_ms | |
| ) | |
| print( | |
| f"Drivers measured: " | |
| f"{batch_size}" | |
| ) | |
| print( | |
| f"Mean total : " | |
| f"{latencies_ms.mean():.4f} ms" | |
| ) | |
| print( | |
| f"P50 total : " | |
| f"{np.percentile(latencies_ms, 50):.4f} ms" | |
| ) | |
| print( | |
| f"P95 total : " | |
| f"{np.percentile(latencies_ms, 95):.4f} ms" | |
| ) | |
| print( | |
| f"P99 total : " | |
| f"{np.percentile(latencies_ms, 99):.4f} ms" | |
| ) | |
| print( | |
| f"\nApprox. per-driver mean: " | |
| f"{latencies_ms.mean() / batch_size:.4f} ms" | |
| ) | |
| # ============================================================ | |
| # 4. PREDICTION OUTPUT CHECK | |
| # ============================================================ | |
| print("\n") | |
| print("=" * 80) | |
| print("PREDICTION CHECK") | |
| print("=" * 80) | |
| processed = ( | |
| preprocessor.transform( | |
| batch | |
| ) | |
| ) | |
| probabilities = ( | |
| model.predict_proba( | |
| processed | |
| )[:, 1] | |
| ) | |
| print( | |
| "\nPredicted probabilities:" | |
| ) | |
| for i, probability in enumerate( | |
| probabilities | |
| ): | |
| print( | |
| f"Driver row {i + 1:02d}: " | |
| f"{probability * 100:.4f}%" | |
| ) | |
| # ============================================================ | |
| # 5. FINAL SUMMARY | |
| # ============================================================ | |
| print("\n") | |
| print("=" * 80) | |
| print("LATENCY BENCHMARK COMPLETE") | |
| print("=" * 80) | |
| print( | |
| "\nThese measurements exclude:" | |
| ) | |
| print( | |
| " - network latency" | |
| ) | |
| print( | |
| " - API/FastF1 latency" | |
| ) | |
| print( | |
| " - raw telemetry acquisition" | |
| ) | |
| print( | |
| " - raw telemetry → compressed-lap processing" | |
| ) | |
| print( | |
| "\nTherefore the 20-driver batch number is the " | |
| "model-side latency, NOT the total backend latency." | |
| ) |