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 infinite run time the default when running headless #1625

Merged
merged 1 commit into from
Nov 11, 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
2 changes: 1 addition & 1 deletion locust/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def setup_parser_arguments(parser):
parser.add_argument(
"-t",
"--run-time",
help="Stop after the specified amount of time, e.g. (300s, 20m, 3h, 1h30m, etc.). Only used together with --headless",
help="Stop after the specified amount of time, e.g. (300s, 20m, 3h, 1h30m, etc.). Only used together with --headless. Defaults to run forever.",
env_var="LOCUST_RUN_TIME",
)
parser.add_argument(
Expand Down
21 changes: 12 additions & 9 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,6 @@ def main():
logger.error("Valid --run-time formats are: 20, 20s, 3m, 2h, 1h20m, 3h30m10s, etc.")
sys.exit(1)

def spawn_run_time_limit_greenlet():
logger.info("Run time limit set to %s seconds" % options.run_time)

def timelimit_stop():
logger.info("Time limit reached. Stopping Locust.")
runner.quit()

gevent.spawn_later(options.run_time, timelimit_stop).link_exception(greenlet_exception_handler)

if options.csv_prefix:
stats_csv_writer = StatsCSVFileWriter(
environment, stats.PERCENTILES_TO_REPORT, options.csv_prefix, options.stats_history_enabled
Expand Down Expand Up @@ -331,8 +322,20 @@ def timelimit_stop():
else:
runner.start(options.num_users, options.spawn_rate)

def spawn_run_time_limit_greenlet():
def timelimit_stop():
logger.info("Time limit reached. Stopping Locust.")
runner.quit()

gevent.spawn_later(options.run_time, timelimit_stop).link_exception(greenlet_exception_handler)

if options.run_time:
logger.info("Run time limit set to %s seconds" % options.run_time)
spawn_run_time_limit_greenlet()
elif options.headless:
logger.info("No run time limit set, use CTRL+C to interrupt.")
else:
pass # dont log anything - not having a time limit is normal when not running headless

stats_printer_greenlet = None
if not options.only_summary and (options.print_stats or (options.headless and not options.worker)):
Expand Down
16 changes: 16 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ def test_default_headless_spawn_options(self):
)
self.assertIn("Spawning 1 users at the rate 1 users/s", output)

def test_headless_spawn_options_wo_run_time(self):
with mock_locustfile() as mocked:
proc = subprocess.Popen(
["locust", "-f", mocked.file_path, "--host", "https://test.com/", "--headless"],
stdout=PIPE,
stderr=PIPE,
)
gevent.sleep(1)
proc.send_signal(signal.SIGTERM)
stdout, stderr = proc.communicate()
self.assertEqual(0, proc.returncode)
stderr = stderr.decode("utf-8")
self.assertIn("Starting Locust", stderr)
self.assertIn("No run time limit set, use CTRL+C to interrupt", stderr)
self.assertIn("Shutting down (exit code 0), bye", stderr)

def test_default_headless_spawn_options_with_shape(self):
content = (
MOCK_LOUCSTFILE_CONTENT
Expand Down