At present, the relatively simple and commonly used tool for performance testing is jmeter, but for some complex requirements and scenarios, you need to write java code. If you only know Python, can you do performance testing? Of course, you can. Today I will introduce Locust, a powerful performance testing framework for Python.
Introduction to Locust
An open source performance testing tool based on the python language. Locust is literally translated as locust. It is a metaphor that it can generate thousands of concurrency like locusts. It uses requests internally to complete http requests and uses coroutines to complete concurrent requests. It provides a beautiful and convenient WebUI and supports distributed Concurrent, of course, secondary development can also be carried out as needed.
Its most important advantage is that it has low running overhead and can generate higher concurrent requests.
For more detailed introduction and usage, please check the official website introduction: https://locust.io/
Github address: https://github.com/locustio/locust
Locust installation
Python version 3.6 and above are required.
pip install locust
Check if the installation is successful
locust -V
A Simple Locust Case
Create a new user behavior class, define a task set (user behavior), including one or more requests. Set the basic parameters of the pressure test, such as the host pointing to the pressure test, waiting time, etc.
from locust import task, HttpUser, between
# 定义用户行为
class UserBehavior(HttpUser):
host = "https://www.baidu.com" # 配置URL
wait_time = between(1,2) # 用户执行每个task之后等待1-2秒
# task装饰的方法会在Locust虚拟用户运行过程中被调用
# 定义任务Locust发送请求是基于requests实现,请求的使用requests库一致
@task
def test_baidu(self):
resp = self.client.get("/")
return resp.status_code
Of course, you can also add multiple requests
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(1, 2)
# wait_time = constant(3) # 每次请求停顿时间 (思考时间)
# 每个模拟用户开始执行,只执行一次
def on_start(self):
self.client.post("/login", json={"username":"foo", "password":"bar"})
@task
def hello_world(self):
self.client.get("/hello")
self.client.get("/world")
# task()参数用于指定该行为的执行权重。参数越大每次被虚拟用户执行的概率越高。
@task(3)
def view_item(self):
for item_id in range(10):
self.client.get(f"/item?id={item_id}", name="/item")
run Locust
Run the Locust script on the command line, enter the script directory under cmd (win), and execute the command:
locust -f 文件名.py
setup test
After the command line is running, visit through the browser: http://localhost:8089 (Locust starts the network monitor, the default port number is: 8089). The following interface appears and the startup is successful. Enter the corresponding number of virtual users and user startup speed, and click Start swarming to start running performance tests.
Number of users: Set the total number of simulated users
Spawn rate (users started/second): the number of new virtual users per second
Host: IP port or domain name of the target server
Test Monitoring Field Description
Click STOP, the operation is complete, view the test statistics as follows, similar to the content of the aggregation report in jmeter:
Type: The type of request, such as GET/POST.
Name: The requested path. Here is the Baidu homepage, namely: https://www.baidu.com/
request: the number of current requests.
fails: The number of current request failures.
Median: 50% of the user response time, in milliseconds
90%ile: 90% request response time
Average: The average response time of all requests, in milliseconds.
Min: Minimum response time, in milliseconds.
Max: The maximum response time, in milliseconds.
Average size: the average number of network bytes
Current RPS: Request per second (TPS).
Current Failures/s: The number of failures per second.
Response time and TPS curve graph
Recommended books
"Python programming from entry to proficiency"
This book focuses on the understanding of key and difficult points in the Python program development process, combined with typical program cases, and uses popular language to help readers master the Python language step by step. The knowledge points use real-life cases to help understanding, each case has a detailed code explanation, and the practical part will also combine the Python language with Web and data analysis technologies to expand the technical application.
Leave a message to like this book: Click to read this article and leave a message (related to the content of this article, related to this book, and suggestions from other official accounts), I will randomly select a fan to send this book with free shipping (the same person only has one chance a month), The book sending activity will continue, and there are many opportunities. Please continue to pay attention, thank you.