question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How I share auth cookie with the rest of tasks only for current locust user?

See original GitHub issue

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

A simple explanation of what my script does is:

Create a thread per user In each thread authenticates user and get auth cookie 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

Stack overflow copy: https://stackoverflow.com/questions/48739300/locust-how-i-share-auth-cookie-with-the-rest-of-tasks-only-for-current-locust-u

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
cgoldbergcommented, Feb 14, 2018

you still aren’t creating the storage instance in your Locust class (the classes based on HttpLocust).

as for your other questions/issues, I really have no idea what you are trying to do… and this entire thread is getting confusing.

I’m closing it, since there is no Locust issue.

3reactions
MatrixManAtYrServicecommented, Feb 6, 2019

Why have separate classes at all? If you encapsulate the whole flow for each user on a single class then you can store the cookies on objects of that type.

This worked for me:

https://gist.github.com/MatrixManAtYrService/1d83abd54adc9d4181f9ebb98b9799f7

Read more comments on GitHub >

github_iconTop Results From Across the Web

how I share auth cookie with the rest of tasks only for current locust ...
A simple explanation of what my script does is: Create a thread per user; In each thread authenticates user and get auth cookie;...
Read more >
Writing a locustfile — Locust 2.14.0 documentation
A locust file is just a normal Python module, it can import code from other files or packages. class QuickstartUser(HttpUser):. Here we define...
Read more >
How we manipulated Locust to test system performance under ...
Our observations that: The load test must reflect real user behaviors and interactions; Load testing alone doesn't validate system behavior against target ...
Read more >
Locust + Python Examples: Variables + Assertions - BlazeMeter
In order to run this test we'll need to execute the command locust in the script's directory from the command line, which will...
Read more >
Performance Testing with Locust [Part 3] | by Infopulse - Medium
When writing the first tests with Locust, I faced the need to get an authorization token on one server and then use it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found