From 41d1d9619db11573d1c1b4370352d98cd6fd18dd Mon Sep 17 00:00:00 2001 From: Lan Hui <1348141770@qq.com> Date: Sun, 13 Aug 2023 16:14:43 +0800 Subject: [PATCH] Stress test code contributed by students --- app/test/test_stress.py | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 app/test/test_stress.py diff --git a/app/test/test_stress.py b/app/test/test_stress.py new file mode 100755 index 0000000..b437b86 --- /dev/null +++ b/app/test/test_stress.py @@ -0,0 +1,43 @@ +''' Contributed by Lin Junhong et al. 2023-06.''' + +import requests +import multiprocessing +import time + +def stress(username): + try: + data = { + 'username': username, + 'password': '123123' + } + headers = { + 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 Edg/114.0.1823.51' + } + session = requests.session() + response = session.post(url='http://127.0.0.1:5000/signup', data=data, headers=headers) + print('Sign up ', response.status_code) + time.sleep(0.5) + response = session.post(url='http://127.0.0.1:5000/login', data=data, headers=headers) + print('Sign in ', response.status_code) + time.sleep(0.5) + response = session.get(url=f'http://127.0.0.1:5000/{username}/userpage', headers=headers) + print('User page', response.status_code) + time.sleep(0.5) + print(session.cookies) + for i in range(5): + response = session.get(url=f'http://127.0.0.1:5000/get_next_article/{username}', headers=headers, cookies=session.cookies) + time.sleep(0.5) + print(f'Next page ({i}) [{username}]') + print(response.status_code) + print(response.json()['today_article']['article_title']) + except Exception as e: + print(e) + + +if __name__ == '__main__': + username = 'Learner' + pool = multiprocessing.Pool(processes=10) + for i in range(10): + pool.apply_async(stress, (f'{username}{i}',)) + pool.close() + pool.join()