Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cpu usage check sleep BEFORE the first check, and make it slightly less frequent #3014

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down
11 changes: 6 additions & 5 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Loading