三、locust --事件
from locust.runners import (MasterRunner, WorkerRunner, STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP, LocalRunner) from locust import HttpUser, TaskSet, task, events, LoadTestShape, run_single_user from gevent._semaphore import Semaphore import json import traceback import gevent import time '''请求成功时触发''' def on_request_success(request_type, name, response_time, response_length): print( 'success Type: {}, Name: {}, Time: {}ms, length: {}' .format(request_type, name, response_time,response_length)) '''请求失败时触发''' def on_request_failure(request_type, name, response_time, response_length, exception): print('failure Type: {}, Name: {}, Time: {}ms, exception: {}, response_length:{}' .format(request_type, name, response_time, exception, response_length)) '''在执行locust类内发生异常时触发''' def on_locust_error(locust_instance, exception, tb): print("error locust_instance: {}, exception: {}, traceback: {}" .format(locust_instance, exception, traceback.format_tb(tb))) '''退出进程时回调''' def on_quitting(environment, **kwargs): print("Test quit") '''停止测试的时候客户端会调这个方法发送数据到主节点这边''' def on_test_stop(environment, **kwargs): if not isinstance(environment.runner, MasterRunner): print("Cleaning up test data") # 节点往主节点发送的数据 environment.runner.send_message('acknowledge_users', f"Thanks for the Cleaning up test data users!") else: users = [ {"name": "User1"}, {"name": "User2"}, {"name": "User3"}, ] environment.runner.send_message('test_users', users) '''#定义worker节点注册的消息''' def setup_test_users(environment, msg, **kwargs): for user in msg.data: print(f"User {user['name']} received") environment.runner.send_message('acknowledge_users', f"Thanks for the {len(msg.data)} users!") '''定义matser节点注册的消息''' def on_acknowledge(msg, **kwargs): print("recv worker data :{}".format(msg.data)) ''' #主动退出进程时:environment.process_exit_code = 0 #判断错误率大于多少N主动退出进程 #判断响应时间大于多少N主动退出进程 #判断响应时间 ''' def checker(environment): while not environment.runner.state in [STATE_STOPPING, STATE_STOPPED, STATE_CLEANUP]: time.sleep(1) if environment.stats.total.fail_ratio > 0.01: print("Test failed due to failure ratio > 1%,code=1") environment.process_exit_code = 1 '''这个语句是退出''' environment.runner.quit() elif environment.stats.total.avg_response_time > 200: print("Test failed due to average response time ratio > 200 ms,code=1") environment.process_exit_code = 1 elif environment.stats.total.get_response_time_percentile(0.95) > 300: print("Test failed due to 95th percentile response time > 800 ms,code=1") environment.process_exit_code = 1 else: environment.process_exit_code = 0 pass # print("Test Normal task exit code=0") ''' #初始化时绑定的重写方法,该类中进行了worler和master节点的消息注册 ''' def on_locust_init(environment, **kwargs): if not isinstance(environment.runner, MasterRunner): ''' #初始化的时候注册消息,客户端的消息,往客户端的往这个类型发就行 ''' environment.runner.register_message('test_users', setup_test_users) if not isinstance(environment.runner, WorkerRunner): ''' #初始化的时候注册消息,服务端的消息,往后服务端往这个类型发就行 ''' environment.runner.register_message('acknowledge_users', on_acknowledge) if isinstance(environment.runner, MasterRunner) or isinstance(environment.runner, LocalRunner): gevent.spawn(checker, environment) def on_test_start(environment, **kwargs): '''如果运行环境不是主节点''' if not isinstance(environment.runner, MasterRunner): users = [ {"name": "User1"}, {"name": "User2"}, {"name": "User3"}, ] environment.runner.send_message('test_users', users) ''''''''' #创建集合点,当locust实例产生完成时触发 ''' all_locusts_spawned = Semaphore() # 上锁 all_locusts_spawned.acquire() ''' #生成所有locust用户时触发 ''' def on_hatch_complete(**kwargs): # 释放锁 all_locusts_spawned.release() ''' #事件回调绑定 ''' events.spawning_complete.add_listener(on_hatch_complete) events.request_success.add_listener(on_request_success) events.request_failure.add_listener(on_request_failure) events.user_error.add_listener(on_locust_error) events.quitting.add_listener(on_quitting) events.init.add_listener(on_locust_init) ''' #主节点才触发 ''' events.test_start.add_listener(on_test_start) events.test_stop.add_listener(on_test_stop) header = {"Content-Type": "application/json;charset=UTF-8"}