本文由dongfanger转载自微信公众号「dongfanger」。转载本文请联系东方儿公众号。Tasks和Events是Locust性能测试工具的核心技术。有了它们,Locust就可以称为性能工具了。Tasks从上一篇文章知道locustfile中肯定有一个类,它继承了User类。当性能测试开始时,会生成一个User类的实例,通常称为模拟用户。这些用户会选择任务执行,睡一会,然后选择新的任务不断迭代。Task是Python中的可调用对象。这是一项任务。对于web系统来说,可以是登录、查询、订单、支付等。@task装饰器@task是定义任务最简单直接的方式,例如:fromlocustimportUser,task,constantclassMyUser(User):wait_time=constant(1)@taskdefmy_task(self):print("Userinstance(%r)executingmy_task"%self)@task有一个可选参数用来设置任务的选择权重,例如:fromlocustimportUser,task,betweenclassMyUser(User):wait_time=between(5,15)@task(3)deftask1(self):pass@task(6)deftask2(self):passtask2被选中的可能性是task1的两倍。除了@task装饰器,tasks属性还可以设置User类的tasks属性来定义任务,如:fromlocustimportUser,constantdefmy_task(user):passclassMyUser(User):tasks=[my_task]wait_time=constant(1)注意,my_task()函数有一个参数,是User类的一个实例。tasks可以是一个列表:tasks=[my_task1,my_task2,my_task3]Locust将使用Python中的random.choice()从中随机选择。tasks也可以是一个字典:{my_task:3,another_task:1}后面的int键值代表选择的权重,这个字典相当于一个列表:[my_task,my_task,my_task,another_task]@tagdecorator@标签使用标记和选择哪些任务在运行时执行,哪些任务不执行。例如:classMyUser(User):wait_time=constant(1)@tag('tag1')@taskdeftask1(self):pass@tag('tag1','tag2')@taskdeftask2(self):pass@tag('tag3')@taskdeftask3(self):pass@taskdeftask4(self):pass如果使用--tagstag1,只会选择task1和task2。如果使用--tagstag2tag3,则只会选择task2和task3。如果使用--exclude-tagstag3,则只会选择task1、task2和task4。请注意,exclude具有更高的优先级。如果一个标签既被包含又被排除,它将被排除。Events@task定义了性能测试的执行动作,@events作为补充,定义了测试开始前和测试结束后的处理。请注意,每个模拟用户的开始和结束是使用User类的on_start()和on_stop()方法处理的。test_start和test_stop在测试开始前和测试结束后触发。例子:fromlocustimportevents@events.test_start.add_listenerdefon_test_start(environment,**kwargs):print("Anewtestisstarting")@events.test_stop.add_listenerdefon_test_stop(environment,**kwargs):print("Anewtestisending")会在master上生效节点。initinit不同于test_start,它会在每个Locust进程开始时触发,分布式执行时每个节点(worker进程)都会生效。fromlocustimporteventsfromlocust.runnersimportMasterRunner@events.init.add_listenerdefon_locust_init(environment,**kwargs):ifisinstance(environment.runner,MasterRunner):print("I'monmasternode")else:print("I'monaworkerorstandalonenode")Events是hook技术,然后在学习Locust的高级用法的时候进一步介绍。Locust项目结构官方建议如下:common/__init__.pyauth.pyconfig.pylocustfile.pyorlocustfiles/api.pywebsite.pyrequirements.txtFastHttpUser从上一篇文章可以知道HttpUser类比User类,其client属性为HttpSession类(requests.Session子类的实例),可以使用requests发送HTTP请求:#UseHttpUserfromlocustimportHttpUser,task,constantclassMyUser(User):wait_time=constant(1)@taskdefmy_task1(self):withself.client.get("https://www.baidu.com/",catch_response=True)asres:ifres.status_code==200:print("成功")else:print("失败")但是请求的性能不是很好,如果想生成更高的建议使用FastHttpUser,性能可以提升5到6倍:(1)@taskdefmy_task(self):withself.client.get("https://www.baidu.com/",catch_response=True)asresponse:ifresponse.status_code==200:print("success")else:print("failure")因为他们的API是不同的是,它们都有各自适用的场景。所以FastHttpUser并不能完全替代HttpUser。总结这篇文章严格来说是上一篇文章?的下一篇。介绍核心技术Tasks和Events,给出官方推荐的项目结构。最后介绍了FastHttpUser,其性能优于HttpUser。如果你想要更高的Concurrency,可以考虑使用后者。学习完这两篇文章,你已经可以开始练习使用Locust进行性能测试了。如果使用locust命令后无法打开网页,可以尝试添加参数:locust--web-host="127.0.0.1"。参考资料:https://zhuanlan.zhihu.com/p/118470760https://docs.locust.io/en/stable/writing-a-locustfile.html#taskshttps://www.axihe.com/tools/locust/increase-performance.htmlhttps://blog.csdn.net/u012002125/article/details/113363768
