Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to use locust.io by supplying user list

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?

like image 987
jloosli Avatar asked Apr 11 '14 15:04

jloosli


People also ask

How do locusts run with different users?

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 are locusts used for performance testing?

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.

How do you stop locusts after number of requests?

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.

How do I run a locust file in Python?

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.


2 Answers

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).

like image 156
heyman Avatar answered Oct 07 '22 23:10

heyman


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']] 
like image 14
Mesut GUNES Avatar answered Oct 07 '22 23:10

Mesut GUNES