How I share auth cookie with the rest of tasks only for current locust user?
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:1
- Comments:12 (4 by maintainers)
Top GitHub Comments
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.
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