diff --git a/test.py b/test.py new file mode 100644 index 00000000..d0713c04 --- /dev/null +++ b/test.py @@ -0,0 +1,76 @@ +import json +import random +import time + +import requests +import concurrent.futures + +# Define the URL and headers +url = "http://localhost:8000/api/v1/messages" +# url = "https://iris-test.artemis.cit.tum.de/api/v1/messages" +headers = { + "Accept": "application/json", + "Authorization": "secret2", + # "Authorization": "ZX4Fznj8kit9aNW26zk38c6PHiUBNvdEUByFT5FSup3RnTbo9stzniAgJBUcNL4pHefw39U95LvV9N28fAm9q2Kn5wxhLxatbB8yum3S2x8BkTGsjYpLRErHEz5wULem", + "Content-Type": "application/json", +} + +# Load JSON data from a file +with open("data.json", "r") as json_file: + json_data = json.load(json_file) + + +# Function to send a POST request and measure the duration +def send_post_request(url, headers, json_data): + try: + start_time = time.time() + json_data["template"]["content"] = json_data["template"][ + "content" + ].replace( + "You're Iris", "You're Iris " + str(random.randint(0, 10000000)) + ) + response = requests.post(url, headers=headers, json=json_data) + end_time = time.time() + response.raise_for_status() + duration = end_time - start_time + print( + f"Request successful: {response.status_code}, " + f"Duration: {duration:.2f} seconds" + ) + print(response.content) + return duration + except Exception as e: + print(f"Request failed: {str(e)}") + print(e.response.content) + return None + + +# Number of times to execute the HTTP call in parallel +n = 1 # Change this to the desired number of parallel requests + + +# Create a thread pool executor +with concurrent.futures.ThreadPoolExecutor(max_workers=n) as executor: + # Submit POST requests in parallel and collect durations + durations = list( + executor.map( + lambda num: send_post_request(url, headers, json_data), range(n) + ) + ) + +# Filter out None values (failed requests) and calculate statistics +successful_durations = [ + duration for duration in durations if duration is not None +] +if successful_durations: + average_duration = sum(successful_durations) / len(successful_durations) + min_duration = min(successful_durations) + max_duration = max(successful_durations) +else: + average_duration = 0 + min_duration = 0 + max_duration = 0 + +print(f"Average Duration: {average_duration:.2f} seconds") +print(f"Min Duration: {min_duration:.2f} seconds") +print(f"Max Duration: {max_duration:.2f} seconds")