From 55b274afc478f3f9a184dfa3caff95870774c32d Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 19 Dec 2024 15:18:13 +0100 Subject: [PATCH 1/2] Make cpu usage check less frequent, and have it sleep BEFORE the first check. --- locust/runners.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/runners.py b/locust/runners.py index 297a1c89dc..efcb5a3a96 100644 --- a/locust/runners.py +++ b/locust/runners.py @@ -52,7 +52,7 @@ ] WORKER_REPORT_INTERVAL = 3.0 WORKER_LOG_REPORT_INTERVAL = 10 -CPU_MONITOR_INTERVAL = 5.0 +CPU_MONITOR_INTERVAL = 10.0 CPU_WARNING_THRESHOLD = 90 HEARTBEAT_INTERVAL = 1 HEARTBEAT_LIVENESS = 3 @@ -283,6 +283,7 @@ def stop_users(self, user_classes_stop_count: dict[str, int]) -> None: def monitor_cpu_and_memory(self) -> NoReturn: process = psutil.Process() while True: + gevent.sleep(CPU_MONITOR_INTERVAL) self.current_cpu_usage = process.cpu_percent() self.current_memory_usage = process.memory_info().rss if self.current_cpu_usage > CPU_WARNING_THRESHOLD: @@ -296,7 +297,6 @@ def monitor_cpu_and_memory(self) -> NoReturn: self.environment.events.usage_monitor.fire( environment=self.environment, cpu_usage=self.current_cpu_usage, memory_usage=self.current_memory_usage ) - gevent.sleep(CPU_MONITOR_INTERVAL) @abstractmethod def start( From b5ac53d706ba4ff4edbf6ded4ea58d960ed55ecb Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Sat, 21 Dec 2024 22:14:51 +0100 Subject: [PATCH 2/2] Tweak cpu warning test for reliability with new solution. --- locust/test/test_runners.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/locust/test/test_runners.py b/locust/test/test_runners.py index d1e7a5a6b0..b9770d572c 100644 --- a/locust/test/test_runners.py +++ b/locust/test/test_runners.py @@ -161,16 +161,16 @@ def t(self): def test_cpu_warning(self): _monitor_interval = runners.CPU_MONITOR_INTERVAL - runners.CPU_MONITOR_INTERVAL = 2.0 + runners.CPU_MONITOR_INTERVAL = 0.1 try: class CpuUser(User): - wait_time = constant(0.001) - @task def cpu_task(self): - for i in range(1000000): - _ = 3 / 2 + for i in range(10): + for j in range(1000000): + _ = 3 / 2 + time.sleep(0.0001) # let other greenlets run, like the cpu monitor environment = Environment(user_classes=[CpuUser]) environment._cpu_warning_event_triggered = False @@ -181,6 +181,7 @@ def cpu_warning(environment, cpu_usage, **kwargs): environment.events.cpu_warning.add_listener(cpu_warning) runner = LocalRunner(environment) + time.sleep(0.2) # let first checks run self.assertFalse(runner.cpu_warning_emitted) runner.spawn_users({CpuUser.__name__: 1}, wait=False) sleep(2.5)