File size: 15,792 Bytes
4ae946d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
#!/usr/bin/env python3
"""
Performance Testing Script
Tests the performance improvements from Phase 1 optimization
"""

import asyncio
import json
import statistics
import time
from typing import Any, Dict

import httpx


class PerformanceTester:
    """Comprehensive performance testing suite"""

    def __init__(self, base_url: str = "http://localhost:8000"):
        self.base_url = base_url
        self.results = {}

    async def run_full_performance_test(self) -> Dict[str, Any]:
        """Run comprehensive performance tests"""

        print("πŸš€ Starting Comprehensive Performance Test Suite")
        print("=" * 60)

        # Test 1: Health endpoint performance
        print("\nπŸ“Š Testing Health Endpoint Performance...")
        health_results = await self.test_health_endpoint()
        self.results["health_endpoint"] = health_results

        # Test 2: Database query performance
        print("\nπŸ’Ύ Testing Database Query Performance...")
        db_results = await self.test_database_performance()
        self.results["database_queries"] = db_results

        # Test 3: API caching effectiveness
        print("\n⚑ Testing API Caching Performance...")
        cache_results = await self.test_api_caching()
        self.results["api_caching"] = cache_results

        # Test 4: Concurrent load testing
        print("\nπŸ”₯ Testing Concurrent Load Handling...")
        load_results = await self.test_concurrent_load()
        self.results["concurrent_load"] = load_results

        # Test 5: Memory usage analysis
        print("\n🧠 Testing Memory Usage Patterns...")
        memory_results = await self.test_memory_usage()
        self.results["memory_usage"] = memory_results

        # Generate comprehensive report
        report = self.generate_performance_report()

        print("\n" + "=" * 60)
        print("βœ… Performance Testing Complete!")
        print("=" * 60)

        return report

    async def test_health_endpoint(self) -> Dict[str, Any]:
        """Test health endpoint performance"""

        response_times = []

        async with httpx.AsyncClient() as client:
            for i in range(100):
                start_time = time.time()
                try:
                    response = await client.get(f"{self.base_url}/health")
                    end_time = time.time()

                    if response.status_code == 200:
                        response_times.append(end_time - start_time)
                    else:
                        print(f"  ⚠️ Health check failed: {response.status_code}")

                except Exception as e:
                    print(f"  ❌ Health check error: {e}")

        if response_times:
            return {
                "sample_size": len(response_times),
                "avg_response_time": round(statistics.mean(response_times), 4),
                "median_response_time": round(statistics.median(response_times), 4),
                "p95_response_time": round(sorted(response_times)[int(len(response_times) * 0.95)], 4),
                "min_response_time": round(min(response_times), 4),
                "max_response_time": round(max(response_times), 4),
                "requests_per_second": round(len(response_times) / sum(response_times), 2),
            }
        else:
            return {"error": "No successful health checks"}

    async def test_database_performance(self) -> Dict[str, Any]:
        """Test database query performance"""

        # This would require authenticated requests
        # For now, we'll test what we can with public endpoints

        try:
            async with httpx.AsyncClient() as client:
                # Test health endpoint that includes database check
                start_time = time.time()
                response = await client.get(f"{self.base_url}/health/detailed")
                end_time = time.time()

                if response.status_code == 200:
                    data = response.json()
                    db_check = data.get("checks", {}).get("database", {})

                    return {
                        "endpoint_response_time": round(end_time - start_time, 4),
                        "database_status": db_check.get("status"),
                        "database_response_time": db_check.get("response_time_seconds"),
                        "query_time": db_check.get("query_time_seconds"),
                        "record_count": db_check.get("case_count"),
                    }
                else:
                    return {"error": f"Health check failed: {response.status_code}"}

        except Exception as e:
            return {"error": str(e)}

    async def test_api_caching(self) -> Dict[str, Any]:
        """Test API caching effectiveness"""

        cache_test_results = {
            "first_request_time": None,
            "cached_request_time": None,
            "cache_hit_ratio": None,
            "performance_improvement": None,
        }

        try:
            async with httpx.AsyncClient() as client:
                # First request (should cache)
                start_time = time.time()
                response1 = await client.get(f"{self.base_url}/health/detailed")
                first_request_time = time.time() - start_time

                if response1.status_code == 200:
                    cache_test_results["first_request_time"] = round(first_request_time, 4)

                    # Immediate second request (should hit cache if implemented)
                    start_time = time.time()
                    response2 = await client.get(f"{self.base_url}/health/detailed")
                    second_request_time = time.time() - start_time

                    if response2.status_code == 200:
                        cache_test_results["cached_request_time"] = round(second_request_time, 4)

                        # Calculate improvement
                        if first_request_time > 0 and second_request_time > 0:
                            improvement = ((first_request_time - second_request_time) / first_request_time) * 100
                            cache_test_results["performance_improvement"] = round(improvement, 2)

                            # Rough cache hit detection (significant improvement indicates caching)
                            if improvement > 20:  # 20%+ improvement suggests caching
                                cache_test_results["cache_hit_ratio"] = "Likely cached"
                            else:
                                cache_test_results["cache_hit_ratio"] = "Possibly not cached"

        except Exception as e:
            cache_test_results["error"] = str(e)

        return cache_test_results

    async def test_concurrent_load(self) -> Dict[str, Any]:
        """Test system under concurrent load"""

        async def make_request(request_id: int) -> Dict[str, Any]:
            """Make a single request and return timing data"""
            async with httpx.AsyncClient() as client:
                start_time = time.time()
                try:
                    response = await client.get(f"{self.base_url}/health")
                    end_time = time.time()

                    return {
                        "request_id": request_id,
                        "response_time": end_time - start_time,
                        "status_code": response.status_code,
                        "success": response.status_code == 200,
                    }
                except Exception as e:
                    end_time = time.time()
                    return {
                        "request_id": request_id,
                        "response_time": end_time - start_time,
                        "status_code": None,
                        "success": False,
                        "error": str(e),
                    }

        # Run 50 concurrent requests
        print("  πŸ“ˆ Running 50 concurrent requests...")
        tasks = [make_request(i) for i in range(50)]
        results = await asyncio.gather(*tasks, return_exceptions=True)

        # Process results
        successful_requests = []
        failed_requests = []

        for result in results:
            if isinstance(result, dict):
                if result.get("success"):
                    successful_requests.append(result)
                else:
                    failed_requests.append(result)

        response_times = [r["response_time"] for r in successful_requests]

        concurrent_results = {
            "total_requests": len(results),
            "successful_requests": len(successful_requests),
            "failed_requests": len(failed_requests),
            "success_rate": round(len(successful_requests) / len(results) * 100, 2) if results else 0,
        }

        if response_times:
            concurrent_results.update(
                {
                    "avg_response_time": round(statistics.mean(response_times), 4),
                    "median_response_time": round(statistics.median(response_times), 4),
                    "p95_response_time": round(sorted(response_times)[int(len(response_times) * 0.95)], 4),
                    "min_response_time": round(min(response_times), 4),
                    "max_response_time": round(max(response_times), 4),
                }
            )

            # Calculate throughput
            total_time = max(r["response_time"] for r in successful_requests)
            concurrent_results["requests_per_second"] = round(len(successful_requests) / total_time, 2)

        return concurrent_results

    async def test_memory_usage(self) -> Dict[str, Any]:
        """Test memory usage patterns under load"""

        try:
            import os

            import psutil

            process = psutil.Process(os.getpid())

            # Get baseline memory
            baseline_memory = process.memory_info().rss / 1024 / 1024  # MB

            # Generate some load
            print("  πŸ”„ Generating load for memory testing...")

            async with httpx.AsyncClient() as client:
                # Make 200 requests over 10 seconds
                tasks = []
                for i in range(200):
                    task = client.get(f"{self.base_url}/health")
                    tasks.append(task)

                    # Small batch to avoid overwhelming
                    if len(tasks) >= 20:
                        await asyncio.gather(*tasks[:20])
                        tasks = tasks[20:]

                        # Check memory midway
                        current_memory = process.memory_info().rss / 1024 / 1024
                        memory_increase = current_memory - baseline_memory

                        if memory_increase > 50:  # 50MB increase
                            print(f"  ⚠️ Significant memory increase detected: +{memory_increase:.1f}MB")

                # Complete remaining tasks
                if tasks:
                    await asyncio.gather(*tasks)

            # Final memory check
            final_memory = process.memory_info().rss / 1024 / 1024
            total_increase = final_memory - baseline_memory

            return {
                "baseline_memory_mb": round(baseline_memory, 2),
                "final_memory_mb": round(final_memory, 2),
                "total_increase_mb": round(total_increase, 2),
                "memory_leak_detected": total_increase > 20,  # 20MB+ increase suggests leak
                "assessment": "Good" if total_increase < 20 else "Needs investigation",
            }

        except ImportError:
            return {"error": "psutil not available for memory testing"}
        except Exception as e:
            return {"error": str(e)}

    def generate_performance_report(self) -> Dict[str, Any]:
        """Generate comprehensive performance report"""

        report = {
            "test_timestamp": time.time(),
            "test_duration_seconds": time.time() - getattr(self, "start_time", time.time()),
            "summary": {},
            "recommendations": [],
            "raw_results": self.results,
        }

        # Calculate summary metrics
        health_results = self.results.get("health_endpoint", {})
        concurrent_results = self.results.get("concurrent_load", {})
        cache_results = self.results.get("api_caching", {})

        report["summary"] = {
            "health_endpoint_avg_ms": health_results.get("avg_response_time", 0) * 1000,
            "health_endpoint_p95_ms": health_results.get("p95_response_time", 0) * 1000,
            "concurrent_success_rate": concurrent_results.get("success_rate", 0),
            "concurrent_rps": concurrent_results.get("requests_per_second", 0),
            "cache_performance_improvement": cache_results.get("performance_improvement", 0),
        }

        # Generate recommendations
        recommendations = []

        # Health endpoint performance
        if health_results.get("avg_response_time", 1) > 0.5:  # >500ms
            recommendations.append(
                "⚠️ Health endpoint response time is slow (>500ms). Consider optimizing database queries."
            )

        # Concurrent performance
        success_rate = concurrent_results.get("success_rate", 0)
        if success_rate < 95:
            recommendations.append(
                f"⚠️ Concurrent load success rate is low ({success_rate}%). Consider implementing rate limiting or load balancing."
            )

        if concurrent_results.get("requests_per_second", 0) < 100:
            recommendations.append(
                "⚠️ Low throughput under concurrent load. Consider optimizing database connections and caching."
            )

        # Caching effectiveness
        cache_improvement = cache_results.get("performance_improvement", 0)
        if cache_improvement < 10:
            recommendations.append(
                "⚠️ Limited caching effectiveness detected. Consider implementing Redis caching for frequently accessed data."
            )

        # Memory usage
        memory_results = self.results.get("memory_usage", {})
        if memory_results.get("memory_leak_detected"):
            recommendations.append(
                "🚨 Potential memory leak detected. Investigate memory usage patterns and implement proper cleanup."
            )

        report["recommendations"] = recommendations if recommendations else ["βœ… All performance metrics look good!"]

        return report


async def main():
    """Main performance testing function"""

    print("🎯 Zenith Backend Performance Testing Suite")
    print("=" * 60)

    # Initialize tester
    tester = PerformanceTester()

    try:
        # Run full test suite
        report = await tester.run_full_performance_test()

        # Display results
        print("\nπŸ“‹ PERFORMANCE TEST RESULTS")
        print("-" * 40)

        report.get("summary", {})
        print(".2f")
        print(".2f")
        print(".1f")
        print(".1f")
        print(".1f")

        recommendations = report.get("recommendations", [])
        if recommendations:
            print(f"\nπŸ’‘ RECOMMENDATIONS ({len(recommendations)})")
            print("-" * 40)
            for rec in recommendations:
                print(f"β€’ {rec}")

        print("\nπŸŽ‰ Performance testing complete! Check the results above for optimization opportunities.")
        # Save detailed report
        with open("performance_test_results.json", "w") as f:
            json.dump(report, f, indent=2, default=str)
        print("πŸ“„ Detailed results saved to: performance_test_results.json")

    except Exception as e:
        print(f"❌ Performance testing failed: {e}")
        import traceback

        traceback.print_exc()


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