深聊性能测试,从入门到放弃之:Locust性能自动化(五)API汇总整理(上)

1、引言

Locust的API共有 15类,如下:

① User class
② HttpUser class
③ TaskSet class
④ task decorator
⑤ tag decorator
⑥ Sequential TaskSet class
⑦ Built in wait_time functions
⑧ HttpSession class
⑨ Response class
⑩ ResponseContextManager class
⑪ Exceptions
⑫ Environment class
⑬ Event hooks
⑭ Runner classes
⑮ Web UI class

关于Locust 的API 内容,小鱼会分两篇来分享。

①~⑦:《深聊性能测试,从入门到放弃之:Locust性能自动化(五)API汇总整理(上)

⑧~⑮:《深聊性能测试,从入门到放弃之:Locust性能自动化(五)API汇总整理(下)

之所以分两篇来写,是因为,小鱼写的时候,就觉得很多,何况各位大佬看文章呢。

如果之前没有接触过这些 API类,那一次看这么多,估计真的 还没进门,就放弃了…

这不是小鱼想要的,所以,小鱼就分两篇来写,怎么也得迈进门口看看~ ~

后来,想不想出去,就看持久力了…

言不跑偏,不是小鱼!

但是,也得回来,先进门瞧瞧 API们吧 !!!

2、User class

2.1 定义


Class User (environment)

引用官网原话:

1、
Represents a “user” which is to be spawned and attack the system that is to be load tested.
2、
The behaviour of this user is defined by its tasks.Tasks can be declared either directly on the class by using the @task decorator on methods, or by setting the tasks attribute.
3、
This class should usually be subclassed by a class that defines some kind of client.
For example when load testing an HTTP system, you probably want to use the HttpUser class.

如果看的不太明白,没关系,
小鱼直接直接用大白话来翻译:

1、使用User进行负载测试。
2、该用户的行为,可以自己定义;可以使用 @task装饰器或者设置 task属性直接在类上声明任务。
3、此类大部分情况都是由定义某种客户端的类继承
例如:在对HTTP系统进行负载测试时, 就会想到使用HttpUser

这样简单一说,是不是就很一目了然了~ ~

2.2 类及用法

abstract= True

---->>若为True,则该类则被子类化,并且Locust不会在测试期间产生此类用户。

on_start()

---->>表示开始运行

on_stop()

---->>表示停止运行

tasks: List[Union[locust.user.task.TaskSet, Callable]]= []

---->> 表示将要运行TaskSet类的集合
如果任务是列表,则随机选择要执行的任务;
如果任务是两个元组(可调用,整数)列表,或者是字典,则随机选择要执行的任务,但是会根据其相应的值来对每个任务进行加权。

举个例子

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class ForumPage(TaskSet):
	#设置权数值
    tasks = {ThreadPage:20, write_post:1}
    

我们可以看到,被选中的ThreadPage 的可能是 write_post20倍。

wait()

---->> 设置等待, 在函数 User.wait_time中定义

---->> stop_timeout:禁止 任务中休眠;
---->> gevent.sleep():休眠

wait_time= None

---->> 设置等待时间间隔,单位是 秒,可以对单个TaskSet 设置;

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

from locust import user,between

class TsetUser(User):
	#设置等待时间间隔为2~15秒
	wait_time =  bwtween(2,15)

weight= 10

---->> 选择用户的权重, 数值越高,被选中的机会就越大。

3、HttpUser class

3.1 定义


classHttpUser(*args, **kwargs)

同样,先上官网的原话:

1、
Represents an HTTP “user” which is to be spawned and attack the system that is to be load tested.
2、
The behaviour of this user is defined by its tasks. Tasks can be declared either directly on the class by using the @task decorator on methods, or by setting the tasks attribute.
3、
This class creates a client attribute on instantiation which is an HTTP client with support for keeping a user session between requests.

小鱼言简意赅的 总结一下:

1、要进行负载测试的 HTTP 的user
2、该用户的行为尤其任务定义。可以使用 @task装饰器或者设置 task属性直接在类上声明任务。
3、此类实例化时创建一个客户端属性,该属性是一个HTTP客户端,支持在请求之间保持用户会话。

3.2 类及用法

abstract= True

---->>若为True,则该类则被子类化,并且用户在测试期间,不会选择locust。

client: locust.clients.HttpSession= None

---->>在Locust实例化后创建Http Session实例。
---->>并且这个客户端支持 cookie,可以保持HTTP请求之间的会话,直到结束,或者被强制停止。

4、TaskSet class

4.1 定义


Class TaskSet  (parent)

引用官网原话:

1、
Class defining a set of tasks that a User will execute.
2、
When a TaskSet starts running, it will pick a task from the tasks attribute, execute it, and then sleep for the number of seconds returned by its wait_time function. If no wait_time method has been declared on the TaskSet, it’ll call the wait_time function on the User by default. It will then schedule another task for execution and so on.
3、
TaskSets can be nested, which means that a TaskSet’s tasks attribute can contain another TaskSet. If the nested TaskSet is scheduled to be executed, it will be instantiated and called from the currently executing TaskSet. Execution in the currently running TaskSet will then be handed over to the nested TaskSet which will continue to run until it throws an InterruptTaskSet exception, which is done when TaskSet.interrupt() is called. (execution will then continue in the first TaskSet).

小鱼言简意赅的 总结一下:

1、定义用户将要执行的一组任务的类。
2、TaskSet开始运行时,它将从task属性中选择一个任务,来执行,然后执行wait_time()函数;如果没有在TaskSet上声明任何wait_time方法,则默认情况将调用User.wait_time()函数。
3、TaskSet可以嵌套,这意味着TaskSet的task属性可以包含另一个TaskSet。
如果计划执行TaskSet,将从当前正在执行的TaskSet实例化并调用。然后,当前正在运行的TaskSet中的执行将移交给嵌套的 TaskSet,它将继续运行,直到抛出 异常,该异常在调用askSet.interrupt()时完成。

嗯,小鱼的工作,就是脱下Locust神秘的…

4.2 类及用法

propertyclient

---->> 是TaskSet 的client的一个"快捷方式"

on_start()

---->>表示开始运行TaskSet

on_stop()

---->>表示停止运行TaskSet

interrupt(reschedule=True)

---->>默认为True,父级用户将立即重新安排并执行任务。
中断TaskSet并将任务移交给父TaskSet。

wait()

---->> 设置等待, 在函数 Locust.wait_time(或者TaskSet.wait_time函数)中定义

---->> stop_timeout:禁止 任务中休眠;
---->> gevent.sleep():休眠

wait_time()

---->> 设置等待时间间隔,单位是 秒,可以对单个TaskSet 设置;

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

from locust import TaskSet,between

class Task(TaskSet):
	#设置等待时间间隔为2~15秒
	wait_time =  bwtween(2,15)

property parent

---->> 此TaskSet 的父TaskSet实例。

schedule_task(task_callable, first=False)

---->>将任务添加到用户的任务执行队列。

---->>task_callable:要计划的用户任务

---->>first:可选参数,为True,则放在任务队列的首位

tasks: List[Union[TaskSet, Callable]]= []

---->> 表示将要运行TaskSet类的集合
如果任务是列表,则随机选择要执行的任务;
如果任务是两个元组(可调用,整数)列表,或者是字典,则随机选择要执行的任务,但是会根据其相应的值来对每个任务进行加权。

举个例子

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class ForumPage(TaskSet):
	#设置权数值
    tasks = {ThreadPage:20, write_post:1}
    

User 的task 定义内容一样的。

property user

---->>创建TaskSet 实例

5、task decorator

5.1 类及用法

task(weight=1)
---->> 用作便利修饰器,以便能够为类中的内联用户或TaskSet声明任务。

举个例子

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class ForumPage(TaskSet):
	'''
	设置TaskSet 类
	定义两个函数,
	read_thread :task设置 100
	create_thread: task 设置7
	'''
	#设置权值
	@task(100)
    def read_thread(self):
    	pass
    
    @task(7)
    def create_thread(self):
    	pass
    

6、tag decorator

6.1 类及用法

tag( *tags)
---->>装饰器,用于使用给定的tag 名进行标记任务 和 TaskSet
可以将测试限定仅执行使用–tag命令行参数提供的任何标签标记的任务。

不明白的小伙伴,可以看一下这个栗子:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class ForumPage(TaskSet):
	#设置tag标签
	@tag('thread')
	#设置权重值
	@task(100)
    def read_thread(self):
    	pass
    
    @tag('thread')
    @tag('post')
    @task(7)
    def create_thread(self):
    	pass

    @tag('post')
    @task(22)
    def comment(self):
    	pass
    
    

7、Sequential TaskSet class

7.1 定义


class SequentialTaskSet(*args, **kwargs)

引用官网原话:

1、
Class defining a sequence of tasks that a User will execute.
2、
Works like TaskSet, but task weight is ignored, and all tasks are executed in order. Tasks can either be specified by setting the tasks attribute to a list of tasks, or by declaring tasks as methods using the @task decorator. The order of declaration decides the order of execution.
3、
It’s possible to combine a task list in the tasks attribute, with some tasks declared using the @task decorator. The order of declaration is respected also in that case.

同样,小鱼来个中文翻译版的:

1、定义用户将要执行的任务序列的类。
2、像TaskSet一样工作,但是忽略任务权重按顺序执行所有任务。可以通过将task属性设置为任务列表来指定任务,也可以使用@task装饰器将任务声明为方法。声明的顺序决定执行的顺序
3、可以将task属性中的任务列表与使用@task装饰器声明的某些任务合在一起。同时需要遵循声明的顺序

7.2 类及用法

propertyclient

---->> 是TaskSet 的client的一个"快捷方式"

on_start()

---->>表示开始运行TaskSet

on_stop()

---->>表示停止运行TaskSet

interrupt(reschedule=True)

---->>默认为True,父级用户将立即重新安排并执行任务。
中断TaskSet并将任务移交给父TaskSet。

property user

---->>创建TaskSet 实例

wait_time()

---->> 设置等待时间间隔,单位是 秒,可以对单个TaskSet 设置;

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

from locust import TaskSet,between

class Task(TaskSet):
	#设置等待时间间隔为2~15秒
	wait_time =  bwtween(2,15)

property parent

---->> 此TaskSet 的父TaskSet实例。

schedule_task(task_callable, first=False)

---->>将任务添加到用户的任务执行队列。

---->>task_callable:要计划的用户任务

---->>first:可选参数,为True,则放在任务队列的首位

8、Built in wait_time functions

8.1 类及用法

between(min_wait, max_wait)
---->> 返回一个函数,在min_wait 与 max_wait之间返回一个随机数。

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class TestUser(User):
	#每个任务等待3秒~15.9秒
	wait_time = between(3.0,15.9)

constant(wait_time)

---->>返回一个仅返回有wait_time参数指定的数字的函数

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class TestUser(User):
	wait_time = constant(5)

constant_pacing(wait_time)

---->>返回一个函数,该函数将跟踪任务的运行时间,并且每次调用该函数时,它将返回一个等待时间,该等待时间将使任务执行之间的总时间等于wait_time参数指定的时间。

例如:

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-10-13
"""

class MyUser(User):
    wait_time = constant_pacing(1)
    @task
    def my_task(self):
        time.sleep(random.random())

解析:
1、无论任务执行时间如何,任务总是每秒执行一次;

2、如果任务执行超过指定的wait_time,则在开始下一个任务之前等待将为 0

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Carl_奕然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值