From 0e10e595fbd069bd5e34143d4010d3c397fd74c6 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 19 Oct 2023 21:54:50 +0200 Subject: [PATCH] Fix incorrect first wait time for constant pacing. It incorrectly assumed the first iteration was instantaneous (because it had no way of knowing when the first iteration started) --- locust/user/users.py | 2 ++ locust/user/wait_time.py | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/locust/user/users.py b/locust/user/users.py index bf819c9a7c..c038ace0ab 100644 --- a/locust/user/users.py +++ b/locust/user/users.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Callable, Dict, List, Optional, final +import time from gevent import GreenletExit, greenlet from gevent.pool import Group from urllib3 import PoolManager @@ -119,6 +120,7 @@ def __init__(self, environment): self._greenlet: greenlet.Greenlet = None self._group: Group self._taskset_instance: TaskSet = None + self._cp_last_run = time.time() # used by constant_pacing wait_time def on_start(self): """ diff --git a/locust/user/wait_time.py b/locust/user/wait_time.py index fcb2d037bc..b86d08b720 100644 --- a/locust/user/wait_time.py +++ b/locust/user/wait_time.py @@ -47,15 +47,12 @@ def my_task(self): """ 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 + if not hasattr(self, "_cp_last_wait_time"): + self._cp_last_wait_time = 0 + 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