forked from YKPS-FooBar/swim4love
-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_test.py
91 lines (66 loc) · 2.13 KB
/
load_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import getpass
import random
import time
import requests
import sys
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--host',
help='full url to load test',
default='https://swim4love.thomaszhu.cn')
args = parser.parse_args()
print('✅ this script tests both server load and connection.')
print('✅ for stress test, running this script from multiple nodes/threads is recommended.')
input('⚠️ warning: this will alter/delete swimmer data: (enter to continue)')
root_url = args.host
username = input('Volunteer username: ')
password = getpass.getpass('Volunteer password: ')
session = requests.Session()
session.post(root_url + '/login',
data={'username': username, 'password': password})
id_pool = [id.zfill(3)
for id in session.get(root_url + '/swimmer/all').json()['data'].keys()]
print(f'all ids: {id_pool}')
def add_lap():
id = random.choice(id_pool)
id_pool.append(id)
print(f'lap added to {id}')
session.post(root_url + '/swimmer/add-lap', data={'id': id})
def sub_lap():
id = random.choice(id_pool)
print(f'lap subbed from {id}')
session.post(root_url + '/swimmer/sub-lap', data={'id': id})
def new_swimmer():
id = random.choice(list(set(range(1000)) - set(id_pool)))
id = f'{id:03d}'
name = f'penguin{id}'
print(f'new swimmer {id}')
session.post(root_url + '/swimmer/add', data={'id': id, 'name': name})
id_pool.append(id)
def del_swimmer():
id = random.choice(id_pool)
print(f'del swimmer {id}')
session.post(root_url + '/swimmer/delete', data={'id': id})
id_pool.remove(id)
start = time.time()
for i in range(50):
new_swimmer()
for i in range(50):
add_lap()
add_lap()
add_lap()
add_lap()
add_lap()
sub_lap()
new_swimmer()
add_lap()
add_lap()
add_lap()
add_lap()
sub_lap()
del_swimmer()
for i in range(len(id_pool)):
del_swimmer()
end = time.time()
print(f'✅ for reference, China -> US swim4love.thomaszhu.cn: ~300s')
print(f'⚠️ the test took {end - start:.2f}s')