Locust 官方文檔 9:擴展 Locust

Locust comes with a number of events hooks that can be used to extend Locust in different ways.

Locust 提供了很多的 events hooks,可以用來在不同情況下擴展 Locust。

Event hooks live on the Environment instance under the events attribute.

Event hooks 存在於 Environment 類的實例中的 events 屬性。

However, since the Environment instance hasn’t been created when locustfiles are imported, the events object can also be accessed at the module level of the locustfile through the locust.events variable.

由於 Environment 實例在 locustfiles 導入時還未創建實例,事件對象也可以通過 locust.events 變量在 locustfile 中通過模塊級別引入。

Here’s an example on how to set up an event listener:

以下是一個設置事件監聽的示例:

from locust import events

@events.request_success.add_listener
def my_success_handler(request_type, name, response_time, response_length, **kw):
    print("Successfully made a request to: %s" % name)

Note

It’s highly recommended that you add a wildcard keyword argument in your listeners (the **kw in the code above), to prevent your code from breaking if new arguments are added in a future version.

注意:

強烈建議在監聽器中添加通配符關鍵字參數(上面代碼中的 **kw),以防止在以後的版本中添加新參數時導致監聽器代碼不可用。

See also

To see all available event, please see Event hooks.

如果要查看所有可用的事件,請參見 Event hooks

添加 web 路由

Locust uses Flask to serve the web UI and therefore it is easy to add web end-points to the web UI.

Locust 使用 Flask 作爲 web UI 的服務架構,因此很容易添加 web 端點到 Locust 的 web UI。

By listening to the init event, we can retrieve a reference to the Flask app instance and use that to set up a new route:

通過監聽 init (初始化事件),我們可以引用 Flask app 的實例併爲其設置一個新路由:

from locust import events

@events.init.add_listener
def on_locust_init(web_ui, **kw):
    @web_ui.app.route("/added_page")
    def my_added_page():
        return "Another page"

You should now be able to start locust and browse to http://127.0.0.1:8089/added_page

你現在應該可以啓動 Locust 並通過瀏覽器查看 http://127.0.0.1:8089/added_page

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章