Load testing with Locust



http://locust.io
@locustio


Jonatan Heyman
@jonatanheyman

What is Locust?


  • An open source load testing tool
  • Write test scenarios in python code
  • Uses gevent
  • Distributed, can simulate millions
    of simultaneous users

What Locust provides
to the developer


  • A framework for simulating user behaviour
  • An HTTP client (python-requests)
  • A web interface that show statistics
  • Ability to run your code distributed 
    across multiple machines

Locust's web interface

Writing Locust test scripts

from locust import HttpLocust, TaskSet, task

class UserBehaviour(TaskSet):
    @task(30)
    def index_page(self):
        self.client.get("/")
    
    @task(10)
    def press_page(self):
        self.client.get("/press")

class WebUser(HttpLocust):
    task_set = UserBehaviour
    min_wait = 5 * 1000
    max_wait = 60 * 1000
 $ locust -f locustfile.py --host http://instagram.com

TaskSets can be nested

class ApiDocs(TaskSet):
    @task
    def overview(self):
        self.client.get("/developer/")
    
    @task
    def authentication(self):
        self.client.get("/developer/authentication")
    
    @task
    def endpoints(self):
        self.client.get("/developer/endpoints")
    
    @task
    def stop(self):
        self.interrupt()

class UserBehaviour(TaskSet):
    tasks = {ApiDocs: 10}
    
    @task(30)
    def index_page(self):
        self.client.get("/")
    
    @task(10)
    def press_page(self):
        self.client.get("/press")

class WebUser(HttpLocust):
    task_set = UserBehaviour
    min_wait = 5 * 1000
    max_wait = 60 * 1000
 

Parsing response data

import random
from locust import HttpLocust, TaskSet, task
from pyquery import PyQuery

class ApiDocs(TaskSet):
    def on_start(self):
        self.overview()
    
    @task
    def overview(self):
        response = self.client.get("/developer/")
        pq = PyQuery(response.content)
        
        self.locust.doc_pages = []
        for link in pq(".sidebar-content li a"):
            self.locust.doc_pages.append(link.attrib["href"])
    
    @task
    def sub_page(self):
        url = random.choice(self.locust_doc_pages)
        self.client.get(url) 

Finally


  • Defining your tests in code is great
  • Create realistic load tests by simulating user behaviour
    Do not just test URL endpoints
  • Locust can test any request/response based system
  • Check out the docs
  • $ pip install locustio

Thanks!



http://locust.io
@locustio


Jonatan Heyman
@jonatanheyman

load-testing-with-locust

By jonatanheyman

load-testing-with-locust

  • 3,677