# 导入locust相关的类
from locust import HttpUser, TaskSet, task, between
# 任务类
class TestIndex(TaskSet):
def on_start(self):
self.data=""
@task(1)
def test_index(self):
res=self.client.get("/")
print("here")
@task(1) #任务项2,这里面数字越大,执行权重比越高,不设置默认1
def baidu_page1(self):
res= self.client.get("/", catch_response=True)#client执行是requests.request方法,传参一致
a = res.text
print('1111')
class WebsiteUser(HttpUser):
tasks = [TestIndex]
wait_time = between(0.5, 1)#执行事务的暂停时间下限和上限
运行方法:
通过设置断言,判断链接结果是否正确;
参考资料:
官方文档:API — Locust 2.9.0 documentationhttps://docs.locust.io/en/stable/api.html#task-decorator 视频教程:Python+Locust性能测试_哔哩哔哩_bilibili蜗牛学院,致力于中国互联网领域的IT高端人才培养。只为成就更好的你!希望学习更多技术,继续在IT行业突破提升自己的各位朋友,欢迎加群594154674,更多学习视频欢迎进入官网www.woniuxy.com,不管你自我感觉牛不牛B!
https://www.bilibili.com/video/BV16441157rv?spm_id_from=333.337.search-card.all.click
-----------------------------------节点6.21--------------------------------------------------------------
今天发现使用locust,如果task类使用了全局变量
itemBodyData = get_body(i, random.randint(1, 5000), dateindex)
这里的dateindex是全局变量初始值为0,我在main方法for循环修改dateindex,但透传不进去,locust每次执行task时,dateindex的值还是一开始的0
for i in range(0,4):
dateindex = i
print(f"产生第{i}天后数据")
print_dateindex()
# 启用locust开始发送日志,-t60则是每天10万,下面-t6是每天一万
os.system(f'nohup locust -f log_a.py --master-bind-port=11111 --headless -u15 -r1 -t6 >mylocust.log 2>&1 &')
# 是否发送完成,没发送完成就等待
isProcessExit =len((os.popen('ps -auxf | grep locust | grep -v grep')).readlines())
while isProcessExit != 0:
time.sleep(10)
isProcessExit = len((os.popen('ps -auxf | grep locust | grep -v grep')).readlines())
i+=1
备用解决方法:
task类task方法里,将dateindex改为随机数,均分打,效果一样
dateindex = random.randint(0, 4)
-----------------------------------节点6.24--------------------------------------------------------------
负载分发
工作进程,起三个
locust -f log_a.py --master-port=11111 --worker &
locust -f log_a.py --master-port=11111 --worker &
locust -f log_a.py --master-port=11111 --worker &
管理进程(--expect-workers=3 调度上面三个进程)
locust -f log_a.py --master --expect-workers=3 --master-bind-port=11111
-----------------------------------节点7.19--------------------------------------------------------------
# 无论任务执行时间如何,任务总是每 1秒执行一次
wait_time = constant_pacing(1)