-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlocustfile.py
65 lines (54 loc) · 1.94 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
###
# This is a simple load test script to perform deeper tests of the app
# and underlying tech stack.
#
# See: https://locust.io/
#
# Usage:
# - python3 -m pip install locustio
# - locust -H http://localhost:3000 (see docs above for more options)
###
import json
from locust import HttpLocust, TaskSet, task
auth_token = ""
class UserBehavior(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
self.login()
def on_stop(self):
""" on_stop is called when the TaskSet is stopping """
self.logout()
def login(self):
global auth_token
# log in if auth token not set yet
if auth_token == "":
payload = {"email": "[email protected]", "password": "changeme"}
headers = {"Content-Type": "application/json"}
login_response = self.client.post("/login", data=json.dumps(payload), headers=headers)
print( "Login response: {}".format(login_response.json()) )
auth_token = login_response.json()["data"]["token"]
print( "Token: {}".format(auth_token) )
def logout(self):
global auth_token
# log out if there is an auth token
if auth_token != "":
self.client.post("/logout", headers={"Authorization": "Bearer {}".format(auth_token)})
auth_token = ""
@task(2)
def health(self):
# no database interaction just app server
self.client.get("/healthz")
@task(1)
def users(self):
"""
Test hits all key elements of stack
auth lookup of token in Redis
users query in Postgres
log read activity in Elasticsearch
"""
self.client.get("/users", headers={"Authorization": "Bearer {}".format(auth_token)})
class ApiUser(HttpLocust):
task_set = UserBehavior
# wait times simulating how long a user might wait between tasks (impatient)
min_wait = 1000
max_wait = 5000