Python之初识Locust

简介:

        Locust翻译中文为“蝗虫”,意指像蝗虫般对系统发起成千上万的请求,以测试系统能否在高并发下正常运行。Locust是一款开源的性能测试工具,完全基于Python开发,采用了基于事件的处理机制。

特点:

       1、基于python开发脚本

       2、开源免费

       3、分布式执行,配置master和slave(主从机器),在多台机器上对系统持续发起请求

       4、基于事件驱动,与其他工具使用进程和线程来模拟用户不同的是,Locust借助了gevent库对线程的支持,可以达到更高数量级的并发

       5、不支持监控被测机,需要配合其他工具进行监控

 

Locust安装配置:

        cmd中运行 pip install locust

输入locust -V 查看是否安装成功(如图显示版本信息即暗转成功)

 

安装成功后打开PyCharm并新建项目

from locust import HttpLocust, TaskSet, task
import subprocess

# 任务类
class UserBehavior(TaskSet):
    @task
    def getIndex(self):
        self.client.get("/70system/adminsys/index.html#/login?redirect=%2Fhome")  # 路径地址
        print("here")

class WebSite(HttpLocust):
    task_set = UserBehavior
    host = "https://www.xxxx.cn"  # 待测试的ip或者域名
    min_wait = 3000   # 用户执行任务之间等待时间的下界,单位:毫秒。
    max_wait = 6000   # 用户执行任务之间等待时间的上界,单位:毫秒。

if __name__ == "__main__":
    subprocess.Popen("locust -f hot_test.py", shell=True)

保存hot_test.py文件

打开cmd窗口cd到该文件目录,然后运行 python hot_test.py

运行成功后在浏览器打开 http://localhost:8089/ 进入下图界面

Number of users to simulate :要模拟的用户数

Hatch rate (users spawned/second):每秒钟产生的用户数

Start swarming:开始运行

设置模拟的用户数后点击Start swarming

运行后如下图所示:(注意:运行后需要手动点击STOP按钮停止)

Statistics:统计表/概要表

    Type:请求类型,即接口的请求方法;

    Name:请求路径;

    requests:当前已完成的请求数量;

    fails:当前失败的数量;

    Median:响应时间的中间值,即50%的响应时间在这个数值范围内,单位为毫秒;

    Average:平均响应时间,单位为毫秒;

    Min:最小响应时间,单位为毫秒;

    Max:最大响应时间,单位为毫秒;

    Content Size:所有请求的数据量,单位为字节;

    reqs/sec:每秒钟处理请求的数量,即QPS;


Charts:图表

      TPS:每秒请求事务数

      RT:响应时间

      NU:所有用户

      

Failures:失败

      有失败的话该页面会显示失败请求

Exceptions::异常

      有异常的话该页面会显示异常请求

Download Data:下载数据

[2019-11-21 09:20:20,796] 小米笔记本/INFO/locust.main: Starting web monitor at *:8089 [2019-11-21 09:20:20,796] 小米笔记本/INFO/locust.main: Starting Locust 0.13.2 [2019-11-21 09:20:20,806] 小米笔记本/ERROR/stderr: Traceback (most recent call last): [2019-11-21 09:20:20,806] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,806] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\threadpool.py", line 281, in _worker value = func(*args, **kwargs) [2019-11-21 09:20:20,807] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,807] 小米笔记本/ERROR/stderr: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 2: invalid continuation byte [2019-11-21 09:20:20,807] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,807] 小米笔记本/ERROR/stderr: 2019-11-21T01:20:20Z [2019-11-21 09:20:20,808] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,808] 小米笔记本/ERROR/stderr: (<ThreadPool at 0x3ae3550 0/1/10 hub=<Hub at 0x41a7bd8 thread_ident=0x26720>>, <built-in func tion gethostbyaddr>) failed with UnicodeDecodeError [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: Traceback (most recent call last): [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: File "src/gevent/greenlet.py", line 818, in gevent._greenlet.Greenlet.run [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\locust\web.py", line 201, in start pywsgi.WSGIServer((options.web_host, options.port), [2019-11-21 09:20:20,812] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,813] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\baseserver.py", line 385, in serve_forever self.start() [2019-11-21 09:20:20,813] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,813] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\baseserver.py", line 323, in start self.init_socket() [2019-11-21 09:20:20,813] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,814] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\pywsgi.py", line 1479, in init_socket self.update_environ() [2019-11-21 09:20:20,814] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,815] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\pywsgi.py", line 1491, in update_environ name = socket.getfqdn(address[0]) [2019-11-21 09:20:20,815] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,815] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\_socketcommon.py", line 275, in getfqdn hostname, aliases, _ = gethostbyaddr(name) [2019-11-21 09:20:20,815] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,816] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\_socketcommon.py", line 247, in gethostbyaddr return get_hub().resolver.gethostbyaddr(ip_address) [2019-11-21 09:20:20,816] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,816] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\resolver\thread.py", line 70, in gethostbyaddr return self.pool.apply(_socket.gethostbyaddr, args, kwargs) [2019-11-21 09:20:20,816] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,817] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\pool.py", line 159, in apply return self.spawn(func, *args, **kwds).get() [2019-11-21 09:20:20,817] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,817] 小米笔记本/ERROR/stderr: File "src/gevent/event.py", line 268, in gevent._event.AsyncResult.get [2019-11-21 09:20:20,817] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,818] 小米笔记本/ERROR/stderr: File "src/gevent/event.py", line 296, in gevent._event.AsyncResult.get [2019-11-21 09:20:20,818] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,818] 小米笔记本/ERROR/stderr: File "src/gevent/event.py", line 286, in gevent._event.AsyncResult.get [2019-11-21 09:20:20,818] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,818] 小米笔记本/ERROR/stderr: File "src/gevent/event.py", line 266, in gevent._event.AsyncResult._raise_exception [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\_compat.py", line 54, in reraise raise value.with_traceback(tb) [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: File "c:\python\lib\site-packages\gevent\threadpool.py", line 281, in _worker value = func(*args, **kwargs) [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,819] 小米笔记本/ERROR/stderr: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 2: invalid continuation byte [2019-11-21 09:20:20,820] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,820] 小米笔记本/ERROR/stderr: 2019-11-21T01:20:20Z [2019-11-21 09:20:20,820] 小米笔记本/ERROR/stderr: [2019-11-21 09:20:20,821] 小米笔记本/ERROR/stderr: <Greenlet at 0x5143798: start([<class 'locustfile.WebsiteUser'>], Namespace(csvfilebase=None, exit_code_on_error=1, )> failed with UnicodeDecodeError [2019-11-21 09:20:20,821] 小米笔记本/INFO/locust.main: Shutting down (exit code 0), bye. [2019-11-21 09:20:20,821] 小米笔记本/INFO/locust.main: Cleaning up runner... [2019-11-21 09:20:20,821] 小米笔记本/INFO/locust.main: Running teardowns... Name # reqs # fails Avg Min Max | Median req/s failures/s -------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------- Aggregated 0 0(0.00%) 0 0 0 | 0 0.00 0.00 Percentage of the requests completed within given times Name # reqs 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 1 00% ------------------------------------------------------------------------------------------------------------------------------------------------ ------ ------------------------------------------------------------------------------------------------------------------------------------------------ ------
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页