I need to stress-test a system and http://locust.io seems like the best way to go about this. However, it looks like it is set up to use the same user every time. I need each spawn to log in as a different user. How do I go about setting that up? Alternatively, is there another system that would be good to use?
We need to allocate unique credentials only once for each user. Also, it makes sense that we need to allocate credentials at the beginning of the script. Therefore, we need to find a place to place this function so it is executed at the beginning of each thread. Luckily, Locust provides such an option.
How to run a load test in Locust. The key –host http://localhost:3000 specifies the host on which Locust should be run (the corresponding field is filled in by default, but this value can be changed). Specify the values: in our example, Number of users = 10 and Spawn rate = 2. Next, use Start swarming to run the test.
In this case, each user should sign-in and make 3 GET request and locust should stop. So a total of 12 request. I found that previously there was an option -n to specify the number of request, however it is not there in version 0.14. 4.
Running the Script And as I mentioned above, if you want to run a script with a different name, you can execute locust -f [file name]. The number of users, users started per second and host (the URL under test) can be chosen from Locust's web user interface.
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).
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']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With