環境安裝:python
腳本編寫:json
from locust import HttpLocust, TaskSet, task class UserOne(TaskSet): @task(1) def baidu1(self): self.client.get("/") print "get one request is operation" @task(2) def baidu2(self): requestParam = "key=value" response = self.client.get("/test?"+requestParam) code = response.json().get("code") print response.json() class UserTwo(TaskSet): tasks = {UserOne:2} @task(1) def leave(self): print "get three request is operation" class WebsiteUser(HttpLocust): task_set = UserOne min_wait = 100 max_wait = 600 host = "http://test"
參數解析:cookie
若是想要獲取http請求的返回,須要在get請求時增長參數catch_response=True,可是增長後locust不會作任何校驗甚至不記錄請求事件數據結構
若是須要本身進行數據校驗和請求記錄須要本身實現events.request_success.fire和events.request_failure.fire測試
例如spa
code = response.json().get("code") if code == "200": events.request_success.fire( request_type=response.locust_request_meta["method"], name=response.locust_request_meta["name"], response_time=response.locust_request_meta["response_time"], response_length=response.locust_request_meta["content_size"], ) else: events.request_failure.fire( request_type=response.locust_request_meta["method"], name=response.locust_request_meta["name"], response_time=response.locust_request_meta["response_time"], response_length=response.locust_request_meta["content_size"], exception="Response Code Error! Code:{0}".format(code) )
遇到的坑code