# -*- coding:utf-8 -*- from locust import HttpUser,TaskSet,task import random class UserBehavior(TaskSet): def on_start(self): #设置user和group的参数下表值 self.users_index = 0 self.groups_index = 0 self.auth = ("admin","admin123") @task def test_users(self): user_id = WebsiteUser.user_id[self.users_index] url = "/user/" + str(user_id) + "/" self.client.get(url,auth=self.auth) self.users_index = (self.users_index+1)%len(WebsiteUser.user_id) @task def test_group(self): group_id = WebsiteUser.group_id[self.groups_index] url = "/group/" + str(group_id) + "/" self.client.get(url,auth = self.auth) self.groups_index = (self.groups_index+1)%len(WebsiteUser.group_id) class WebsiteUser(HttpUser): user_id = [1,2] group_id = [1,2,3] tasks = [UserBehavior] min_wait = 3000 max_wait = 6000 host = "http://127.0.0.1:8000"
locust -f F:pyScriptdjango_restfullocust_2021locust_user_group.py --headless -u 10 -r 2 -t 300s
-f 脚本路径
--headless 非web模式
-u 用户数
-r 每秒启用用户数
-t 运行时间 加单位s
分布式压测
locust -f F:pyScriptdjango_restfullocust_2021locust_user_group.py --master
locust -f F:pyScriptdjango_restfullocust_2021locust_user_group.py --worker
--master 主节点
--worker 节点 可以开多个命令一样