Locust HTTP client

Http client 默认是以安全模式运行的,任何由于连接错误、超时或者类似错误引起的异常,都会返回一个空的Response对象,这个请求将会再locust统计中标记为failure,返回的虚拟对象Response’content属性将会被置为空,status_code为0。一般不用再使用try...exception处理异常。

默认情况下,返回2xx的状态码的都会标记为success,之外的情况都会被标记为failure,如果想手动改变这种情况,需要手动控制。

 

实例:如果返回的内容不包含Success文本,则标记为失败;如果返回码不是404失败

class UserTask1(TaskSet):
    @task
    def task(self):
        with self.client.get("/", catch_response=True) as response:
            if response.status_code == 400:
                response.success()
            else:
                response.failure(response)

class UserTask2(TaskSet):
    @task
    def task(self):
        with self.client.get("/belle-ls/", catch_response=True, verify=False) as response:
            if response.content != b"Success":
                response.failure("Failed")

 

请求时参数替换

# Statistics for these requests will be grouped under: /blog/?id=[id]
for i in range(10):
    client.get("/blog?id=%i" % i, name="/blog?id=[id]")

 

猜你喜欢

转载自www.cnblogs.com/belle-ls/p/10491590.html