def test_stop_timeout_with_interrupt(self): short_time = 0.05 class MySubTaskSet(TaskSet): @task def a_task(self): gevent.sleep(0) self.interrupt(reschedule=True) class MyTaskSet(TaskSet): tasks = [MySubTaskSet] class MyTestLocust(Locust): task_set = MyTaskSet options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(0) timeout = gevent.Timeout(short_time) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail("Got Timeout exception. Interrupted locusts should exit immediately during stop_timeout.") finally: timeout.cancel()
def test_stop_timeout_during_on_start(self): short_time = 0.05 class MyTaskSet(TaskSet): finished_on_start = False my_task_run = False def on_start(self): gevent.sleep(short_time) MyTaskSet.finished_on_start = True @task def my_task(self): MyTaskSet.my_task_run = True class MyTestLocust(Locust): task_set = MyTaskSet min_wait = 0 max_wait = 0 options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertTrue(MyTaskSet.finished_on_start) self.assertFalse(MyTaskSet.my_task_run)
def test_stop_timeout_exit_during_wait(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): task_set = MyTaskSet wait_time = between(1, 1) options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) # sleep to make sure locust has had time to start waiting timeout = gevent.Timeout(short_time) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail("Got Timeout exception. Waiting locusts should stop immediately, even when using stop_timeout.") finally: timeout.cancel()
def test_no_reset_stats(self): class User(Locust): wait_time = constant(0) @task class task_set(TaskSet): @task def my_task(self): self.locust.environment.events.request_success.fire( request_type="GET", name="/test", response_time=666, response_length=1337, ) sleep(2) environment = Environment(reset_stats=False, options=mocked_options()) runner = LocalLocustRunner(environment, locust_classes=[User]) runner.start(locust_count=6, hatch_rate=12, wait=False) sleep(0.25) self.assertGreaterEqual( runner.stats.get("/test", "GET").num_requests, 3) sleep(0.3) self.assertEqual(6, runner.stats.get("/test", "GET").num_requests) runner.quit()
def test_stop_timeout_during_on_start(self): short_time = 0.05 class MyTaskSet(TaskSet): finished_on_start = False my_task_run = False def on_start(self): gevent.sleep(short_time) MyTaskSet.finished_on_start = True @task def my_task(self): MyTaskSet.my_task_run = True class MyTestLocust(Locust): tasks = [MyTaskSet] wait_time = constant(0) environment = create_environment(mocked_options()) environment.stop_timeout = short_time runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertTrue(MyTaskSet.finished_on_start) self.assertFalse(MyTaskSet.my_task_run)
def test_stop_timeout_with_interrupt_no_reschedule(self): state = [0] class MySubTaskSet(TaskSet): @task def a_task(self): gevent.sleep(0.1) state[0] = 1 self.interrupt(reschedule=False) class MyTestLocust(Locust): tasks = [MySubTaskSet] wait_time = constant(3) environment = create_environment(mocked_options()) environment.stop_timeout = 0.3 runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(1, 1, wait=True) gevent.sleep(0) timeout = gevent.Timeout(0.11) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail( "Got Timeout exception. Interrupted locusts should exit immediately during stop_timeout." ) finally: timeout.cancel() self.assertEqual(1, state[0])
def test_change_user_count_during_hatching(self): class User(Locust): wait_time = constant(1) @task def my_task(self): pass environment = Environment(options=mocked_options()) runner = LocalLocustRunner(environment, [User]) runner.start(locust_count=10, hatch_rate=5, wait=False) sleep(0.6) runner.start(locust_count=5, hatch_rate=5, wait=False) runner.hatching_greenlet.join() self.assertEqual(5, len(runner.locusts)) runner.quit()
def test_change_user_count_during_hatching(self): class User(Locust): wait_time = constant(1) class task_set(TaskSet): @task def my_task(self): pass runner = LocalLocustRunner([User], mocked_options()) runner.start_hatching(locust_count=10, hatch_rate=5, wait=False) sleep(0.6) runner.start_hatching(locust_count=5, hatch_rate=5, wait=False) runner.hatching_greenlet.join() self.assertEqual(5, len(runner.locusts)) runner.quit()
def test_kill_locusts_with_stop_timeout(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): MyTaskSet.state = "first" gevent.sleep(short_time) MyTaskSet.state = "second" # should only run when run time + stop_timeout is > short_time gevent.sleep(short_time) MyTaskSet.state = "third" # should only run when run time + stop_timeout is > short_time * 2 class MyTestLocust(Locust): tasks = [MyTaskSet] wait_time = constant(0) environment = create_environment(mocked_options()) runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(1, 1) gevent.sleep(short_time / 2) runner.kill_locusts(1) self.assertEqual("first", MyTaskSet.state) runner.quit() environment.stop_timeout = short_time / 2 # exit with timeout runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(1, 1) gevent.sleep(short_time) runner.kill_locusts(1) self.assertEqual("second", MyTaskSet.state) runner.quit() environment.stop_timeout = short_time * 3 # allow task iteration to complete, with some margin runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(1, 1) gevent.sleep(short_time) timeout = gevent.Timeout(short_time * 2) timeout.start() try: runner.kill_locusts(1) runner.locusts.join() except gevent.Timeout: self.fail( "Got Timeout exception. Some locusts must have kept runnining after iteration finish" ) finally: timeout.cancel() self.assertEqual("third", MyTaskSet.state)
def test_stop_event_quit(self): class User(Locust): wait_time = constant(1) @task def my_task(self): pass test_stop_run = [0] environment = Environment(options=mocked_options()) def on_test_stop(*args, **kwargs): test_stop_run[0] += 1 environment.events.test_stop.add_listener(on_test_stop) runner = LocalLocustRunner(environment, locust_classes=[User]) runner.start(locust_count=3, hatch_rate=3, wait=False) self.assertEqual(0, test_stop_run[0]) runner.quit() self.assertEqual(1, test_stop_run[0])
def test_cpu_warning(self): _monitor_interval = runners.CPU_MONITOR_INTERVAL runners.CPU_MONITOR_INTERVAL = 2.0 try: class CpuLocust(Locust): wait_time = constant(0) class task_set(TaskSet): @task def cpu_task(self): for i in range(1000000): _ = 3 / 2 runner = LocalLocustRunner([CpuLocust], mocked_options()) self.assertFalse(runner.cpu_warning_emitted) runner.spawn_locusts(1, wait=False) sleep(2.5) runner.quit() self.assertTrue(runner.cpu_warning_emitted) finally: runners.CPU_MONITOR_INTERVAL = _monitor_interval
def test_cpu_warning(self): _monitor_interval = runners.CPU_MONITOR_INTERVAL runners.CPU_MONITOR_INTERVAL = 2.0 try: class CpuLocust(Locust): wait_time = constant(0.001) @task def cpu_task(self): for i in range(1000000): _ = 3 / 2 environment = Environment( options=mocked_options(), ) runner = LocalLocustRunner(environment, [CpuLocust]) self.assertFalse(runner.cpu_warning_emitted) runner.spawn_locusts(1, 1, wait=False) sleep(2.5) runner.quit() self.assertTrue(runner.cpu_warning_emitted) finally: runners.CPU_MONITOR_INTERVAL = _monitor_interval
def test_stop_timeout(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): MyTaskSet.state = "first" gevent.sleep(short_time) MyTaskSet.state = "second" # should only run when run time + stop_timeout is > short_time gevent.sleep(short_time) MyTaskSet.state = "third" # should only run when run time + stop_timeout is > short_time * 2 class MyTestLocust(Locust): task_set = MyTaskSet options = mocked_options() runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertEqual("first", MyTaskSet.state) options.stop_timeout = short_time / 2 # exit with timeout runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.quit() self.assertEqual("second", MyTaskSet.state) options.stop_timeout = short_time * 2 # allow task iteration to complete, with some margin runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.quit() self.assertEqual("third", MyTaskSet.state)
def test_stop_timeout(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): MyTaskSet.state = "first" gevent.sleep(short_time) MyTaskSet.state = "second" # should only run when run time + stop_timeout is > short_time gevent.sleep(short_time) MyTaskSet.state = "third" # should only run when run time + stop_timeout is > short_time * 2 class MyTestLocust(Locust): task_set = MyTaskSet wait_time = constant(0) options = mocked_options() runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertEqual("first", MyTaskSet.state) options.stop_timeout = short_time / 2 # exit with timeout runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.quit() self.assertEqual("second", MyTaskSet.state) options.stop_timeout = short_time * 3 # allow task iteration to complete, with some margin runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) timeout = gevent.Timeout(short_time * 2) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail( "Got Timeout exception. Some locusts must have kept runnining after iteration finish" ) finally: timeout.cancel() self.assertEqual("third", MyTaskSet.state)