Locust的API介绍-----ResponseContextManager类InterruptTaskSet Exception及Event hooks

来源:互联网 发布:3ds max模型导出优化 编辑:程序博客网 时间:2024/05/20 10:21
ResponseContextManager类
class ResponseContextManager(response)
一个响应类,它也充当上下文管理器,提供手动控制HTTP请求如何被标记为成功或者在Locust统计信息中失败的能力


这个类是一个Response具有两个附加方法的子类:success和 failure。


failure(exc )
报告响应失败。


exc可以是python异常,也可以是一个字符串,在这种情况下,它将被包装在CatchResponseError中。


例:


with self.client.get("/", catch_response=True) as response:
    if response.content == "":

        response.failure("No data")


success()
报告响应成功


例:


with self.client.get("/does/not/exist", catch_response=True) as response:
    if response.status_code == 404:

        response.success()


InterruptTaskSet Exception
exception InterruptTaskSet(reschedule = True )
在一个任务中抛出一个蝗虫的异常


Event hooks
事件钩子是locust.events.EventHook类的实例:


class EventHook
简单的事件类用于为Locust中不同类型的事件提供钩子。


以下是使用EventHook类的方法:


my_event = EventHook()
def on_my_event(a, b, **kw):
    print "Event was fired with arguments: %s, %s" % (a, b)
my_event += on_my_event

my_event.fire(a="foo", b="bar")


Available hooks
以下事件钩子在locust.events模块下可用:


request_success = <locust.events.EventHook object>
请求成功完成时,会触发request_success。

Listeners应该采取以下参数:
request_type:请求类型方法
name:被调用的URL的路径(如果在调用客户端时使用该名称,则覆盖该名称)

response_time:以毫秒为单位的响应时间

response_length:响应的内容长度


request_failure = <locust.events.EventHook object>
请求失败时触发request_failure
事件由以下参数触发:

request_type:请求类型方法
name:被调用的URL的路径(如果在调用客户端时使用该名称,则覆盖该名称)
response_time:抛出异常之前以毫秒为单位的时间

exception:抛出的异常实例


locust_error = <locust.events.EventHook object>
当Locust类的执行中发生异常时,会触发locust_error。
事件由以下参数触发:
locust_instance:发生异常的Locust类实例
exception:被抛出的异常

tb:Traceback对象(来自sys.exc_info()[2])


report_to_master = <locust.events.EventHook object>
当Locust以-slave模式运行时,使用report_to_master。它可以用于将数据附加到定期发送给主服务器的数据库中。当报告要发送到主服务器时,它会定期启动。

请注意,“stats” 和“errors”由Locust使用,不需要被覆盖。

事件由以下参数触发:
client_id:运行的Locust进程的客户端ID。

data:可以修改数据,以便附加应发送到主站的数据。


slave_report = <locust.events.EventHook object>
当Locust以“主”模式运行时,将使用slave_report,并在主服务器从Locust从属服务器接收到报告时触发。
此事件可用于汇总来自locust从属服务器的数据。
事件由以下参数触发:

client_id:locust从服务端的ID

data:来自从节点的数据


hatch_complete = <locust.events.EventHook object>
当所有locust用户产生时,hatch_complete被触发。
事件由以下参数触发:

user_count:生成的用户数


quitting = <locust.events.EventHook object>
当locust进程退出时,停止测试