Spaces:
Sleeping
Sleeping
Upload tests.py
#1
by
hw01558
- opened
tests.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import time
|
| 3 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 4 |
+
import csv
|
| 5 |
+
|
| 6 |
+
NUM_REQUESTS = 5
|
| 7 |
+
CONCURRENT_THREADS = 10
|
| 8 |
+
URL = "http://localhost:5000/"
|
| 9 |
+
|
| 10 |
+
def send_request():
|
| 11 |
+
data = {
|
| 12 |
+
'search': "A MRI, magnetic resonance imaging, scan is a very useful diagnosis tool.",
|
| 13 |
+
'pipeline_select': '1'
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
start_time = time.time()
|
| 17 |
+
try:
|
| 18 |
+
response = requests.post(URL, data=data)
|
| 19 |
+
elapsed = time.time() - start_time
|
| 20 |
+
if response.status_code != 200:
|
| 21 |
+
print(f"Error {response.status_code}: {response.text[:100]}")
|
| 22 |
+
return response.status_code, elapsed
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print("Request failed:", e)
|
| 25 |
+
return 500, 0 # Treat exceptions as failures
|
| 26 |
+
|
| 27 |
+
def run_stress_test():
|
| 28 |
+
results = []
|
| 29 |
+
|
| 30 |
+
with ThreadPoolExecutor(max_workers=CONCURRENT_THREADS) as executor:
|
| 31 |
+
futures = [executor.submit(send_request) for _ in range(NUM_REQUESTS)]
|
| 32 |
+
for future in futures:
|
| 33 |
+
results.append(future.result())
|
| 34 |
+
|
| 35 |
+
successes = sum(1 for r in results if r[0] == 200)
|
| 36 |
+
failures = NUM_REQUESTS - successes
|
| 37 |
+
avg_time = sum(r[1] for r in results) / NUM_REQUESTS
|
| 38 |
+
max_time = max(r[1] for r in results)
|
| 39 |
+
min_time = min(r[1] for r in results)
|
| 40 |
+
|
| 41 |
+
print(f"\n=== Stress Test Results ===")
|
| 42 |
+
print(f"Total Requests: {NUM_REQUESTS}")
|
| 43 |
+
print(f"Concurrency Level: {CONCURRENT_THREADS}")
|
| 44 |
+
print(f"Successes: {successes}")
|
| 45 |
+
print(f"Failures: {failures}")
|
| 46 |
+
print(f"Avg Time: {avg_time:.3f}s")
|
| 47 |
+
print(f"Min Time: {min_time:.3f}s")
|
| 48 |
+
print(f"Max Time: {max_time:.3f}s")
|
| 49 |
+
|
| 50 |
+
return [NUM_REQUESTS, CONCURRENT_THREADS, avg_time, max_time]
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
# Open the CSV file for writing the summary results
|
| 54 |
+
with open('stress_test_results.csv', 'w', newline='') as csvfile:
|
| 55 |
+
writer = csv.writer(csvfile)
|
| 56 |
+
writer.writerow(['Total Requests', 'Concurrency Level', 'Avg Time', 'Max Time'])
|
| 57 |
+
|
| 58 |
+
for users in [1, 5, 10, 20, 50, 100]:
|
| 59 |
+
CONCURRENT_THREADS = users
|
| 60 |
+
NUM_REQUESTS = users * 5
|
| 61 |
+
result = run_stress_test()
|
| 62 |
+
|
| 63 |
+
writer.writerow(result)
|
| 64 |
+
|