首页 > 其他分享 >7-Locust自带的events钩子函数

7-Locust自带的events钩子函数

时间:2023-08-04 18:34:50浏览次数:43  
标签:MateBook HUAWEI Locust 钩子 Pro locust print test events

Events Hook

Locust提供了事件钩子函数,它们可以在特定的时间点执行,例如test_start,其类似与pytest中的setup_module

使用方法举例

  1. 使用时需要引入events模块 from locust import events
  2. 自定义一个方法,若不清楚需要接收什么参数,一般来说直接给一个可变关键字参数**kwargs即可(除非你需要用到接收的参数来做某些事情)「官方文档
  3. 加上装饰器@events.xxx.add_listener,(xxx指你希望使用的events,比如test_startinit等等)

以下介绍Locust自带的一些常用的events对象

test_start&test_stop

类似pytest中的setup_moduleteardown_module,在整个模块开始执行测试之前(后)执行,例子:

from locust import task, HttpUser, events


@events.test_start.add_listener
def on_test_start(**kwargs):
    print(kwargs['environment'])
    print('test is start')


@events.test_stop.add_listener
def on_test_stop(environment):  # 知道要接受什么参数就填,不知道就用可变关键字参数,效果一样
    print(environment)
    print('test is stop')


class User1(HttpUser):

    @task
    def test_01(self):
        print('test_01')
        self.stop()


class User2(HttpUser):

    @task
    def test_02(self):
        print('test_02')
        self.stop()
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_test_start.py -u 4 -r 4 -t 2 --headless --host https://www.baidu.com --only-summary
[2023-08-04 12:03:58,402] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Run time limit set to 2 seconds
[2023-08-04 12:03:58,402] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
<locust.env.Environment object at 0x105b778e0>
test is start
[2023-08-04 12:03:58,403] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 4 users at a rate of 4.00 per second
[2023-08-04 12:03:58,403] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"User1": 2, "User2": 2} (4 total users)
test_01
test_01
test_02
test_02
[2023-08-04 12:04:00,278] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: --run-time limit reached, shutting down
<locust.env.Environment object at 0x105b778e0>
test is stop
  • ❕结果可看到设置4个用户,两个钩子在模块执行测试之前(后)只执行一次❕

init

Locust创建好Environment和locust runner实例后执行,在test_start之前,例子

from locust import task, HttpUser, events


@events.init.add_listener
def on_test_init(**kwargs):
    print(kwargs.get('environment'))
    print(kwargs.get('runner'))
    print(kwargs.get('web_ui'))
    print('test init fire')


@events.test_start.add_listener
def on_test_start(**kwargs):
    print(kwargs['environment'])
    print('test is start')


class User(HttpUser):

    @task
    def test_01(self):
        print('test_01')
        self.stop()
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_init.py -u 4 -r 4 -t 2 --headless --host https://www.baidu.com --only-summary
<locust.env.Environment object at 0x106bd5d50>
<locust.runners.LocalRunner object at 0x106bd5b40>
None
test init fire
[2023-08-04 12:19:06,528] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Run time limit set to 2 seconds
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
<locust.env.Environment object at 0x106bd5d50>
test is start
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 4 users at a rate of 4.00 per second
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"User": 4} (4 total users)
test_01
test_01
test_01
test_01
[2023-08-04 12:19:08,443] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: --run-time limit reached, shutting down
[2023-08-04 12:19:08,444] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Shutting down (exit code 0)

init_command_line_parser

自定义locust运行命令行参数,例子

import os
import sys

from locust import task, HttpUser, events


@events.init_command_line_parser.add_listener
def _(parser):
    # "--custom-argument" 即命令行参数名
    # type 指定入参类型
    # env_var 指本地环境变量中的key值
    # env_var 、 default 和 命令行输入的参数之间的关系是,若输入该参数并传入非空值,则使用该值,若是空值,则从环境变量中查找env_var值(os.environ[env_var])
    # 入参为空,环境变量os.environ.get(env_var)也是空,才使用default值
    parser.add_argument("--custom-argument", type=str, env_var="HOME", default="kami", help="自己自定义的命令行参数")
    parser.add_argument("--custom-argument2", type=str, env_var="CA2", default="kami2", help="自己自定义的命令行参数")
    parser.add_argument("--custom-argument3", type=str, env_var="HOME", default="kami3", help="自己自定义的命令行参数")


class WebsiteUser(HttpUser):
    host = 'https://www.baidu.com'

    @task
    def my_task(self):
        print(f"HOME={os.environ.get('HOME')}")
        print(f"CA2={os.environ.get('CA2')}")
        print(f"custom_argument={self.environment.parsed_options.custom_argument}")
        print(f"custom_argument2={self.environment.parsed_options.custom_argument2}")
        print(f"custom_argument3={self.environment.parsed_options.custom_argument3}")
        sys.exit(1)
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_init_command_line_parser.py --headless --custom-argument3 "custom-argument3 传参数了"  --only-summary
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: No run time limit set, use CTRL+C to interrupt
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 1 users at a rate of 1.00 per second
[2023-08-04 18:10:11,363] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"WebsiteUser": 1} (1 total users)
HOME=/Users/kamiyeung
CA2=None
custom_argument=/Users/kamiyeung
custom_argument2=kami2
custom_argument3=custom-argument3 传参数了

相关文章

  • 使用Locust进行接口性能测试:安装、命令参数解析与示例解读(一)
    “Locust是一款开源的Python性能测试工具,它可以模拟大量并发用户对网站或者其他接口进行压力测试”一、Locust简介与安装1.使用pip安装Locust:pip3installlocust2.通过GitHub克隆项目并安装(推荐Python3):gitclonehttps://github.com/locustio/locustcdlocustpython......
  • 使用Locust进行接口性能测试:Locust and TaskSet类详细分析(二)
    “Locust是一款开源的Python性能测试工具,它可以模拟大量并发用户对网站或者其他接口进行压力测试”一、Locust类详细说明在Locust中,Locust类是整个负载测试工具的核心。它用于创建并发用户场景,模拟用户行为。示例:fromlocustimportLocust,TaskSet,task#每一个Locust类,......
  • 【wordpress开发必备】新增必填字段相关函数和钩子,适用6.1版本
    当表单包含多个必填字段时,它们的标签可能带有一个带有图例的星号,以说明这些字段是必填的。为了减少代码重复并帮助维护全局一致的标记,WordPress有两个新函数:wp_required_field_indicator()和wp_required_field_message()。如果主题和插件至少需要 WordPress6.1,它们也可以使用这......
  • Locust 压测socket接口样例
    importsocketfromlocustimportUser,task,between,constantclassSocketUser(User):#初始化,建立Socket连接defon_start(self):self.client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)self.client.connect(('localhost',8080))#替换为目标Socket服务......
  • vue-router钩子执行顺序
    Vue的路由在执行跳转时,根据源码可知,调用了router中定义的navigate函数functionpush(to:RouteLocationRaw){returnpushWithRedirect(to)}functionreplace(to:RouteLocationRaw){returnpush(assign(locationAsObject(to),{replace:true}))}functionpush......
  • Unity UGUI的EventSystem(事件系统)组件的介绍及使用
    UnityUGUI的EventSystem(事件系统)组件的介绍及使用1.什么是EventSystem组件?EventSystem是UnityUGUI中的一个重要组件,用于处理用户输入事件,如点击、拖拽、滚动等。它负责将用户输入事件传递给合适的UI元素,并触发相应的事件回调函数。2.EventSystem组件的工作原理EventSystem......
  • Unity UGUI的EventSystem(事件系统)组件的介绍及使用
    UnityUGUI的EventSystem(事件系统)组件的介绍及使用1.什么是EventSystem组件?EventSystem是UnityUGUI中的一个重要组件,用于处理用户输入事件,如点击、拖拽、滚动等。它负责将用户输入事件传递给合适的UI元素,并触发相应的事件回调函数。2.EventSystem组件的工作原理EventSystem......
  • 使用Locust进行分布式性能测试
    Locust是一个强大的性能测试工具,用于评估系统的性能和可扩展性。本文将简洁地介绍使用Locust进行分布式性能测试的步骤和优势。步骤:1.配置测试环境:在主节点和多个从节点上安装相同版本的Locust,并确保网络互通。2.编写Locust脚本:使用Python编写Locust脚本文件,定义虚拟用户的行......
  • locust与jmeter测试过程及结果对比
    JMeter和Locust都是强大的性能测试工具,各自拥有自己的优势和专注领域。JMeter提供了全面的功能和基于GUI的界面,适用于复杂的场景和非技术人员。相比之下,Locust采用了以代码为中心的方法,使开发人员可以创建灵活且易于维护的测试场景。今天对同一系统的同一个测试场景,在本人电脑分......
  • 使用Locust进行性能测试
    当涉及到评估应用程序或服务的性能时,Locust是一个功能强大且易于使用的开源工具。本文将介绍Locust的基本概念和使用方法。什么是Locust?Locust是一个用于编写、运行和分析负载测试的Python框架。它使用简单直观的方式来定义用户行为,并允许模拟大量并发用户对目标系统进行压力测......