""" backend/api/fabric_benchmark.py — Benchmark simulato per rotazione token e fallback. """ import asyncio import time import logging from typing import Dict, Any from .execution_fabric import ExecutionFabric, DispatchRequest, ProviderKind, ProviderSpec, AlwaysOn, ProviderHealth from .token_rotator import rotator _logger = logging.getLogger("api.fabric_benchmark") class FabricBenchmark: def __init__(self): self.fabric = ExecutionFabric() self.stats = { "rotations": 0, "fallbacks": 0, "latencies": [], "errors": [] } async def setup_simulated_environment(self): """Configura un ambiente di test con provider simulati.""" # Provider Supabase A (Quota superata) self.fabric._specs["supabase-a"] = ProviderSpec( provider_id="supabase-a", name="Supabase A", kind=ProviderKind.LOCAL, capabilities=["storage"], always_on=AlwaysOn.YES ) self.fabric._states["supabase-a"] = type('State', (), {"health": ProviderHealth.OK})() # Provider Oracle (Timeout) self.fabric._specs["oracle-core"] = ProviderSpec( provider_id="oracle-core", name="Oracle Core", kind=ProviderKind.ORACLE, capabilities=["compute"], always_on=AlwaysOn.YES ) self.fabric._states["oracle-core"] = type('State', (), {"health": ProviderHealth.OK})() # Provider Railway (Fallback di Oracle) self.fabric._specs["railway-core"] = ProviderSpec( provider_id="railway-core", name="Railway Space E", kind=ProviderKind.RAILWAY, capabilities=["compute"], always_on=AlwaysOn.YES ) self.fabric._states["railway-core"] = type('State', (), {"health": ProviderHealth.OK})() self.fabric._initialized = True async def run_benchmark(self): print("--- Inizio Benchmark Simulato (No Token) ---") await self.setup_simulated_environment() # Test 1: Simulazione Rotazione Token (A -> B) # Sovrascriviamo _call_provider per simulare 402 su Supabase A async def mock_call_provider(spec, req): if spec.provider_id == "supabase-a": return {"status_code": 402, "error": "Quota exceeded"} return {"status": "ok", "provider": spec.name} self.fabric._call_provider = mock_call_provider t0 = time.time() req = DispatchRequest(capability="storage", provider_hint="supabase-a") result = await self.fabric.dispatch(req) duration = (time.time() - t0) * 1000 print(f"[Rotazione] Status: {result.status}, Durata: {duration:.2f}ms") if result.status == "executed" or "failed": # In questo mock fallirà dopo 4 tentativi se non cambiamo hint print("Nota: La rotazione è stata innescata internamente.") # Test 2: Simulazione Fallback Oracle -> Railway async def mock_call_oracle_fail(spec, req): if spec.kind == ProviderKind.ORACLE: raise Exception("Oracle Timeout") return {"status": "ok", "provider": spec.name} self.fabric._call_provider = mock_call_oracle_fail t0 = time.time() req = DispatchRequest(capability="compute", provider_hint="oracle-core") result = await self.fabric.dispatch(req) duration = (time.time() - t0) * 1000 print(f"[Fallback] Eseguito da: {result.provider_name}, Status: {result.status}, Durata: {duration:.2f}ms") print("--- Benchmark Completato ---") async def run(): bench = FabricBenchmark() await bench.run_benchmark() if __name__ == "__main__": asyncio.run(run())