FrostByte/server/testing/stresstest.py
2023-10-10 19:30:10 +02:00

38 lines
No EOL
931 B
Python

import requests
import multiprocessing
import time
import uuid
# Target API endpoint
api_url = 'http://localhost:8080/api/register'
# Send a POST request to the API with mock data
def send_request(_):
username = str(uuid.uuid4())[:8]
password = str(uuid.uuid4())[-12:]
payload = {'username': username, 'password': password, 'captcha': '1234'}
response = requests.post(api_url, json=payload)
return response.status_code
# Number of parallel requests to send
num_requests = 10
# Pool of worker processes
pool = multiprocessing.Pool(processes=num_requests)
# Record the start time
start_time = time.time()
n = 0
# Bench for one min
while time.time() - start_time < 60:
results = pool.map(send_request, range(num_requests))
n += num_requests
# Close the pool
pool.close()
pool.join()
# Print summary
print(f'Total Requests: {n}')
print(f'Requests per second: {n / (time.time() - start_time)} (avg)')