如何重命名python蝗虫操作?

如何重命名python蝗虫操作?,python,performance-testing,locust,taurus,Python,Performance Testing,Locust,Taurus,我有locustio文档中的下一个代码: from locust import HttpLocust, TaskSet, between def login(l): l.client.post("/login", {"username":"ellen_key", "password":"education"}) def logout(l): l.client.post("/logout", {"username":"ellen_key", "password":"educat

我有locustio文档中的下一个代码:

from locust import HttpLocust, TaskSet, between

def login(l):
    l.client.post("/login", {"username":"ellen_key", "password":"education"})

def logout(l):
    l.client.post("/logout", {"username":"ellen_key", "password":"education"})

def index(l):
    l.client.get("/")

def profile(l):
    l.client.get("/profile")

class UserBehavior(TaskSet):
    tasks = {index: 2, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    wait_time = between(5.0, 9.0)
在蝗虫日志和蝗虫网络(localhost:8089)中,我看到了接下来的任务

- /login
- /logout
- /
- /profile
但是,如果我需要在一个任务中有几个请求,并从完整的任务(而不是一个请求)中获取度量值,该怎么办呢

- login
- logout
- index
- profile
我想查看任务的名称,而不是请求url。
在Jmeter中,我可以在一个操作中插入几个请求并获得操作时间(不是请求)。

您可以通过实现一个定制的
执行任务()
方法来实现这一点,在该方法中触发请求成功事件

像这样的方法应该会奏效:

导入时间
类TaskReportingTaskSet(任务集):
def execute_任务(self、task、*args、**kwargs):
开始=时间。时间()
尝试:
super().执行_任务(任务,*args,**kwargs)
除:
events.request\u failure.fire(
请求\u type=“任务”,
名称=任务,
响应时间=(time.time()-start)*1000,
响应长度=0,
)
提升
其他:
events.request\u success.fire(
请求\u type=“任务”,
名称=任务,
响应时间=(time.time()-start)*1000,
响应长度=0,
)
类用户行为(TaskReportingTaskSet):
任务=。。。
使用上述代码,如果任务集继承自
TaskReportingTaskSet
,则将报告所有任务的运行时<如果要在启动时包含
,在停止时包含
,则必须单独触发代码>请求成功
事件

如果您不想报告HTTP请求,只需使用一个HTTP客户端,它不是内置的蝗虫HTTP客户端之一。例如,您可以直接使用python请求:

导入请求
def索引(l):
请求。获取(“/”)

您可以通过实现一个定制的
execute\u task()
方法来实现这一点,您可以在该方法中触发request\u success事件

像这样的方法应该会奏效:

导入时间
类TaskReportingTaskSet(任务集):
def execute_任务(self、task、*args、**kwargs):
开始=时间。时间()
尝试:
super().执行_任务(任务,*args,**kwargs)
除:
events.request\u failure.fire(
请求\u type=“任务”,
名称=任务,
响应时间=(time.time()-start)*1000,
响应长度=0,
)
提升
其他:
events.request\u success.fire(
请求\u type=“任务”,
名称=任务,
响应时间=(time.time()-start)*1000,
响应长度=0,
)
类用户行为(TaskReportingTaskSet):
任务=。。。
使用上述代码,如果任务集继承自
TaskReportingTaskSet
,则将报告所有任务的运行时<如果要在启动时包含
,在停止时包含
,则必须单独触发代码>请求成功
事件

如果您不想报告HTTP请求,只需使用一个HTTP客户端,它不是内置的蝗虫HTTP客户端之一。例如,您可以直接使用python请求:

导入请求
def索引(l):
请求。获取(“/”)

您可以通过每个请求的
name
属性设置名称,请参见示例:

def index(l):
  l.client.get("/", name="index")

def profile(l):
  l.client.get("/profile", name="my-profile")

您可以通过
name
属性为每个请求设置名称,请参见示例:

def index(l):
  l.client.get("/", name="index")

def profile(l):
  l.client.get("/profile", name="my-profile")

我可以将此方法用于装饰器
@task(weight)
?例如
@task(2)def execute_task…
非常好的方法!谢谢但是如果执行任务失败怎么办?在这种情况下,如果任务引发任何异常,则会触发
事件.request\u success
以触发
request\u failure
事件。我是否可以将此方法用于装饰程序
@task(weight)
?例如
@task(2)def execute_task…
非常好的方法!谢谢但是如果执行任务失败怎么办?在这种情况下,
事件。如果任务引发任何异常,请求成功
将被触发,因为我已更新了答案以触发一个
请求失败
事件。