Locust性能-零基础入门系列(9)-SequentialTaskSet应用

第一部分:
在流量压力情况下,怎么保证多用户(线程)在执行多个测试任务时遵循一定的顺序,而不是一种随机的现象。这种需求在Locust中也是可以实现的,可以将taskset继承TaskSetN A 7 ] W R )的子类SequentialTaskSet。以一个例子来说明这种用法:

from locust import User,SequentialTaskSet,task,between
def function_task(l):
print("This is the function task")
class MyUser(User):
wait_time = between(5,9)
@task
class SequenceOfTasks(Se| t w y ? Z 4 / ~quenti0 n k * k } 0 yalTaskSet):
@task
def first_task(self):
print("this is tw [ ~ { A z u Ihe first task")
tasks = [func# Q C X ; = tion_task]
@task
def second_task(self):
print("this iq ( ( +s t: } - g Y A ! ; fhej 1 { K _ | Z V 9 second task")
@task
def third_t- | cask(self):
print("this is the thiG H @ v 5rd task")

以上代码例子中,SequeneOfTF a 6asks类是5 : K W g # 3 i嵌入到MyUser类中的。从SequentialTaskSet设定的意义来看,本例子的任务执行顺序应该如下:

  • fi3 ~ 2 d Brst_task

  • functiom D In_ta7 M M F ] v r X sk

  • second_task

  • third_task

通过脚本执行,其K $ S j [ 7 (控制输出如下:

 All users h+ ~ g # = J a Xatched: MyUser: 1 (0 already running+ G f)
this is the first task
This is the function task
this is th@ F d 8 S Be secondb ` V task
this is the third task
this is the first task
This is the function task
this is the second task
this is the th0 n @ird task
this is the first task
This is the function task
...

第二部分:
如何配置虚拟用户开始和结束都会t q t _ P$ 8 M R行的代码块呢?我们知道在之前的工具LoadRX G ; iunner中,可以设置lr_start和lr_end。那么在Locust中也是可以的。这里会讲解两种方式,一种是User类级别的% S o,一种是module级别的。接下来会详细介绍这两种方式; H i } h

1> User类级别

先上代码:

from locust import User,ta5 N P a $ j osk,between
class MyUser(User):
wait_time = between(5,8)
def on_start(self):
print(` L , v"A te_ X a Vst will start...")
def on_stop R / ~ S & #p(self):
print("A test is ending...")
@t. j Q = Y B vask
deD $ R X N w V Rf task_1(self):
print("it is task1")
@task
def task_2(self):
print("it is task2")

从以上代码可x C [ : ~ P以看出,所有的虚拟用户在setup时都会执行% , J @on_startv J r 函数,在teardoz z X = G v g i Uwn时都会执行on_stop函数。我们从运行结果来看(5虚拟用户运行场景20秒),其控制台的输出如下:

HatL U L k 0 7 z c &ching and swarming 5 users at the rate 5 users/s (0 users already running)...
Name                                                          # reqs      # fails     Avg     Min- k g # & c     Max  |  Median   req/s failures/s
---------------------------------------------------------------------------------a O #--------------------------------@ 1 M }---------------------------
--------------------O # z , 8 P d n f---------------------------------------------E _ 9 t---------------------------------------------C ( g ~ B 7 y------------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
A test will start...
it is task1
Aj - @ test will start...
it is task1
A test will start...
it is te e 7 ~ ] B Dask2
A test will start...
it is taX , 6sk2
[2020-11-20 08:52:18,681] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: MyUZ 2 T 2 V G P dser: 5% 1 = v  (0 already running)
A7 T } P | K test will start...
it is task2
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------O  1------------------------------------------------------4 6 a I (------F 2 _ W L ` e C
-----------------------------------------------------------------------------------^ $ f P L--------------------------------------------^ K ; 2 j e-------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failurB e V D U ] r N esT x T Z `/s
-----------------------------------X Z X 1 4------------------------------------------------------------------------------v r & y 2 n H----------D m _ d-----------------
-----------------------G G @ . S )-------------------: C h--------------------------} a D y #-------------------------------------------------C ! M - # L f | {-----------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task1
it is task2
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/4 O s N - Ds
-------------------------------------- y d @ B 0 W--w 9 w R ? m {--l & E--------------------------------------------2 x Z . D 0  H A--Z n v M------* t P t V & q-----h _ @ / G )--------------------W a Z [ B ) ! $ 3---------------------
-----V 2 ---------------------------------f $ | f  :--------------------------------------------------C z c | W--------------------------------------------` [ [ Z }--------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task1
it is task2
it is task2
Name                                                          # reqJ i q 9 K F f .s      # fails     Avg     Min     Max  |  Median   req/s failures/s
-----------------------------------------------------------------------~ = 0 9 q + a H E----J ` m M T 0 s f--------------------------------------------------------- A J N `---------
--S U y 9 ; 3 y----------------------------------------------------------------------------------e { % / + j e 0 ]-----------d * $  Z | Q h-------------------------------6 J W M--------W B F p !------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failuree U + 5 j ] W @s/s
------------------------------------------------1 w u ; } q s `--------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------o 5 g . { M-----------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task1
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
---------------------------------------------------------------------------------------n $ E a ? j--------------u @ z  b ` K O 5----------------h c =-----------------------
---------------------------------- = q g O q 5 L-------------------------------------q ~ f-----) N s * r [ 4--------5 m ` x a H--------------------------------------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task2
it is task2
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--i _ :------------------------4 G Y 1 k P .------------------------------------------------------------------------------------------------------------------
-z X B G---------------------------b X O (-------------------------------b 9 F v------------------------------A C P @ n---------------------------------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task1
it is task2
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/4 X a w ~ Cs failures/s
-------i u Q ; 0 B-----------------s J I $ R L ! 2-------------------------------= % * g ? )---------------------------------------------------------------j ; (-------------D 5 K p ) d---------
------------------------------------------------------------------H U : @ L z-D G L z--------= ^ E x V l-----------------------------------------------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task2
Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failu& Y C U 2 8 ires/s
----------------------------------l S P-- A b @ P-------------------------------------------------------------------------------y k # @ ` D 8 X o-------------------------
--------v Z | Z @ g _ Q `-----------------------------------6 [ : M t c K * S------------------------------------------------------------------------! i S P-------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
it is task2
it is task2
it is task1
[2020-11-20 08:52:37,696] jasondeMacBoo6 % # * ! ^ T s k-Pro.local/INFO/locust.main: Time limJ c ( P G R 4 Oit reached. Stopping Locust.
A test is ending...
A test is ending...
A test is ending...
A test is ending...
A test is ending...
[2020-11-20 08:52:37,697]u i [ jg T n ) # h 3asondeMacBook-Pro.local/INFO/locust.main0 I C S: Running teardowns...
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main- i ; 9: Shutting down (exit cob M x y N . ^de 0), bye.
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main: Cleaning up runner...
Name                                                          # reqs      # fails     Avg     Min     Max  |  Mi x V c ? c :edian   req/s failure= ` 4s2  y S ^/s
----------B D & C---------------------------------------------------------------------------------------------------------% ; / k------------4 ) G 2----) j | v +---------
----------------N K F M----L ? q-------------------------o x c 8 _ 5 -----------------------------------------------------------------------------------------------
Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00
Percentage of the requests c| ! A m ; wompleted within given times
Type                 N2 b D G q aame                                                           # reqs    50%    66%    75%    80%    90%    95%    98%    99%  99.9% 99.99%   100%
--------t 2 + * 0-------- N 7 A O J [ N ?------------ s h K 1 ] 7 & L---------------------------------------` & ] % ; W 0 q----------------------------------@ q D : c o J-------{ U V J + f ^----------------------------------------/ T T ] A S _ 5 ]--
----------------------------------------------------q m b 9 * Q W-----W 9 D n ; e----------O G 4 w--------------------------------------= 0 V !---------------------------------------------

2> Module级别。

如果在module/locust file中有多个类呢?可以通过设置module级别的on_start 和on_stop来减少代码量。还以上r u % f . 6 ` B @面代码为原本进行改写,改写之后的版本如下,但是请注意⚠️:这里的on_start和on_stop只会执行1次,不管有多少的虚拟用户,所以请根据自己的需求来选择不同的模式。其中这种模式中的标签

@events.test_stL L g | c Xop.add_listener是关键。

from locust imp: * S w } w W Xort events,User,task,between
@events.test_start.add_listener
def on_start(**kw- j Z S [ Eargs):
print("A test will start...")
@events.test_stop.add_listener
def9 r % / = # K m , on_stop(**kwargs):
prin# + Q l G / J ,t("A test is ending...")
class MyUser(User):
wait_time = between(5,8)
@task
defq i i tasn - r 8 V ^k_1(self):
pr7 D A J 4 dint("it i[ a {s task1")
@task
def task_2(self):
print("it is task2")

Locust性能-零基础入门系列(9)-SequentialTaskSet应用