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

Fix 100% cpu usage when running in docker/non-tty terminal #1631

Merged
merged 4 commits into from
Nov 16, 2020
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
20 changes: 14 additions & 6 deletions locust/input_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
import tty


class InitError(Exception):
pass


class UnixKeyPoller:
def __init__(self):
try:
if sys.stdin.isatty():
self.stdin = sys.stdin.fileno()
self.tattr = termios.tcgetattr(self.stdin)
tty.setcbreak(self.stdin, termios.TCSANOW)
except termios.error:
pass
else:
raise InitError("Terminal was not a tty. Keyboard input disabled")

def cleanup(self):
termios.tcsetattr(self.stdin, termios.TCSANOW, self.tattr)
Expand Down Expand Up @@ -80,13 +84,17 @@ def get_poller():


def input_listener(key_to_func_map):
poller = get_poller()

def input_listener_func():
try:
poller = get_poller()
except InitError as e:
logging.info(e)
return

try:
while True:
input = poller.poll()
if input is not None:
if input:
for key in key_to_func_map:
if input == key:
key_to_func_map[key]()
Expand Down
33 changes: 26 additions & 7 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import platform
import pty
import signal
import subprocess
import textwrap
Expand Down Expand Up @@ -297,7 +298,19 @@ def test_web_options(self):
proc.terminate()

def test_input(self):
with mock_locustfile() as mocked:
LOCUSTFILE_CONTENT = textwrap.dedent("""
from locust import User, TaskSet, task, between

class UserSubclass(User):
wait_time = between(0.2, 0.8)
@task
def t(self):
print("Test task is running")
""")
with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked:
stdin_m, stdin_s = pty.openpty()
stdin = os.fdopen(stdin_m, "wb", 0)

proc = subprocess.Popen(
" ".join(
[
Expand All @@ -312,20 +325,26 @@ def test_input(self):
]
),
stderr=STDOUT,
stdin=PIPE,
stdin=stdin_s,
stdout=PIPE,
shell=True,
)

gevent.sleep(1)
proc.stdin.write(b"w")
proc.stdin.write(b"W")
proc.stdin.write(b"s")
proc.stdin.write(b"S")

stdin.write(b"w")
gevent.sleep(0.1)
stdin.write(b"W")
gevent.sleep(0.1)
stdin.write(b"s")
gevent.sleep(0.1)
stdin.write(b"S")

gevent.sleep(1)

output = proc.communicate()[0].decode("utf-8")
stdin.close()
self.assertIn("Spawning 1 users at the rate 100 users/s", output)
self.assertIn("Spawning 10 users at the rate 100 users/s", output)
self.assertIn("1 Users have been stopped", output)
self.assertIn("10 Users have been stopped", output)
self.assertIn("Test task is running", output)