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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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
"""
Load Testing Framework for QuantLib Pro

Simulates concurrent users and measures system performance under load.

Features:
- Concurrent user simulation (50+ users)
- Request rate control
- Performance metric collection
- Bottleneck identification
- Load test scenarios

Example
-------
>>> from quantlib_pro.testing.load_testing import LoadTester, Scenario
>>>
>>> tester = LoadTester(base_url='http://localhost:8000')
>>> scenario = Scenario(name='Portfolio Optimization', endpoint='/api/portfolio/optimize')
>>> results = tester.run_load_test(scenario, users=50, duration=60)
>>> print(results.summary())
"""

from __future__ import annotations

import logging
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Callable, Dict, List, Optional

import numpy as np
import pandas as pd

log = logging.getLogger(__name__)


class LoadPattern(Enum):
    """Load test patterns."""
    CONSTANT = 'constant'  # Constant user count
    RAMP_UP = 'ramp_up'  # Gradually increase users
    SPIKE = 'spike'  # Sudden traffic spike
    WAVE = 'wave'  # Oscillating load


@dataclass
class Request:
    """Single request in load test."""
    scenario_name: str
    start_time: float
    end_time: float
    success: bool
    status_code: Optional[int] = None
    error: Optional[str] = None
    response_size: int = 0
    
    @property
    def duration(self) -> float:
        """Request duration in milliseconds."""
        return (self.end_time - self.start_time) * 1000


@dataclass
class LoadTestResult:
    """Load test results."""
    scenario_name: str
    total_requests: int
    successful_requests: int
    failed_requests: int
    duration_seconds: float
    requests_per_second: float
    response_times: List[float]
    errors: List[str] = field(default_factory=list)
    
    @property
    def success_rate(self) -> float:
        """Success rate as percentage."""
        return (self.successful_requests / self.total_requests * 100) if self.total_requests > 0 else 0
    
    @property
    def mean_response_time(self) -> float:
        """Mean response time in milliseconds."""
        return np.mean(self.response_times) if self.response_times else 0
    
    @property
    def median_response_time(self) -> float:
        """Median response time in milliseconds."""
        return np.median(self.response_times) if self.response_times else 0
    
    @property
    def p95_response_time(self) -> float:
        """95th percentile response time in milliseconds."""
        return np.percentile(self.response_times, 95) if self.response_times else 0
    
    @property
    def p99_response_time(self) -> float:
        """99th percentile response time in milliseconds."""
        return np.percentile(self.response_times, 99) if self.response_times else 0
    
    def summary(self) -> str:
        """Generate summary report."""
        return f"""
Load Test Results: {self.scenario_name}
{'=' * 70}
Total Requests:        {self.total_requests:,}
Successful:            {self.successful_requests:,} ({self.success_rate:.1f}%)
Failed:                {self.failed_requests:,}
Duration:              {self.duration_seconds:.2f}s
Throughput:            {self.requests_per_second:.2f} req/s

Response Times (ms):
  Mean:                {self.mean_response_time:.2f}
  Median:              {self.median_response_time:.2f}
  95th Percentile:     {self.p95_response_time:.2f}
  99th Percentile:     {self.p99_response_time:.2f}
  Min:                 {min(self.response_times) if self.response_times else 0:.2f}
  Max:                 {max(self.response_times) if self.response_times else 0:.2f}

Status:                {'βœ… PASS' if self.success_rate > 95 and self.p95_response_time < 1000 else '❌ FAIL'}
"""


@dataclass
class LoadTestScenario:
    """Load test scenario definition."""
    name: str
    test_function: Callable
    weight: float = 1.0  # Probability weight
    think_time: float = 0.0  # Seconds between requests


class LoadTester:
    """
    Load testing framework.
    
    Simulates concurrent users executing scenarios and measures performance.
    
    Parameters
    ----------
    max_workers : int
        Maximum concurrent users
    ramp_up_time : float
        Time to ramp up to max users (seconds)
    
    Example
    -------
    >>> def portfolio_optimization():
    >>>     # Your test logic here
    >>>     time.sleep(0.1)  # Simulate work
    >>>     return True
    >>>
    >>> tester = LoadTester()
    >>> scenario = LoadTestScenario('Portfolio Opt', portfolio_optimization)
    >>> results = tester.run_load_test([scenario], users=50, duration=60)
    """
    
    def __init__(
        self,
        max_workers: int = 100,
        ramp_up_time: float = 10.0,
    ):
        self.max_workers = max_workers
        self.ramp_up_time = ramp_up_time
        self.requests: List[Request] = []
        
        log.info(f"Initialized LoadTester: max_workers={max_workers}")
    
    def run_load_test(
        self,
        scenarios: List[LoadTestScenario],
        users: int = 50,
        duration: float = 60.0,
        pattern: LoadPattern = LoadPattern.RAMP_UP,
    ) -> Dict[str, LoadTestResult]:
        """
        Run load test with specified scenarios.
        
        Parameters
        ----------
        scenarios : list of LoadTestScenario
            Test scenarios to execute
        users : int
            Number of concurrent users
        duration : float
            Test duration in seconds
        pattern : LoadPattern
            Load pattern
        
        Returns
        -------
        dict
            Results for each scenario
        """
        log.info(f"Starting load test: {users} users, {duration}s, pattern={pattern.value}")
        
        self.requests = []
        start_time = time.time()
        end_time = start_time + duration
        
        # Calculate user schedule based on pattern
        user_schedule = self._generate_user_schedule(users, duration, pattern)
        
        # Execute load test
        with ThreadPoolExecutor(max_workers=min(users, self.max_workers)) as executor:
            futures = []
            
            for user_id, spawn_time in enumerate(user_schedule):
                # Wait until spawn time
                wait_time = spawn_time - (time.time() - start_time)
                if wait_time > 0:
                    time.sleep(wait_time)
                
                # Submit user task
                future = executor.submit(
                    self._user_session,
                    user_id,
                    scenarios,
                    end_time
                )
                futures.append(future)
            
            # Wait for all users to complete
            for future in as_completed(futures):
                try:
                    future.result()
                except Exception as e:
                    log.error(f"User session error: {e}")
        
        # Process results
        actual_duration = time.time() - start_time
        
        return self._generate_results(scenarios, actual_duration)
    
    def _generate_user_schedule(
        self,
        users: int,
        duration: float,
        pattern: LoadPattern,
    ) -> List[float]:
        """Generate user spawn schedule based on load pattern."""
        if pattern == LoadPattern.CONSTANT:
            # Spawn all users immediately
            return [0.0] * users
        
        elif pattern == LoadPattern.RAMP_UP:
            # Linear ramp up
            ramp_duration = min(self.ramp_up_time, duration / 2)
            return [i * ramp_duration / users for i in range(users)]
        
        elif pattern == LoadPattern.SPIKE:
            # Spawn half at start, spike rest at midpoint
            half = users // 2
            return ([0.0] * half) + ([duration / 2] * (users - half))
        
        elif pattern == LoadPattern.WAVE:
            # Sinusoidal spawn pattern
            return [
                abs(np.sin(2 * np.pi * i / users)) * duration / 2
                for i in range(users)
            ]
        
        return [0.0] * users
    
    def _user_session(
        self,
        user_id: int,
        scenarios: List[LoadTestScenario],
        end_time: float,
    ) -> None:
        """Simulate single user session."""
        # Calculate scenario probabilities
        total_weight = sum(s.weight for s in scenarios)
        probabilities = [s.weight / total_weight for s in scenarios]
        
        while time.time() < end_time:
            # Select scenario based on weights
            scenario = np.random.choice(scenarios, p=probabilities)
            
            # Execute request
            request = self._execute_request(scenario)
            self.requests.append(request)
            
            # Think time
            if scenario.think_time > 0:
                time.sleep(scenario.think_time)
    
    def _execute_request(self, scenario: LoadTestScenario) -> Request:
        """Execute single request."""
        start_time = time.time()
        success = False
        error = None
        
        try:
            # Execute test function
            result = scenario.test_function()
            success = bool(result)
        
        except Exception as e:
            error = str(e)
            log.debug(f"Request failed: {e}")
        
        end_time = time.time()
        
        return Request(
            scenario_name=scenario.name,
            start_time=start_time,
            end_time=end_time,
            success=success,
            error=error,
        )
    
    def _generate_results(
        self,
        scenarios: List[LoadTestScenario],
        duration: float,
    ) -> Dict[str, LoadTestResult]:
        """Generate results for each scenario."""
        results = {}
        
        for scenario in scenarios:
            # Filter requests for this scenario
            scenario_requests = [
                r for r in self.requests
                if r.scenario_name == scenario.name
            ]
            
            total = len(scenario_requests)
            successful = sum(1 for r in scenario_requests if r.success)
            failed = total - successful
            
            response_times = [r.duration for r in scenario_requests]
            errors = [r.error for r in scenario_requests if r.error]
            
            rps = total / duration if duration > 0 else 0
            
            results[scenario.name] = LoadTestResult(
                scenario_name=scenario.name,
                total_requests=total,
                successful_requests=successful,
                failed_requests=failed,
                duration_seconds=duration,
                requests_per_second=rps,
                response_times=response_times,
                errors=errors[:10],  # Keep first 10 errors
            )
        
        return results
    
    def generate_report(self, results: Dict[str, LoadTestResult]) -> pd.DataFrame:
        """Generate DataFrame report."""
        data = []
        
        for name, result in results.items():
            data.append({
                'Scenario': name,
                'Total Requests': result.total_requests,
                'Success Rate (%)': result.success_rate,
                'RPS': result.requests_per_second,
                'Mean (ms)': result.mean_response_time,
                'Median (ms)': result.median_response_time,
                'P95 (ms)': result.p95_response_time,
                'P99 (ms)': result.p99_response_time,
            })
        
        return pd.DataFrame(data)


class PerformanceBenchmark:
    """
    Performance benchmarking suite.
    
    Defines performance targets and validates against them.
    """
    
    def __init__(self):
        self.benchmarks: Dict[str, Dict[str, float]] = {
            'portfolio_optimization': {
                'max_p95_ms': 500,  # 95th percentile < 500ms
                'max_p99_ms': 1000,  # 99th percentile < 1s
                'min_rps': 10,  # At least 10 req/s
                'min_success_rate': 99.0,  # 99% success
            },
            'var_calculation': {
                'max_p95_ms': 100,
                'max_p99_ms': 200,
                'min_rps': 50,
                'min_success_rate': 99.5,
            },
            'options_pricing': {
                'max_p95_ms': 50,
                'max_p99_ms': 100,
                'min_rps': 100,
                'min_success_rate': 99.9,
            },
            'regime_detection': {
                'max_p95_ms': 200,
                'max_p99_ms': 500,
                'min_rps': 20,
                'min_success_rate': 98.0,
            },
        }
    
    def validate(
        self,
        scenario_name: str,
        result: LoadTestResult,
    ) -> Dict[str, bool]:
        """
        Validate results against benchmark.
        
        Parameters
        ----------
        scenario_name : str
            Scenario name
        result : LoadTestResult
            Test results
        
        Returns
        -------
        dict
            Validation results for each metric
        """
        if scenario_name not in self.benchmarks:
            log.warning(f"No benchmark defined for {scenario_name}")
            return {}
        
        benchmark = self.benchmarks[scenario_name]
        validation = {}
        
        # Check P95 latency
        if 'max_p95_ms' in benchmark:
            validation['p95_latency'] = result.p95_response_time <= benchmark['max_p95_ms']
        
        # Check P99 latency
        if 'max_p99_ms' in benchmark:
            validation['p99_latency'] = result.p99_response_time <= benchmark['max_p99_ms']
        
        # Check throughput
        if 'min_rps' in benchmark:
            validation['throughput'] = result.requests_per_second >= benchmark['min_rps']
        
        # Check success rate
        if 'min_success_rate' in benchmark:
            validation['success_rate'] = result.success_rate >= benchmark['min_success_rate']
        
        return validation
    
    def generate_validation_report(
        self,
        scenario_name: str,
        result: LoadTestResult,
    ) -> str:
        """Generate validation report."""
        validation = self.validate(scenario_name, result)
        
        if not validation:
            return "No benchmark defined"
        
        lines = [
            f"Benchmark Validation: {scenario_name}",
            "=" * 50,
        ]
        
        benchmark = self.benchmarks[scenario_name]
        
        for metric, passed in validation.items():
            status = "βœ… PASS" if passed else "❌ FAIL"
            
            if metric == 'p95_latency':
                lines.append(f"P95 Latency: {result.p95_response_time:.2f}ms / {benchmark['max_p95_ms']}ms {status}")
            elif metric == 'p99_latency':
                lines.append(f"P99 Latency: {result.p99_response_time:.2f}ms / {benchmark['max_p99_ms']}ms {status}")
            elif metric == 'throughput':
                lines.append(f"Throughput: {result.requests_per_second:.2f} / {benchmark['min_rps']} req/s {status}")
            elif metric == 'success_rate':
                lines.append(f"Success Rate: {result.success_rate:.1f}% / {benchmark['min_success_rate']}% {status}")
        
        overall = "βœ… ALL PASSED" if all(validation.values()) else "❌ SOME FAILED"
        lines.append(f"\nOverall: {overall}")
        
        return '\n'.join(lines)