Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locust: how I share auth cookie with the rest of tasks only for current locust user?

I am looking into to moving my multi-threaded python script to locust.

A simple explanation of what my script does is:

  1. Create a thread per user
  2. In each thread authenticates user and get auth cookie
  3. With that auth cookie perform various api calls at a set interval

When i started looking into locust, I have noticed that the only way to perform each task at its own specific interval, I would need to create a taskset per task.

This brought up an issue of how do i share the auth cookie for the given spawned user between task sets? Since in the long run I also need to share response data between taskset for the given spawned user as it differs between spawned users.

In the sample code below, all of the users spawned by locust, share the same "storage.cookie". Is there a way to keep storage.cookie unique per user, share it with all tasks sets for the given spawned user by locust ? Does locust report on which user is currently executing the task?

from __future__ import print_function
from locust import Locust, TaskSet, task, HttpLocust
import json


def auth(l):
    payload = {"username":"some_username","password":"some_password"} 
    resp = l.client.post('/auth', data = json.dumps(payload))
    storage.cookie = # get auth cookie from resp

def do_i_auth(l):
    if len(storage.cookie) == 0:
        auth(l)

class storage(object):
    cookie == ''

class first_call(TaskSet):
    def on_start(self):
        do_i_auth(self)

    @task
    def get_api_a(self):
        headers = {"Cookie":storage.cookie}
        self.client.get('/api_a', headers)

class second_call(TaskSet):
    def on_start(self):
        do_i_auth(self)

    @task
    def get_api_b(self):
        headers = {"Cookie":storage.cookie}
        self.client.get('/api_b', headers)

class api_A(HttpLocust):
    task_set = first_call
    min_wait = 5000
    max_wait = 5000    

class api_B(HttpLocust):
    task_set = second_call
    min_wait = 10000
    max_wait = 10000
like image 995
Alex D Avatar asked Feb 12 '18 03:02

Alex D


People also ask

How do you stop locusts after number of requests?

e.g. say I want a user to make 3 GET requests after sign in. I want the user count as 3. In this case, each user should sign-in and make 3 GET request and locust should stop.

What does RPS mean in Locust?

Current RPS — Current requests per second.

What is Wait_time in Locust?

The wait_time attribute It's used to determine for how long a simulated user will wait between executing tasks. Locust comes with a few built in functions that return a few common wait_time methods.

What is HttpUser in Locust?

HttpUser is the most commonly used User . It adds a client attribute which is used to make HTTP requests. from locust import HttpUser, task, between class MyUser(HttpUser): wait_time = between(5, 15) @task(4) def index(self): self. get("/") @task(1) def about(self): self.


1 Answers

You can try have your authorization function return the cookie and have it stored in each class separately. Something like this:

from __future__ import print_function
from locust import Locust, TaskSet, task, HttpLocust
import json


def auth(l):
    payload = {"username":"some_username","password":"some_password"} 
    resp = l.client.post('/auth', data = json.dumps(payload))
    cookie = # get auth cookie from resp
    return cookie

class first_call(TaskSet):
    cookie = ""

    def on_start(self):
        self.cookie = auth(self)

    @task
    def get_api_a(self):
        headers = {"Cookie":self.cookie}
        self.client.get('/api_a', headers)
like image 177
Dimitris Avatar answered Nov 16 '22 01:11

Dimitris