51Testing软件测试论坛

标题: Python+ Locust性能测试 [打印本页]

作者: ailvyoyo    时间: 2021-1-12 10:35
标题: Python+ Locust性能测试
本帖最后由 ailvyoyo 于 2021-1-12 10:52 编辑

1、安装
python安装locust库
2、pycharm脚本编写
在locust现版本中HttpLocust改为HttpUser
以下是改变后的调用方式:
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):

    wait_time = between(5, 9)
   
    # @task
    # def index_page(self):
    #     self.client.get("/hello")
    #     self.client.get("/world")
   
    # @task(3)
    # def view_item(self):
    #     item_id = random.randint(1, 10000)
    #     self.client.get(f"/item?id={item_id}", name="/item")
   
    #on_start只会在最开始的时候调用一次
    def on_start(self):
        self.client.post("/api/users/signin/", {"username": "lxm1", "password": "123456"})



注意 :1、在locust现版本中HttpLocust改为HttpUser
            2、locust版本0.13之后已经废除了min_wait和max_wait的使用
                                 min_wait = 3000   
                                  max_wait = 7000
                          改为 wait_time = between(3,7)
3、cmd系统运行
1、先进入python脚本目录,cd D:\Users\Admin\PycharmProjects\性能_Locust\demo
2、后输入命令:locust -f test.py --host=http://10.5.4.131:8899
3、浏览器打开locust服务:http://localhost:8089
4、系统指标体系1、响应时间:
         反应系统处理效率的指标,从开始到完成某项工作所需时间的度量,响应时间通常随负载的增加而增加
2、吞吐量:
       反应系统处理能力的指标,指单位时间内完成工作的度量,可以从客户端或服务器端视角两个方面来进行综合评估
3、事务处理能力(TPS):
      对一笔事务进行处理时的响应情况,通常包含三个指标,处理该业务的响应时间,二是处理该笔业务的成功率,三是单位时间内可以处理的业务数。
5、服务器指标体系
1、CPU使用率
2、可用内存数
3、磁盘使用率
4、网络带宽
5、线程池 消耗
6、连接池消耗
6、利用Python监控系统资源
Psutil
import psutil
import time
print("CPU使用率     内存使用率      C盘使用率")
delay = 2
while True:
    time.sleep(delay)
    # print(psutil.disk_usage("D:\\").percent)
    # print(psutil.cpu_percent())
    # print(psutil.virtual_memory().percent)
    print(str(psutil.cpu_percent()) + '%s     ' +           str(psutil.virtual_memory().percent) + '%s      '+    str(psutil.disk_usage("D:\\").percent) + '%s      ')






欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2