Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Created February 6, 2019 18:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MatrixManAtYrService/1d83abd54adc9d4181f9ebb98b9799f7 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/1d83abd54adc9d4181f9ebb98b9799f7 to your computer and use it in GitHub Desktop.
example of keeping tasks on a single class
from locust import HttpLocust, TaskSet, task
import json
class UserBehavior(TaskSet):
def on_start(self):
# this retrieves a coookie from the server and stores in in a RequestsCookieJar: self.locust.client.cookes
# that is, it stores the cookie on an instance of this class, not the class itself (as your code does)
# your server may have different requirements for how the login request should look, but this worked for me
self.client.post("/login",
data=json.dumps({'username':'foo',
'password':'bar'}),
headers={'Content-Type' : 'application/json',
'Accept' : 'application/json, text/javascript, */*; q=0.01',
'Connection' : 'keep-alive'}
)
@task(1)
def do_thing(self):
# your cookies may look differently, mine use this key:
print(self.locust.client.cookies['internalSession']) # used to verify that n unique cookies appear
# where n is the number of users I specified in the locust gui
# use the cookies kwarg and pass a dictionary
# get_dict pulls a dictionary from the RequestsCookieJar
response=self.client.get('/do/the/thing/', cookies=self.locust.client.cookies.get_dict())
# used to visually verify that I'm getting back what I think I should
print(' ', response.status_code, response.text)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 1000
max_wait = 9000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment