Locust
Introduction | |
Install | |
Quick Start | |
Related Topics |
Introduction
In these articles you will learn how to set up load testing in Python with Locust.
There is minor assumption that you are working on
Ubuntu Linux
or similar OS
I have aliases python - python3 and pip - pip3 so I do not bother with writing 3 at the end.
Further reading about aliases
«Bash Python alias»
Installation
Activate your virtual environment and install locust by running
python -m pip install locust
Check list of installed modules
python3 -m pip list
Package Version ---------- --------- Package Version ---------------- --------- certifi 2020.6.20 chardet 3.0.4 click 7.1.2 ConfigArgParse 1.2.3 Flask 1.1.2 Flask-BasicAuth 0.2.0 gevent 20.9.0 geventhttpclient 1.4.4 greenlet 0.4.17 idna 2.10 itsdangerous 1.1.0 Jinja2 2.11.2 locust 1.3.1 MarkupSafe 1.1.1 msgpack 1.0.0 pip 20.2.3 psutil 5.7.3 pyzmq 19.0.2 requests 2.24.0 setuptools 50.3.1 six 1.15.0 urllib3 1.25.11 Werkzeug 1.0.1 wheel 0.35.1 zope.event 4.5.0 zope.interface 5.1.2
locust installs many dependencies, as you can see even
Flask
is here.
In this list everything except pip, setuptools and wheel are locust dependencies.
To check where locust is installed execute
python -m pip show locust
Name: locust Version: 1.3.1 Summary: Developer friendly load testing framework Home-page: https://locust.io/ Author: None Author-email: None License: MIT Location: /home/andrei/python/virtualenvs/locust1_env/lib/python3.8/site-packages Requires: Flask-BasicAuth, msgpack, gevent, psutil, pyzmq, requests, ConfigArgParse, flask, geventhttpclient, Werkzeug Required-by:
Quick Start
Create locustfile.py file and copy the following code there ( source )
import time
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(1, 2)
@task
def index_page(self):
self.client.get("/hello")
self.client.get("/world")
@task(3)
def view_item(self):
for item_id in range(10):
self.client.get(f"/item?id={item_id}", name="/item")
time.sleep(1)
def on_start(self):
self.client.post("/login", json={"username":"foo", "password":"bar"})
From the same dir with locustfile.py execute
locust
[2020-10-27 11:33:44,124] andrei-ubuntu/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces) [2020-10-27 11:33:44,133] andrei-ubuntu/INFO/locust.main: Starting Locust 1.3.1
If locust complains with:
[2020-10-27 11:33:16,969] andrei-ubuntu/WARNING/locust.main: System open file limit '1024' is below minimum setting '10000'. It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
You can find the solution here
Start Test
Errors
System open file limit '1024' is below minimum setting
If you execute
locust
And get the following warning
[2020-10-27 10:36:12,176] andrei-ubuntu/WARNING/locust.main: System open file limit '1024' is below minimum setting '10000'. It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
[2020-10-27 10:36:12,177] andrei-ubuntu/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces) [2020-10-27 10:36:12,194] andrei-ubuntu/INFO/locust.main: Starting Locust 1.3.1
It means that your limit of simultaneously open files is below 10 000.
It should be increased above 10 000 or locust will not be able to work
To check this limit in
Linux
you can execute
cat /proc/sys/fs/file-max
9223372036854775807
9223372036854775807 - this is my absolute maximum. We are interested in limitations for external software
To study these limits execute ulimit with -Hn flag for hard limit
and -Sn for soft limit
setrlimit
To change ulimit for specific user please read
«Changing ulimit in CentOS»
ulimit -Sn
1024
1024 is the value locust was complaining at
To raise this limit execute
ulimit -Sn 10001
ulimit -Sn
10001
locust
[2020-10-27 11:33:44,124] andrei-ubuntu/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces) [2020-10-27 11:33:44,133] andrei-ubuntu/INFO/locust.main: Starting Locust 1.3.1
locust: error: unrecognized arguments: --no-web
Means that you are running locust without UI and executing
locust -f locustfile.py --host=https://my-site.com --no-web -u 100 -r 10
The error you get
locust: error: unrecognized arguments: --no-web
Change --no-web to --headless
Locust | |
Start Locust | |
Install Locust | |
Start Options --help | |
Locust Errors | |
Install Locust in PyCharm | |
Software QA | |
Call REST API with Python | |
Python | |
Pip | |
Flask |