Stress testing python script

This commit is contained in:
Imbus 2023-10-10 19:30:10 +02:00
parent dfe3804467
commit 8b58f97aa2

View file

@ -0,0 +1,38 @@
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)')