import time
from locust import User, env, task, between
from utils.mysql import MySql #上面的 mysql.py文件的 mysql对象from utils.logger import log #此处可使用 系统logging
#log.set_logpath("/mysql/locust/")
# 第一步 构建新协议的客户端 和 第二步 对类的方法进行装饰class MysqlClient(MySql):
"""
Simple, sample XML RPC client implementation that wraps xmlrpclib.ServerProxy and
fires locust events on request_success and request_failure, so that all requests
gets tracked in locust's statistics.
"""
_locust_environment = None #设置默认私有的环境变量
def __getattribute__(self, item): # __getattribute__ """通过该方法对所有的方法进行装饰,能被locust 统计数据
可参考:官方文档或下面链接
https://blog.csdn.net/weixin_36179862/article/details/102829018
"""
func = super().__getattribute__(item) # __getattr__ __getattribute__ if str(type(func)) == "" or str(type(func)) == "":
def wrapper(*args, **kwargs):
if 'locust_name' in kwargs.keys(): # 处理数据统计名字 name = kwargs['locust_name']
kwargs.pop('locust_name')
else:
name = item
start_time = time.time()
try:
result = func(*args, **kwargs)
except Exception as e:
total_time = int((time.time() - start_time) * 1000)
# 添加请求失败事件 self._locust_environment.events.request_failure.fire(request_type="mysql", name=name,
response_time=total_time, exception=e)
else:
total_time = int((time.time() - start_time) * 1000)
#添加请求成功事件 self._locust_environment.events.request_success.fire(request_type="mysql", name=name,
response_time=total_time,
response_length=0)
# In this example, I've hardcoded response_length=0. If we would want the response length to be # reported correctly in the statistics, we would probably need to hook in at a lower level log.info("{} 耗时total_time:{} reslut:\n{}".format(name, total_time, result))
return result
return wrapper
else:
return func
# 第三步 继承user类,并初始化客户端和设置环境变量class MysqlUser(User):
"""
This is the abstract User class which should be subclassed. It provides an XML-RPC client
that can be used to make XML-RPC requests that will be tracked in Locust's statistics.
"""
abstract = True
def __init__(self, *args, **kwargs):
# super(MysqlUser, self).__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.client = MysqlClient()
self.client._locust_environment = self.environment
# 第四步 使用继承了user的类,产生新协议的用户class ApiUser(MysqlUser):
host = "http://10.2.1.95:3318/"
wait_time = between(0.1, 1)
def on_start(self):
log.info("开始登陆:")
user = 'root'
pwd = '123456'
host = '10.2.1.95'
dbname = 'coreframe'
self.client.creat_conn(host, user, pwd, dbname, port=3318)
@task(1)
def test_mysql(self):
# log.info("self.client2:", self.client,self.environment) sql = """
select count(*) from om_employee
"""
name = "select count(*) from LD16090835"
data = self.client.query(sql, locust_name=name)
log.info("data:{}".format(data))
# [log.info(('on_start:', s)) for s in [self.environment.stats.total]]
if __name__ == '__main__':
import subprocess, os
path = os.path.dirname(os.path.abspath(__file__))
file_name = os.path.split(__file__)[-1].split(".")[0]
default_url = 'http://10.2.1.95:3318/'
log.info(path, file_name)
log.info("开始运行wt") # --no-web -c 2 -r 1 -t 3s # locust -f --headless -u 1000 -r 100 --run-time 1h30m --step-load --step-users 300 --step-time 20m # subprocess.call( # 'locust -f {}/{}.py -u 1 -r 1 -t 10 -l --csv-full-history'.format(path,file_name, default_url), # shell=True) cmd = 'locust -f {}/{}.py --host={} --web-host="127.0.0.1" --web-port 8090 --csv CSV_{}'.format(path, file_name,
default_url,
file_name)
log.info("cmd:", cmd)
dd = subprocess.call(cmd, shell=True) ##--web-port 8090 # d=subprocess.check_call('locust -f {}/{}.py --worker'.format(path,file_name, default_url),shell=True) # d1=subprocess.check_call('locust -f {}/{}.py --worker'.format(path,file_name, default_url),shell=True) d2 = subprocess.call(
'locust -f {}/{}.py --master --host={} --web-host="127.0.0.1" --web-port 8090 --csv CSV_{}'.format(path,
file_name,
default_url,
file_name),
shell=True)