Way to use locust.io by supplying user list
Alternatively, you can create users.py
module to hold the users' information you need in your test cases, in my example, it holds email
and cookies
. Then you can call them randomly in your tasks. See below:
# locustfile.py
from locust import HttpLocust, TaskSet, task
from user_agent import *
from users import users_info
class UserBehaviour(TaskSet):
def get_user(self):
user = random.choice(users_info)
return user
@task(10)
def get_siparislerim(self):
user = self.get_user()
user_agent = self.get_user_agent()
r = self.client.get("/orders", headers = {"Cookie": user[1], 'User-Agent': user_agent})
class User(HttpLocust):
task_set = UserBehaviour
min_wait = 5000
max_wait = 60000
User and user-agent can be called by a function. With this way, we could distribute the test with many users and different user-agents.
# users.py
users_info = [
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user'],
['[email protected]', 'cookies_created_by_each_user']]
Locust author here.
By default, each HttpLocust user instance has an HTTP client that has it's own separate session.
Locust doesn't have any feature for providing a list of user credentials or similar. However, your load testing scripts are just python code, and luckily it's trivial to implement this yourself.
Here's a short example:
# locustfile.py
from locust import HttpLocust, TaskSet, task
USER_CREDENTIALS = [
("user1", "password"),
("user2", "password"),
("user3", "password"),
]
class UserBehaviour(TaskSet):
def on_start(self):
if len(USER_CREDENTIALS) > 0:
user, passw = USER_CREDENTIALS.pop()
self.client.post("/login", {"username":user, "password":passw})
@task
def some_task(self):
# user should be logged in here (unless the USER_CREDENTIALS ran out)
self.client.get("/protected/resource")
class User(HttpLocust):
task_set = UserBehaviour
min_wait = 5000
max_wait = 60000
The above code wouldn't work when running Locust distributed, since the same code runs on each slave node, and they don't share any state. Therefore you would have to introduce some external datastore which the slave nodes could use to share states (e.g. PostgreSQL, redis, memcached or something else).