Load test tools with python

To Nha Notes | May 10, 2021, 9:58 a.m.

locus

An open source load testing tool. Define user behaviour with Python code, and swarm your system with millions of simultaneous users. For more detail in its document https://locust.io/

Example code:

-- /path/to/load_test.py --

from locust import HttpUser, between, task


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

    def on_start(self):
        self.client.post("/login", {
            "username": "test_user",
            "password": ""
        })

    @task
    def index(self):
        self.client.get("/")
        self.client.get("/static/assets.js")

    @task
    def about(self):
        self.client.get("/about/")

-- /path/to/locust.conf --

locustfile = /path/to/load_test.py
headless = true
host = http://localhost:8000
users = 50
spawn-rate = 5
run-time = 10m

Command usage:

locust --config=/path/to/locust.conf

molotov

The Molotov project is an interesting project geared towards load testing. Some of its features are similar to those of Apache Benchmark, but being a Python project, it provides a way to write scenarios with Python and the asyncio module. For more detail in its document https://molotov.readthedocs.io/en/stable/

Example code:

--- load_test.py ---

import molotov

@molotov.setup_session()
async def init_session(worker_num, session):
    session.cookies = {"sessionid": "r4vlxmedn92fzlx26iq0qkb3qf7gy0kr"}

@molotov.scenario()
async def scenario_one(session):
    async with session.get("http://localhost:5000/") as resp:
        assert resp.status == 200

@molotov.scenario(80)
async def scenario_two(session):
    resp = await session.post("http://localhost:5000", params={'q': 'devops'})
    redirect_status = resp.history[0].status
    error = "unexpected redirect status: %s" % redirect_status
    assert redirect_status == 301, error

Command usage:

molotov -v -r 1 --processes 1 load_test.py