locust任务等待有三种方式,分别是constant
、between
、constant_pacing
.
他们的区别是:
constant(2) # 任务执行完毕等待2秒开始下一任务
between(1,7) # 任务执行完毕等待1-7秒(中间随机取值)开始下一任务
constant_pacing(2) # # 设置任务启动总得等待时间,若任务耗时超过该时间,则任务结束后立即执行下一任务;若任务耗时不超过该时间,则等待达到该时间后执行下一任务。
下面我们分别来验证:
# locust_file.py
import time
from locust import User, task, constant, constant_pacing, between
class U(User):
wait_time = constant(2) # 任务执行完毕等待2秒开始下一任务
# wait_time = between(1,7) # 任务执行完毕等待1-7秒(中间随机取值)开始下一任务
# wait_time = constant_pacing(1) # 任务执行完毕,立即执行下一任务
@task
def fun1(self):
print('%s fun1 start' % time.strftime('%H:%M:%S'))
time.sleep(4)
print('%s fun1 end' % time.strftime('%H:%M:%S'))
@task
def fun2(self):
print('%s fun2 start' % time.strftime('%H:%M:%S'))
time.sleep(5)
print('%s fun2 end' % time.strftime('%H:%M:%S'))
执行压测 locust -f locust_file.py 设置1路并发,等待设置为constant(2)
查看结果如下:
F:\01pro\st_locust>locust -f locust_file.py
[2021-03-15 21:58:02,755] DESKTOP-OSUQOMS/INFO/locust.main: Starting web interface at http://127.0.0.1:8088
[2021-03-15 21:58:02,786] DESKTOP-OSUQOMS/INFO/locust.main: Starting Locust 1.4.3
[2021-03-15 21:58:15,642] DESKTOP-OSUQOMS/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-03-15 21:58:15,642] DESKTOP-OSUQOMS/INFO/locust.runners: All users spawned: U: 1 (1 total running)
21:58:15 fun2 start
21:58:20 fun2 end # 任务结束等待2秒开始下一任务
21:58:22 fun1 start
21:58:26 fun1 end # 任务结束等待2秒开始下一任务
21:58:28 fun1 start
21:58:32 fun1 end # 任务结束等待2秒开始下一任务
21:58:34 fun2 start
21:58:39 fun2 end
[2021-03-15 21:58:41,791] DESKTOP-OSUQOMS/INFO/locust.runners: Stopping 1 users
[2021-03-15 21:58:41,799] DESKTOP-OSUQOMS/INFO/locust.runners: 1 Users have been stopped, 0 still running
可以看到新任务均在上一任务结束2秒后才开始,下面等待设置为between(1, 7)
查看结果:
[2021-03-15 22:17:27,943] DESKTOP-OSUQOMS/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-03-15 22:17:27,945] DESKTOP-OSUQOMS/INFO/locust.runners: All users spawned: U: 1 (1 total running)
22:17:27 fun1 start
22:17:31 fun1 end # 任务结束等待5秒开始下一任务
22:17:36 fun1 start
22:17:40 fun1 end # 任务结束等待5秒开始下一任务
22:17:45 fun1 start
22:17:49 fun1 end # 任务结束等待4秒开始下一任务
22:17:53 fun1 start
22:17:57 fun1 end # 任务结束等待3秒开始下一任务
22:18:00 fun2 start
22:18:05 fun2 end # 任务结束等待3秒开始下一任务
22:18:08 fun1 start
22:18:12 fun1 end # 任务结束等待4秒开始下一任务
22:18:14 fun2 start
22:18:19 fun2 end
[2021-03-15 22:18:21,283] DESKTOP-OSUQOMS/INFO/locust.runners: Stopping 1 users
[2021-03-15 22:18:21,285] DESKTOP-OSUQOMS/INFO/locust.runners: 1 Users have been stopped, 0 still running
可以看到任务结束到新任务开始停顿了1-7秒*(上面的例子是3-5秒)*,下面等待设置为constant_pacing(3)
:
# locust_file.py
import time
from datetime import datetime
from locust import User, task, constant, constant_pacing, between
class U(User):
wait_time = constant_pacing(2) # 设置任务启动总得等待时间,若任务耗时超过该时间,则任务结束后立即执行下一任务;若任务耗时不超过该时间,则等待达到该时间后执行下一任务。
@task
def fun1(self):
print('%s fun1 start' % datetime.now())
time.sleep(1)
print('%s fun1 end' % datetime.now())
@task
def fun2(self):
print('%s fun2 start' % datetime.now())
time.sleep(3)
print('%s fun2 end' % datetime.now())
查看结果:
[2021-03-16 08:23:44,068] DESKTOP-OSUQOMS/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-03-16 08:23:44,068] DESKTOP-OSUQOMS/INFO/locust.runners: All users spawned: U: 1 (1 total running)
2021-03-16 08:23:44.069707 fun2 start
2021-03-16 08:23:47.077988 fun2 end # 第一个任务执行时初始化一个等待时间2秒
2021-03-16 08:23:49.089835 fun2 start
2021-03-16 08:23:52.090200 fun2 end # 任务耗时3秒,超过了设置的等待时间,则下一任务开始不再等待
2021-03-16 08:23:52.091109 fun2 start
2021-03-16 08:23:55.092400 fun2 end # 同上
2021-03-16 08:23:55.093421 fun2 start
2021-03-16 08:23:58.094757 fun2 end # 同上
2021-03-16 08:23:58.095666 fun1 start
2021-03-16 08:23:59.099090 fun1 end # 任务耗时1秒,等待时间为2秒,所以需要额外等待1秒
2021-03-16 08:24:00.103593 fun2 start
2021-03-16 08:24:03.105886 fun2 end # 任务耗时3秒,超过了设置的等待时间,则下一任务开始不再等待
2021-03-16 08:24:03.106793 fun1 start
2021-03-16 08:24:04.114227 fun1 end # 任务耗时1秒,等待时间为2秒,所以需要额外等待1秒
2021-03-16 08:24:05.116731 fun2 start
2021-03-16 08:24:08.124968 fun2 end # 任务耗时3秒,超过了设置的等待时间,则下一任务开始不再等待
2021-03-16 08:24:08.125927 fun1 start
[2021-03-16 08:24:08,275] DESKTOP-OSUQOMS/INFO/locust.runners: Stopping 1 users
[2021-03-16 08:24:08,276] DESKTOP-OSUQOMS/INFO/locust.runners: 1 Users have been stopped, 0 still running
下面附上等待的源码:
# D:\Programs\Python\Python38-32\Lib\site-packages\locust\user\wait_time.py
import random
from time import time
def between(min_wait, max_wait):
"""
Returns a function that will return a random number between min_wait and max_wait.
Example::
class MyUser(User):
# wait between 3.0 and 10.5 seconds after each task
wait_time = between(3.0, 10.5)
"""
return lambda instance: min_wait + random.random() * (max_wait - min_wait)
def constant(wait_time):
"""
Returns a function that just returns the number specified by the wait_time argument
Example::
class MyUser(User):
wait_time = constant(3)
"""
return lambda instance: wait_time
def constant_pacing(wait_time):
"""
Returns a function that will track the run time of the tasks, and for each time it's
called it will return a wait time that will try to make the total time between task
execution equal to the time specified by the wait_time argument.
In the following example the task will always be executed once every second, no matter
the task execution time::
class MyUser(User):
wait_time = constant_pacing(1)
@task
def my_task(self):
time.sleep(random.random())
If a task execution exceeds the specified wait_time, the wait will be 0 before starting
the next task.
"""
def wait_time_func(self):
if not hasattr(self, "_cp_last_run"):
self._cp_last_wait_time = wait_time
self._cp_last_run = time()
return wait_time
else:
run_time = time() - self._cp_last_run - self._cp_last_wait_time
self._cp_last_wait_time = max(0, wait_time - run_time)
self._cp_last_run = time()
return self._cp_last_wait_time
return wait_time_func