File size: 4,407 Bytes
24480a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
backend/api/advanced_complex_benchmark.py — Stress test per task complessi e concorrenza.
"""
import asyncio
import time
import random
import statistics
from .execution_fabric import ExecutionFabric, DispatchRequest, ProviderKind, ProviderSpec, AlwaysOn, ProviderHealth
from .token_rotator import rotator

class AdvancedBenchmark:
    def __init__(self):
        self.fabric = ExecutionFabric()
        self.metrics = {
            "complex_reasoning": [],
            "data_heavy_sync": [],
            "multi_provider_chain": [],
            "failures": 0,
            "successes": 0
        }

    async def setup(self):
        # Configurazione flotta
        for char in ['a', 'b', 'c', 'd']:
            self.fabric._specs[f"sb-{char}"] = ProviderSpec(
                provider_id=f"sb-{char}", name=f"Supabase {char.upper()}", kind=ProviderKind.LOCAL,
                capabilities=["memory"], always_on=AlwaysOn.YES
            )
            self.fabric._states[f"sb-{char}"] = type('State', (), {"health": ProviderHealth.OK})()
        
        self.fabric._specs["oracle-core"] = ProviderSpec(
            provider_id="oracle-core", name="Oracle Core", kind=ProviderKind.ORACLE,
            capabilities=["reasoning", "sandbox"], always_on=AlwaysOn.YES, timeout=60.0
        )
        self.fabric._states["oracle-core"] = type('State', (), {"health": ProviderHealth.OK})()

        self.fabric._specs["railway-core"] = ProviderSpec(
            provider_id="railway-core", name="Railway Space E", kind=ProviderKind.RAILWAY,
            capabilities=["reasoning", "sandbox"], always_on=AlwaysOn.YES
        )
        self.fabric._states["railway-core"] = type('State', (), {"health": ProviderHealth.OK})()
        
        self.fabric._initialized = True

    async def _simulate_call(self, spec, req):
        # Simula complessità variabile
        if "reasoning" in spec.capabilities:
            await asyncio.sleep(random.uniform(0.5, 2.0)) # Calcolo pesante
        if random.random() < 0.05: # 5% probabilità di errore casuale
            raise Exception("Random Provider Glitch")
        if spec.provider_id == "sb-a" and random.random() < 0.3:
            return {"status_code": 402} # 30% probabilità rate limit su A
        return {"status": "ok", "provider": spec.name}

    async def run_complex_task(self, name, capability, count=20):
        print(f"⚙️ Esecuzione: {name} ({count} task)...")
        tasks = []
        for _ in range(count):
            req = DispatchRequest(capability=capability, payload={"complexity": "high"})
            tasks.append(self.fabric.dispatch(req))
        
        t0 = time.time()
        results = await asyncio.gather(*tasks)
        duration = (time.time() - t0) * 1000
        
        latencies = []
        for r in results:
            if r.status == "executed":
                self.metrics["successes"] += 1
                latencies.append(r.latency_ms)
            else:
                self.metrics["failures"] += 1
        
        self.metrics[name] = latencies

    def print_stats(self):
        print("\n" + "="*50)
        print("📊 REPORT AVANZATO PRESTAZIONI SISTEMA")
        print("="*50)
        
        total = self.metrics["successes"] + self.metrics["failures"]
        print(f"Success Rate Totale: {(self.metrics['successes']/total)*100:.2f}% ({self.metrics['successes']}/{total})")
        
        for name in ["complex_reasoning", "data_heavy_sync"]:
            data = self.metrics.get(name, [])
            if data:
                print(f"\n[{name.upper()}]")
                print(f"  - Media Latenza: {statistics.mean(data):.2f}ms")
                print(f"  - P95: {statistics.quantiles(data, n=20)[18]:.2f}ms")
                print(f"  - P99: {max(data):.2f}ms")
                print(f"  - Efficienza: {len(data)} task completati con successo")
        
        print("\n" + "="*50)

    async def run(self):
        await self.setup()
        self.fabric._call_provider = self._simulate_call
        
        # Scenario 1: Ragionamento Complesso (Oracle/Railway)
        await self.run_complex_task("complex_reasoning", "reasoning", count=30)
        
        # Scenario 2: Sincronizzazione Dati (Supabase A-D)
        await self.run_complex_task("data_heavy_sync", "memory", count=50)
        
        self.print_stats()

if __name__ == "__main__":
    asyncio.run(AdvancedBenchmark().run())