-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathperformance_test.py
More file actions
205 lines (177 loc) · 7.91 KB
/
performance_test.py
File metadata and controls
205 lines (177 loc) · 7.91 KB
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
#!/usr/bin/env python3
"""
Simple performance test script for comparing Django dev server vs Gunicorn.
Tests common endpoints with concurrent requests.
"""
import time
import statistics
import requests
import concurrent.futures
from typing import List, Dict, Tuple
import argparse
import sys
def test_endpoint(url: str, timeout: int = 30) -> Tuple[float, int, bool]:
"""
Test a single request to an endpoint.
Returns: (response_time, status_code, success)
"""
try:
start = time.time()
response = requests.get(url, timeout=timeout)
elapsed = time.time() - start
return (elapsed, response.status_code, True)
except Exception as e:
print(f"Error: {e}")
return (timeout, 0, False)
def run_concurrent_tests(url: str, num_requests: int, num_workers: int) -> List[Tuple[float, int, bool]]:
"""
Run concurrent requests to the endpoint.
"""
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
futures = [executor.submit(test_endpoint, url) for _ in range(num_requests)]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
return results
def analyze_results(results: List[Tuple[float, int, bool]]) -> Dict:
"""
Analyze the test results.
"""
successful = [r[0] for r in results if r[2]]
failed = len([r for r in results if not r[2]])
if not successful:
return {
'total_requests': len(results),
'successful': 0,
'failed': failed,
'success_rate': 0.0,
'min_time': 0,
'max_time': 0,
'mean_time': 0,
'median_time': 0,
'p95_time': 0,
'p99_time': 0,
}
successful_sorted = sorted(successful)
return {
'total_requests': len(results),
'successful': len(successful),
'failed': failed,
'success_rate': (len(successful) / len(results)) * 100,
'min_time': min(successful),
'max_time': max(successful),
'mean_time': statistics.mean(successful),
'median_time': statistics.median(successful),
'p95_time': successful_sorted[int(len(successful_sorted) * 0.95)] if len(successful_sorted) > 1 else successful[0],
'p99_time': successful_sorted[int(len(successful_sorted) * 0.99)] if len(successful_sorted) > 1 else successful[0],
}
def print_results(server_name: str, results: Dict):
"""
Pretty print the test results.
"""
print(f"\n{'='*60}")
print(f" {server_name} Performance Results")
print(f"{'='*60}")
print(f"Total Requests: {results['total_requests']}")
print(f"Successful: {results['successful']}")
print(f"Failed: {results['failed']}")
print(f"Success Rate: {results['success_rate']:.1f}%")
print(f"\nResponse Times (seconds):")
print(f" Min: {results['min_time']:.3f}s")
print(f" Max: {results['max_time']:.3f}s")
print(f" Mean: {results['mean_time']:.3f}s")
print(f" Median: {results['median_time']:.3f}s")
print(f" 95th percentile: {results['p95_time']:.3f}s")
print(f" 99th percentile: {results['p99_time']:.3f}s")
# Calculate requests per second
if results['mean_time'] > 0:
rps = 1 / results['mean_time']
print(f"\nThroughput: {rps:.2f} requests/second (single thread)")
print(f"{'='*60}\n")
def compare_results(dev_results: Dict, gunicorn_results: Dict):
"""
Compare and show improvement from dev to gunicorn.
"""
print(f"\n{'='*60}")
print(f" Performance Comparison")
print(f"{'='*60}")
if dev_results['mean_time'] > 0 and gunicorn_results['mean_time'] > 0:
speedup = dev_results['mean_time'] / gunicorn_results['mean_time']
print(f"Mean Response Time:")
print(f" Dev Server: {dev_results['mean_time']:.3f}s")
print(f" Gunicorn: {gunicorn_results['mean_time']:.3f}s")
print(f" Improvement: {speedup:.2f}x {'faster' if speedup > 1 else 'slower'}")
print(f"\nMedian Response Time:")
speedup_median = dev_results['median_time'] / gunicorn_results['median_time']
print(f" Dev Server: {dev_results['median_time']:.3f}s")
print(f" Gunicorn: {gunicorn_results['median_time']:.3f}s")
print(f" Improvement: {speedup_median:.2f}x {'faster' if speedup_median > 1 else 'slower'}")
print(f"\n95th Percentile Response Time:")
speedup_p95 = dev_results['p95_time'] / gunicorn_results['p95_time']
print(f" Dev Server: {dev_results['p95_time']:.3f}s")
print(f" Gunicorn: {gunicorn_results['p95_time']:.3f}s")
print(f" Improvement: {speedup_p95:.2f}x {'faster' if speedup_p95 > 1 else 'slower'}")
print(f"\nSuccess Rate:")
print(f" Dev Server: {dev_results['success_rate']:.1f}%")
print(f" Gunicorn: {gunicorn_results['success_rate']:.1f}%")
print(f"{'='*60}\n")
def main():
parser = argparse.ArgumentParser(description='Test Django server performance')
parser.add_argument('--url', default='http://localhost:8000/',
help='URL to test (default: http://localhost:8000/)')
parser.add_argument('--requests', type=int, default=100,
help='Number of requests to send (default: 100)')
parser.add_argument('--concurrency', type=int, default=10,
help='Number of concurrent requests (default: 10)')
parser.add_argument('--warmup', type=int, default=5,
help='Number of warmup requests (default: 5)')
args = parser.parse_args()
print(f"\n{'='*60}")
print(f" Amplicon Repository Performance Test")
print(f"{'='*60}")
print(f"URL: {args.url}")
print(f"Total Requests: {args.requests}")
print(f"Concurrency: {args.concurrency}")
print(f"Warmup Requests: {args.warmup}")
print(f"{'='*60}\n")
# Check if server is responding
print("Checking server availability...")
try:
response = requests.get(args.url, timeout=10)
print(f"✓ Server is responding (Status: {response.status_code})")
except Exception as e:
print(f"✗ Cannot connect to server: {e}")
print("\nMake sure your server is running at", args.url)
sys.exit(1)
# Warmup
print(f"\nWarming up with {args.warmup} requests...")
for _ in range(args.warmup):
test_endpoint(args.url)
print("✓ Warmup complete")
# Run the test
print(f"\nRunning performance test with {args.requests} requests...")
print(f"(Concurrency: {args.concurrency} threads)\n")
start_time = time.time()
results = run_concurrent_tests(args.url, args.requests, args.concurrency)
total_time = time.time() - start_time
# Analyze and print results
stats = analyze_results(results)
print_results("Test Results", stats)
print(f"Total test duration: {total_time:.2f}s")
print(f"Average throughput: {args.requests / total_time:.2f} requests/second\n")
# Recommendations
print("Recommendations:")
if stats['mean_time'] > 1.0:
print(" • Mean response time is high (>1s). Consider:")
print(" - Increasing GUNICORN_WORKERS")
print(" - Enabling GUNICORN_THREADS")
print(" - Database query optimization")
if stats['success_rate'] < 100:
print(f" • {stats['failed']} requests failed. Consider:")
print(" - Increasing timeout settings")
print(" - Checking server logs for errors")
print(" - Reducing concurrency")
if stats['p99_time'] > stats['mean_time'] * 3:
print(" • High variance in response times. Consider:")
print(" - Investigating slow queries")
print(" - Checking for resource contention")
if __name__ == '__main__':
main()