-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocustfile.py
72 lines (61 loc) · 2.1 KB
/
locustfile.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
import random
import yaml
import urllib3
from locust import HttpLocust, TaskSet, task
# Load config file
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
with open('config.yaml', 'r') as config_file:
try:
config = yaml.load(config_file)
api_base_url = config['api_base_url']
auth = (config['user'], config['password'])
except yaml.YAMLError as error:
exit(error)
term_ids = [f'20{y:02}0{t}' for y in range(15, 20) for t in range(0, 4)]
osu_ids = set()
class UserBehavior(TaskSet):
# get some ids before run the task set
def on_start(self):
self.get_by_term_id()
@task(2)
def get_by_osuId_id(self):
osu_id = random.sample(osu_ids, 1)[0]
url = f'{api_base_url}?osuId={osu_id}'
self.client.get(url, verify=False, auth=auth)
@task(2)
def get_by_term_id(self):
term_id = random.choice(term_ids)
url = f'{api_base_url}?term={term_id}'
with self.client.get(
url,
catch_response=True, verify=False, auth=auth
) as response:
if response.status_code == 200:
if response.json()['data'] and len(osu_ids) < 10:
data = random.choice(response.json()['data'])
osu_ids.add(data['id'][:9])
response.success()
@task(1)
def get_by_bad_request(self):
url = f'{api_base_url}'
with self.client.get(
url,
catch_response=True, verify=False, auth=auth
) as response:
if response.status_code == 400:
response.success()
@task(1)
def get_by_id(self):
term_id = random.choice(term_ids)
osu_id = random.sample(osu_ids, 1)[0]
url = f'{api_base_url}/{osu_id}-{term_id}'
with self.client.get(
url,
catch_response=True, verify=False, auth=auth
) as response:
if response.status_code == 200 or response.status_code == 404:
response.success()
class ApiUser(HttpLocust):
task_set = UserBehavior
min_wait = 100
max_wait = 5000