Load Testing with Locust

You might be familiar with load testing tools such as Apache Benchmark (ab), siege, Apache JMeter and cloud services such as BlazeMeter, LoadImpact, Loader.io, etc. I tried many other tools, and found that there is no tool that completely satisfies me: ab and siege are too plain and simple without scenario, JMeter needs time for recording and defining cases (and I personally don’t like its UI, years ago. I have not tried it yet recent years), cloud load testing tools are … expensive, while I am poor 🙂 So today I blog a new entry: load testing with Locust. Hope that Locust does not disappoint me.

I mostly use Ubuntu 16.04 for master and slave nodes.

Install Locust

  1. Install pip:
    apt-get install python-pip
    pip install --upgrade pip
    apt remove python-pip
  2. Install locust:
    pip install locustio

Write a simple test scenario with Python

  1. First, I will install faker lib so that I can easily generate test data later:
    pip install Faker
  2. Next, I will write a sample-test.py file as follows (First I will log in, and then visit a blog or add a new blog entry. I will also wait from 5s – 9s between 2 testing actions/tasks):
    from locust import HttpLocust, TaskSet
    from faker import Faker
    from random import randint
    
    fake = Faker()
    
    def login(l):
        response = l.client.post("/login", {"login":"[email protected]", "password":"123456"})
    
    def visitBlog(l):
        response = l.client.get("/blog")
    
    def addBlog(l):
        response = l.client.post("/blog/add/", {"title":fake.sentence(), "text": fake.text(), "selected_categories": [randint(16,27)], "publish": "Publish"})
    #    print "Response status code:", response.status_code
    #    print "Response content:", response.content
    
    class UserBehavior(TaskSet):
        tasks = {visitBlog: 2, addBlog: 1}
    
        def on_start(self):
            login(self)
    
    class WebsiteUser(HttpLocust):
        task_set = UserBehavior
        min_wait = 5000
        max_wait = 9000

     

  3. Next, I’ll run it with locust with no web for 1 min:
    locust -f sample-test.py --host=https://MY_WEB_TEST_ADDRESS.com --no-web -c 1 -r 1 -t 1m
  4. After checking everything, I write the full test case and then run real testing with a web interface and higher load.

Leave a Reply