From 8c5aaaa33d1ba1517a216456ccb528b3251bf5c4 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Wed, 26 Jun 2024 23:40:31 -0400 Subject: [PATCH] feat(scripts): add load testing --- scripts/load_test.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/load_test.py diff --git a/scripts/load_test.py b/scripts/load_test.py new file mode 100644 index 0000000..00e4793 --- /dev/null +++ b/scripts/load_test.py @@ -0,0 +1,31 @@ +import requests +from bs4 import BeautifulSoup +from concurrent.futures import ThreadPoolExecutor + +base_url = "http://localhost:8080" + +full_path = f"{base_url}/r/politics" + +ctr = 0 + +def fetch_url(url): + global ctr + response = requests.get(url) + ctr += 1 + print(f"Request count: {ctr}") + return response + +while full_path: + response = requests.get(full_path) + ctr += 1 + print(f"Request count: {ctr}") + soup = BeautifulSoup(response.text, 'html.parser') + comment_links = soup.find_all('a', class_='post_comments') + comment_urls = [base_url + link['href'] for link in comment_links] + with ThreadPoolExecutor(max_workers=10) as executor: + executor.map(fetch_url, comment_urls) + next_link = soup.find('a', accesskey='N') + if next_link: + full_path = base_url + next_link['href'] + else: + break