Skip to content

Instantly share code, notes, and snippets.

@AliYmn
Created October 9, 2024 13:32
Show Gist options
  • Save AliYmn/d9f5a7bf9491078e73fdcec905776f19 to your computer and use it in GitHub Desktop.
Save AliYmn/d9f5a7bf9491078e73fdcec905776f19 to your computer and use it in GitHub Desktop.
import threading
import time
import requests
# Set the URL to send requests to (choose a test server or an address that responds quickly)
URL = "https://httpbin.org/get"
# Total number of requests to be made
NUM_REQUESTS = 1000
NUM_THREADS = 50
# Calculate the number of requests per thread
requests_per_thread = NUM_REQUESTS // NUM_THREADS
# Function for each thread to send requests
def make_requests():
for _ in range(requests_per_thread):
try:
response = requests.get(URL)
response.raise_for_status() # Check if each request is successful
except requests.RequestException as e:
print(f"Request failed: {e}")
# Start measuring time
start_time = time.time()
# List to hold threads
threads = []
# Create and start threads
for _ in range(NUM_THREADS):
thread = threading.Thread(target=make_requests)
threads.append(thread)
thread.start()
# Wait for all threads to finish
for thread in threads:
thread.join()
# Measure and print the total time taken
end_time = time.time()
print(f"Total execution time: {end_time - start_time} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment