from locust import TaskSet, task
from locust import HttpUser as HttpLocust
class UserBehavior(TaskSet):
def on_start(self): # 当模拟用户开始执行TaskSet类的时候,on_start方法会被调用
self.index = 0
self.share_data = ['/', '/archives/', '/about/', '/archives/2018/05/', '/archives/2018/02/'] # 共享数据,循环遍历使用
@task
def test_visit(self):
url = self.share_data[self.index] # 取 self.locust.share_data<等于 WebsiteUser 类定义的 share_data >中的第self.index 个元素
self.index = (self.index + 1) % len(self.share_data) # self.index 的值小于 self.locust.share_data 的长度,循环生成 <0.1.2.3.4、0.1.2.3.4...>
#这里可以判断请求方式切换请求
r = self.client.get(url) # TaskSet类有一个client属性,返回self.locust.client
assert r.status_code == 200
class WebsiteUser(HttpLocust):
# host = 'http://debugtalk.com'
tasks = [UserBehavior]
min_wait = 1000
max_wait = 3000
if __name__ == "__main__":
import os
os.system("/usr/local/bin/locust -f /Users/lucax/Downloads/EasyTest-master/locustfile.py --host=https://baidu.com")
标签:__,index,task,share,self,locust,嵌入,data From: https://www.cnblogs.com/kaibindirver/p/16830653.html