Locust 压力测试helloworld

1. 什么是Locust

Locust 是一种易于使用、可直接使用pyhton编写脚本运行且可扩展的性能测试工具。

2. 安装Locust

Python 3.9.16
pip install locust==2.15.1

3. 一个简单的示例

3.1.  编写下面代码,文件命名为locustfile_test.py

from locust import HttpUser, TaskSet, task
import urllib3

urllib3.disable_warnings()

class UserBehavior(TaskSet):

    def on_start(self):
        print("start****")

    def on_stop(self):
        print("stop****")

    @task(1)
    def baidu_page1(self):
        res = self.client.get("/", verify=False)

class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    host = "https://www.baidu.com"
    min_wait = 1000
    max_wait = 2000

3.2. 通过下面命令执行上面代码

locust -f locustfile_test.py

3.3. 通过 http://localhost:8089/ 访问,Number of total users to simulate设置模拟的用户数,Spawn rate (users spawned/second)每秒启动的虚拟用户数

3.4.【Start swarming】后结果:

    性能参数:

Type: 请求类型,如Get/Post
Name: 请求路径
Requests: 当前请求数量
Failes: 请求失败数量
Median: 中间值毫秒,一半的服务器响应低于该值,还有一半高于该值
 90%: 90%的请求响应时间
Average: 平均值,单位毫秒,所有请求平均响应时间
 Min: 请求的服务器最小响应时间
 Max: 请求的服务器最大响应时间
Average size: 单个请求大小,字节
RPS: 每秒能处理的请求数目

4. locust与jmeter的区别

工具 区别
jmeter 需要在UI界面上通过选择组件来“编写”脚本,模拟的负载是线程绑定的,意味着模拟的每个用户,都需要一个单独的线程。单台负载机可模拟的负载数有限
locust 通过编写简单易读的代码完成测试脚本,基于事件,同样配置下,单台负载机可模拟的负载数远超jmeter

PS:但locust的局限性在于,目前其本身对测试过程的监控和测试结果展示,不如jmeter全面和详细,需要进行二次开发才能满足需求越来越复杂的性能测试需要。

参考资料

Locust - A modern load testing framework

猜你喜欢

转载自blog.csdn.net/keeppractice/article/details/130260844