简体   繁体   English

如何重命名 python 蝗虫行动?

[英]How to rename python locust actions?

I have the next code from locustio documentation:我有来自 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)

In the locust logs and locust web (localhost:8089) I see the next tasks在蝗虫日志和蝗虫 web (localhost:8089) 我看到了下一个任务

- /login
- /logout
- /
- /profile

But what if I need to have few requests in one task and get measure from full tasks (not 1 request).但是,如果我需要在一项任务中获得很少的请求并从完整任务(不是 1 个请求)中获取度量,该怎么办。
What I want to see is:我想看到的是:

- login
- logout
- index
- profile

I want to see the names of tasks instead of request url.我想查看任务的名称,而不是请求 url。 In Jmeter I can insert few request in one action and get action time (not request).在 Jmeter 中,我可以在一个动作中插入少量请求并获得动作时间(不是请求)。

You can set the name by name attribute for each request, see the example:您可以为每个请求按name属性设置名称,请参见示例:

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

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

You can achieve this by implementing a custom execute_task() method where you fire the request_success event.您可以通过在触发 request_success 事件的地方实现自定义execute_task()方法来实现这一点。

Something like this should work:像这样的东西应该工作:

import time

class TaskReportingTaskSet(TaskSet):
    def execute_task(self, task, *args, **kwargs):
        start = time.time()
        try:
            super().execute_task(task, *args, **kwargs)
        except:
            events.request_failure.fire(
                request_type="task",  
                name=task.__name__, 
                response_time=(time.time()-start)*1000, 
                response_length=0,
            )
            raise
        else:
            events.request_success.fire(
                request_type="task",  
                name=task.__name__, 
                response_time=(time.time()-start)*1000, 
                response_length=0,
            )

class UserBehavior(TaskReportingTaskSet):
    tasks = ...

With the above code the run time of all the tasks will be reported if the TaskSet inherit from TaskReportingTaskSet .使用上面的代码,如果 TaskSet 继承自TaskReportingTaskSet ,将报告所有任务的运行时间。 request_success events would have to be fired separately if you want to include on_start and on_stop .如果要包含on_starton_stop ,则必须单独触发request_success事件。

If you don't want HTTP requests to be reported, you can simply use an HTTP client that isn't one of the built in Locust HTTP clients.如果您不想报告 HTTP 请求,您可以简单地使用不是内置 Locust HTTP 客户端之一的 HTTP 客户端。 For example you could use python requests directly:例如,您可以直接使用 python 请求:

import requests

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM