locust的 -T,--tags使用

官网的TAG配置说明:
-T [TAG [TAG ...]], --tags [TAG [TAG ...]]List of tags to include in the test, so only tasks with any matching tags will be executed
使用-T(大写)或者--tags都可以,命令里输入的标签是要被执行压测的 -E [TAG [TAG ...]], --exclude-tags [TAG [TAG ...]] List of tags to exclude from the test, so only tasks with no matching tags will be executed
使用-E(大写)或者
--exclude-tags都可以,命令里输入的标签是不会被执行压测的


TAG方法说明:
tag decorator

tag(*tags)

Decorator for tagging tasks and TaskSets with the given tag name. You can then limit the test to only execute tasks that are tagged with any of the tags provided by the --tags command-line argument. Example:

class ForumPage(TaskSet):
    @tag('thread')
    @task(100)
    def read_thread(self):
        pass

    @tag('thread')
    @tag('post')
    @task(7)
    def create_thread(self):
        pass

    @tag('post')
    @task(11)
    def comment(self):
        pass
在需要标签区分执行的方法前通过tag装饰类引入标签即可,可以同时打上多个标签
执行时候得命令:直接在命令后面加上该参数即可,若是分布式压测,只需在slave里加即可,如:
master:
locust -f D:\***.py  --master  --master-bind-port 9800 --headless -u 1000 -r 50 --expect-workers 1 -t 10m -s 10  --csv D:\theco***y0106115648  
slave:
locust -f D:\***.py --master-host  170.240.110.245 --master-port 9800 --headless --worker -T "test1" "test2" "test3"
这样就只执行含有test1,test2,test3标签的方法
当然,-E也是如此
但是 -T有个坑:
比如:
复制代码
    @tag('test1')
    @task
    def api_api_channel_item_list(self):
        headers = {'tenantId': 'FA9DA2211F880F10', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
        # 请求参数组装 ## r_url:固定参数
        r_url = "/api/channel/itemList"
        requests_data = {'channelId': '11', 'pageSize': '3', 'page': '1', 'queryType': '2', 'queryAllImg': 'true', 'client': 'WEB'}
        # 发起请求
        with self.client.post(r_url, data=requests_data, catch_response=True, name=r_url) as r:
            if r.content == b"":
                r.failure("No data")
            if r.status_code != 200:
                em = "request error --" + str(r.status_code)
                r.failure(em)

    @tag('test2')
    @task
    def api_api_channel_item_list(self):
        headers = {'tenantId': 'FA9DA2211F880F10', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
        # 请求参数组装 ## r_url:固定参数
        r_url = "/api/channel/itemList"
        requests_data = {'channelId': '11', 'pageSize': '3', 'page': '1', 'queryType': '2', 'queryAllImg': 'true', 'client': 'WEB'}
        # 发起请求
        with self.client.post(r_url, data=requests_data, catch_response=True, name=r_url) as r:
            if r.content == b"":
                r.failure("No data")
            if r.status_code != 200:
                em = "request error --" + str(r.status_code)
                r.failure(em)
复制代码

 

 

以上的方法名是一样的,但一个打的标签是test1,一个打的是test2

当执行-T "test1"时,locust会报错:

Traceback (most recent call last):
  File "d:\python\lib\site-packages\locust\user\task.py", line 285, in run
    self.schedule_task(self.get_next_task())
  File "d:\python\lib\site-packages\locust\user\task.py", line 417, in get_next_task
    raise Exception(
Exception: No tasks defined on SYLocust. use the @task decorator or set the tasks property of the User (or mark it as abstract = True if you only intend to subclass it)

-E倒是不会错

如果不是按场景压测的话,一般是不会遇到上面问题的,若压测代码是自动生成的话,相同的方法打上相同的标签即可,不会存在这个问题。

Locust QQ 群:

 







posted @   drewgg  阅读(745)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示