Tasks和Events是Locust性能测试工具的核心技术,有了它们,Locust才能称得上是一个性能工具。

Tasks

从上篇文章知道,locustfile里面必须要有一个类,继承User类,当性能测试开始后,会产生一个User类实例,这就是常说的模拟用户。这些用户会选择task执行,休眠一会,再选择新的task,不断迭代。

task是Python中的可调用对象,它是一项任务,对于Web系统来说,可以是登录、查询、下订单、支付等等。

@task装饰器

@task是定义task最简单直接的方式,比如:

from locust import User, task, constant

class MyUser(User):
wait_time = constant(1) @task
def my_task(self):
print("User instance (%r) executing my_task" % self)

@task有一个可选参数,用来设置task的选择权重,比如:

from locust import User, task, between

class MyUser(User):
wait_time = between(5, 15) @task(3)
def task1(self):
pass @task(6)
def task2(self):
pass

task2比task1被选择的可能性大两倍。

tasks属性

除了@task装饰器,还可以设置User类的tasks属性来定义任务,比如:

from locust import User, constant

def my_task(user):
pass class MyUser(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]

@tag装饰器

@tag用来打标记,在运行时选择哪些task执行,哪些task不执行。比如:

class MyUser(User):
wait_time = constant(1) @tag('tag1')
@task
def task1(self):
pass @tag('tag1', 'tag2')
@task
def task2(self):
pass @tag('tag3')
@task
def task3(self):
pass @task
def task4(self):
pass
  • 如果使用--tags tag1,那么只有task1和task2会被选择。
  • 如果使用--tags tag2 tag3,那么只有task2和task3会被选择。
  • 如果使用--exclude-tags tag3,那么只有task1、task2和task4会被选择。

注意,exclude的优先级更高,如果某个tag既包括又排除,那么会被排除。

Events

@task定义了性能测试的执行动作,@events作为补充,定义了测试开始前和测试结束后的处理。

注意,每个模拟用户开始和结束的处理是使用的User类的on_start()和on_stop()方法。

test_start和test_stop

测试开始前和测试结束后触发。示例:

from locust import events

@events.test_start.add_listener
def on_test_start(environment, **kwargs):
print("A new test is starting") @events.test_stop.add_listener
def on_test_stop(environment, **kwargs):
print("A new test is ending")

分布式执行时,它们只会在master节点生效。

init

init和test_start不同,它会在每个Locust进程开始时触发,分布式执行时,每个节点(worker进程)都会生效。

from locust import events
from locust.runners import MasterRunner @events.init.add_listener
def on_locust_init(environment, **kwargs):
if isinstance(environment.runner, MasterRunner):
print("I'm on master node")
else:
print("I'm on a worker or standalone node")

Events是一项hook技术,在学习Locust高级用法时再做进一步介绍。

Locust项目结构

官方建议如下:

common/

__init__.py

auth.py

config.py

locustfile.py或者locustfiles/

api.py

website.py

requirements.txt

FastHttpUser

从上篇文章可以知道,HttpUser类比User类更常用,它的client属性是HttpSession类(requests.Session子类)的一个实例,可以使用requests发HTTP请求:

# 使用HttpUser
from locust import HttpUser,task,constant class MyUser(User):
   wait_time = constant(1)
   
   @task
   def my_task1(self):
       with self.client.get("https://www.baidu.com/", catch_response=True) as res:
           if res.status_code == 200:
               print("成功")
           else:
               print("失败")

但是requests性能是不太好的,如果要产生更高的压力,建议使用FastHttpUser,性能可以提升5到6倍

# 使用FastHttpUser
from locust import task, constant
from locust.contrib.fasthttp import FastHttpUser class MyUser(FastHttpUser):
wait_time = constant(1) @task
def my_task(self):
with self.client.get("https://www.baidu.com/", catch_response=True) as response:
if response.status_code == 200:
print("成功")
else:
print("失败")

由于它们的API不一样,都有各自的适用场景,所以FastHttpUser不能完全替代HttpUser。

小结

本文严格来说是上篇文章《locustfile中的User类和HttpUser类》的下篇,介绍了核心技术Tasks和Events,并给出了官方推荐的项目结构,最后介绍了比HttpUser性能更好的FastHttpUser,如果想要更多的并发,可以考虑使用后者。经过这两篇文章的学习,已经可以开始动手实践使用Locust进行性能测试了。如果使用locust命令启动后,无法打开网页,可以试试加上参数:locust --web-host="127.0.0.1"

参考资料:

https://zhuanlan.zhihu.com/p/118470760

https://docs.locust.io/en/stable/writing-a-locustfile.html#tasks

https://www.axihe.com/tools/locust/increase-performance.html

https://blog.csdn.net/u012002125/article/details/113363768

Locust性能测试工具核心技术@task和@events的更多相关文章

  1. Locust 性能测试工具安装使用说明

    1. 介绍     它是一个开源性能测试工具.使用 Python 代码来定义用户行为.用它可以模拟百万计的并发用户访问你的系统. 性能工具对比 LoadRunner 是非常有名的商业性能测试工具,功能 ...

  2. Locust性能测试工具的安装及实际应用

    一.安装Locust 安装Locust之前先安装的库:gevent库:第三方库,gevent为python提供了比较完善的协程支持.使用gevent,可以获得极高的并发性能. pip install ...

  3. 开源性能测试工具Locust使用篇(二)

    那如何理解Locust和TaskSet这两个类呢? class HttpLocust(Locust) 在Locust类中,具有一个client属性,它对应着虚拟用户作为客户端所具备的请求能力,也就是我 ...

  4. 性能测试工具Locust

    An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...

  5. 性能测试工具Locust的使用

    一.写在前面 官网:https://www.locust.io/ 官方使用文档:https://docs.locust.io/en/latest/ 大并发量测试时,建议在linux系统下进行. 二.L ...

  6. Python3中性能测试工具Locust安装使用

    Locust安装使用: 安装: python3中           ---> pip3 install locust 验证是否安装成功---> 终端中输入 locust --help  ...

  7. 性能测试工具Locust,一个开源性能测试工具

    性能测试工具Locust,一个开源性能测试工具使用Python代码来定义用户行为.用它可以模拟百万计的并发用户访问你的系统.1.它与目前主流的LoadRunner和Jmeter玩法都不一样.2.它完全 ...

  8. 性能测试工具Locust的介绍和使用

    内容来自网络 https://www.w3xue.com/exp/article/20191/16707.html https://blog.csdn.net/qq_36255988/article/ ...

  9. 基于python的性能测试工具–locust

    现在有很多的性能测试工具,比如说我们熟悉的loadrunner.jmeter.ab.webbench等等,这些工具如果对一个没用过的朋友来说,学习起来比较不容易,但是如果你能看懂python代码,会写 ...

随机推荐

  1. 网络编程Netty入门:ByteBuf分析

    目录 Netty中的ByteBuf优势 NIO使用的ByteBuffer有哪些缺点 ByteBuf的优势和做了哪些增强 ByteBuf操作示例 ByteBuf操作 简单的Demo示例 堆内和堆外内存 ...

  2. Squares UVA - 201

    A children's board game consists of a square array of dots that contains lines connecting some of th ...

  3. 编写一个简单的flask的前后端交互的网页(flask简单知识的讲解)

    实验原理: 1.什么是flask Flask是一个使用Python编写的轻量级Web应用框架,其WSGI工具采用Werkzeng,模板引擎使用Jinja2.Flask与 Django之间的区别就是Dj ...

  4. 开源Influxdb2高性能客户端

    前言 最近我在了解时序数据库Influxdb 2.x版本,体验一翻之后,感觉官方的出品的.net客户端还有很多优化的地方,于是闭关几天,不吃不喝,将老夫多年练就的高性能网络通讯与高性能Buffer操作 ...

  5. 浅谈Java的反射机制和作用

    浅谈Java的反射机制和作用 作者:Java大师 欢迎转载,转载请注明出处 很多刚学Java反射的同学可能对反射技术一头雾水,为什么要学习反射,学习反射有什么作用,不用反射,通过new也能创建用户对象 ...

  6. PAT归纳总结——关于模拟类问题的一些总结

    关于时间的模拟问题 在刷题的过程中碰到了一些关于时间先后顺序的模拟题目,刚开始做的时候确实挺麻烦的,今天把这类问题的解题思路来整理一下. 比较典型的有: 1017 Queueing at Bank 1 ...

  7. 1058 A+B in Hogwarts

    If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- a ...

  8. 硬件篇-02-TX2刷机Jetpack4.3

    Jetson TX2收货将近一周了,期间趁摄像头还没到,预先给TX2配置了环境.由于学校要求写毕设日志,故干脆一起在知乎写了,顺带帮助想要给TX2配环境却苦于网络上关于TX2的教程很少的同学. 本期内 ...

  9. SpringCloud-Stream消息通信

    SpringCloud微服务实战系列教程 Spring Cloud Stream 消息驱动组件帮助我们更快速,更⽅便,更友好的去构建消息驱动微服务的.当时定时任务和消息驱动的⼀个对⽐.(消息驱动:基于 ...

  10. Android APK程序的smali动态调试

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/71250622 一.Apktool的下载和安装 Apktool是Android逆向分 ...