I am looking into to moving my multi-threaded python script to locust.
A simple explanation of what my script does is:
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
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.
Current RPS — Current requests per second.
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.
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.
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)
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