locust性能压测连接mysql,随机取出班级,绑定学生

from locust import HttpLocust, TaskSet, task
import pymysql
import random

class UserBehavior(TaskSet):

def on_start(self):
db = pymysql.connect("ip地址", "用户名", "密码", "数据库")
cursor = db.cursor()
cursor.execute("SELECT id,code from zj_class WHERE name like '%测试创建班级%' limit 20001")
self.data = cursor.fetchall()
db.close()
self.bindstudents()


def login_user(self):
zhi=random.randint(0,20000)
classid=self.data[zhi][0]
code=self.data[zhi][1]
print(classid,code)
return(classid, code)

@task
def bindstudents(self):
classid,code=self.login_user()

self.client.post("/wx/bind/add/MQ/openid", {"class_id": classid,
"stuname": "李丽芳22",
"code": code,
"isVerify": "N"
})

# @task(1)
# def baidu(self):
# self.client.get("/")



class WebsiteUser(HttpLocust):
host=""
task_set = UserBehavior
min_wait = 0
max_wait = 0
原文地址:https://www.cnblogs.com/wlyd/p/9271953.html