Skip to content

Instantly share code, notes, and snippets.

Last active May 5, 2022 00:49
Show Gist options
  • Save agconti/0f3886cbf1ce158e913e to your computer and use it in GitHub Desktop.
Save agconti/0f3886cbf1ce158e913e to your computer and use it in GitHub Desktop.
Example Locustfile Keywords: Locust, LocistIo.
from locust import HttpLocust, TaskSet, task
class MostBasicLoadTest(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
def login(self):"/api/api-token-auth/",
def index(self):
def profile(self):
class TemplateLoadTest(TaskSet):
def preview_template(self):
response = self.client.request(method="POST",
"Authorization": 'Token 182bc14esdf334438ec02d8b192a89d37267d4bfc'
"user_photo": open("logo.png", 'r'),
"template_choice": "blank"
print "Preview; Response status code:", response.status_code
def create_tempalte(self):
response = self.client.request(method="POST",
"Authorization": 'Token 182bc14e571b4438ec02d8b192a454ddd7267d4bfc'
"user_photo": open("logo.png", 'r'),
"email": "",
"first_name": "John",
"last_name": "Doe",
"template_text": "Locust Test",
"quantity": 3,
"street_address": "123 Fake Street",
"state": "NY",
"city": "New York",
"zip_code": 10001,
"international_shipping": True,
print "Create; Response status code:", response.status_code
print "Create; Response content:", response.content
class WebsiteUser(HttpLocust):
host = ''
task_set = TemplateLoadTest
min_wait = 5000
max_wait = 9000
Copy link

hello, I have a question: how to keep token in multiple tasks?

Copy link

ahmad-ih commented Oct 22, 2020

@Tesla9527 You can keep the token as a class attribute by writing you login function like this:

def login(self):
    with"/api/api-token-auth/", {
    }) as response:
        self.token = response.json().get('token')

and then use that token where ever you want in the class by calling self.token :)

Copy link

Hello, I am new to the locust.
I wanted to do a load test with a POST request and we have TPS associated with each user at the account level so to initiate every request I need to replace the account Id in the URL and token in the headers.

URL -**your_account_id**/v1

headers = {
'Content-Type': "application/json",
'Authorization': "your_api_token"

Here I need to replace the account Id and API token for each request? I have saved Id and token in a file separated by Pipe.
How can i do this? Can you please guide me?

Copy link

Luis16121 commented May 5, 2022

  class WebsiteUser(HttpUser):
      wait_time = between(5, 15)

 def api_refresh_token(self):
    response ='"/api/api-token-auth/"',
    auth = HTTPBasicAuth('1q1w2e3r24', '9iuey6391'),
    data = {'grant_type': 'client_credentials'}, name = 'Refresh Token').json()
    return response.get('access_token')

      def creacion_leads(self):
'"/ acerca de"',
          data = json.dumps({"Nombre" : "Pablito Perez=="}),
          headers = {"Authorization":"Bearer " + self.api_refresh_token(),'Accept': 'application/json'},
          name = 'Creación de LProspectos')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment