官网的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 decoratortag
(*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-worker 1 -t 10m -s 10 --csv D: heco***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:pythonlibsite-packageslocustuser ask.py", line 285, in run self.schedule_task(self.get_next_task()) File "d:pythonlibsite-packageslocustuser ask.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倒是不会错
如果不是按场景压测的话,一般是不会遇到上面问题的,若压测代码是自动生成的话,相同的方法打上相同的标签即可,不会存在这个问题。