Skip to content

Instantly share code, notes, and snippets.

@agconti
Last active May 5, 2022 00:49
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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: Locustfile.py. 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 """
self.login()
def login(self):
self.client.post("/api/api-token-auth/",
{
"username":"your_username",
"password":"your_password"
})
@task(2)
def index(self):
self.client.get("/")
@task(1)
def profile(self):
self.client.get("/about")
class TemplateLoadTest(TaskSet):
@task(5)
def preview_template(self):
response = self.client.request(method="POST",
url="/api/preview-template/",
headers={
"Authorization": 'Token 182bc14esdf334438ec02d8b192a89d37267d4bfc'
},
files={
"user_photo": open("logo.png", 'r'),
},
data={
"template_choice": "blank"
})
print "Preview; Response status code:", response.status_code
@task(1)
def create_tempalte(self):
response = self.client.request(method="POST",
url="/api/create-template/",
headers={
"Authorization": 'Token 182bc14e571b4438ec02d8b192a454ddd7267d4bfc'
},
files={
"user_photo": open("logo.png", 'r'),
},
data={
"email": "example@example.com",
"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 = 'http://www.example.com'
task_set = TemplateLoadTest
min_wait = 5000
max_wait = 9000
@Luis16121
Copy link

Luis16121 commented May 5, 2022

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

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

   @task
      def creacion_leads(self):
          self.client.post('"/ 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