-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_key_rotation_targeted.py
More file actions
124 lines (97 loc) · 4.03 KB
/
Copy pathtest_key_rotation_targeted.py
File metadata and controls
124 lines (97 loc) · 4.03 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
#!/usr/bin/env python3
"""
Targeted Key Rotation Test - Tests key rotation with rapid successive calls
"""
import requests
import json
import time
import threading
def rapid_fire_test():
"""Send rapid requests to trigger rate limits and key rotation"""
print("🔥 RAPID FIRE KEY ROTATION TEST")
print("=" * 50)
print("🎯 Sending 10 rapid requests to trigger rate limits...")
# Small document with minimal questions to process quickly
test_payload = {
"documents": "https://hackrx-pdf-documents.s3.amazonaws.com/documents/Family_20Medicare_20Policy_20(UIN-_20UIIHLIP22070V042122)_201.pdf",
"questions": ["What is this document?"]
}
results = []
def send_request(request_id):
try:
print(f"🚀 Sending request #{request_id}")
start_time = time.time()
response = requests.post(
"http://127.0.0.1:8000/hackrx/run",
json=test_payload,
timeout=30
)
response_time = time.time() - start_time
if response.status_code == 200:
print(f"✅ Request #{request_id} - Success in {response_time:.2f}s")
results.append(True)
else:
print(f"❌ Request #{request_id} - Failed: {response.status_code}")
results.append(False)
except Exception as e:
print(f"❌ Request #{request_id} - Error: {e}")
results.append(False)
# Send requests in rapid succession
threads = []
for i in range(1, 11): # 10 requests
thread = threading.Thread(target=send_request, args=(i,))
threads.append(thread)
thread.start()
time.sleep(0.1) # 100ms between starts
# Wait for all threads to complete
for thread in threads:
thread.join()
successful = sum(results)
print(f"\n📊 RAPID FIRE RESULTS")
print(f"✅ Successful: {successful}/10")
print(f"❌ Failed: {10-successful}/10")
print(f"📈 Success Rate: {successful/10*100:.1f}%")
def sequential_burst_test():
"""Send a burst of sequential requests"""
print(f"\n⚡ SEQUENTIAL BURST TEST")
print("=" * 40)
test_payload = {
"documents": "https://hackrx-pdf-documents.s3.amazonaws.com/documents/Family_20Medicare_20Policy_20(UIN-_20UIIHLIP22070V042122)_201.pdf",
"questions": ["What is the grace period?", "What are the benefits?"]
}
successful = 0
for i in range(1, 6): # 5 sequential requests
try:
print(f"📤 Sequential request #{i}...")
start_time = time.time()
response = requests.post(
"http://127.0.0.1:8000/hackrx/run",
json=test_payload,
timeout=45
)
response_time = time.time() - start_time
if response.status_code == 200:
print(f"✅ Request #{i} - Success in {response_time:.2f}s")
successful += 1
else:
print(f"❌ Request #{i} - Failed: {response.status_code}")
print(f" Error: {response.text[:150]}...")
except Exception as e:
print(f"❌ Request #{i} - Error: {e}")
# Very short delay
time.sleep(0.5)
print(f"\n📊 Sequential Results: {successful}/5 successful")
if __name__ == "__main__":
print("🔑 TARGETED KEY ROTATION TESTING")
print("🎯 This test is designed to trigger rate limits quickly")
print("💡 Watch the server logs for key rotation messages!")
print()
# Test 1: Rapid concurrent requests
rapid_fire_test()
# Small delay between tests
print("\n⏳ Waiting 10 seconds before sequential test...")
time.sleep(10)
# Test 2: Sequential burst
sequential_burst_test()
print(f"\n✨ Key rotation testing complete!")
print("📋 Check server logs for detailed rotation information")