def test_user_count_in_csv_history_stats(self): start_time = int(time.time()) class TestUser(Locust): wait_time = constant(10) @task def t(self): self.environment.runner.stats.log_request("GET", "/", 10, 10) runner = LocalLocustRunner(self.environment, [TestUser]) runner.start(3, 5) # spawn a user every 0.2 second gevent.sleep(0.1) greenlet = gevent.spawn(stats_writer, self.environment, self.STATS_BASE_NAME, full_history=True) gevent.sleep(0.6) gevent.kill(greenlet) runner.stop() with open(self.STATS_HISTORY_FILENAME) as f: reader = csv.DictReader(f) rows = [r for r in reader] self.assertEqual(6, len(rows)) for i in range(3): row = rows.pop(0) self.assertEqual("%i" % (i + 1), row["User count"]) self.assertEqual("/", row["Name"]) self.assertEqual("%i" % (i + 1), row["# requests"]) self.assertGreaterEqual(int(row["Timestamp"]), start_time) row = rows.pop(0) self.assertEqual("%i" % (i + 1), row["User count"]) self.assertEqual("Aggregated", row["Name"]) self.assertEqual("%i" % (i + 1), row["# requests"]) self.assertGreaterEqual(int(row["Timestamp"]), start_time)
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_exit_during_wait(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): tasks = [MyTaskSet] wait_time = between(1, 1) options = mocked_options() options.stop_timeout = short_time environment = Environment(options=options) runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(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_spawn_zero_locusts(self): class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): tasks = [MyTaskSet] wait_time = constant(0.1) environment = Environment(options=mocked_options()) runner = LocalLocustRunner(environment, [MyTestLocust]) timeout = gevent.Timeout(2.0) timeout.start() try: runner.start(0, 1, wait=True) runner.hatching_greenlet.join() except gevent.Timeout: self.fail( "Got Timeout exception. A locust seems to have been spawned, even though 0 was specified." ) finally: timeout.cancel()
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_taskset_setup_method_exception(self): class User(Locust): setup_run_count = 0 task_run_count = 0 locust_error_count = 0 wait_time = constant(1) @task class task_set(TaskSet): def setup(self): User.setup_run_count += 1 raise Exception("some exception") @task def my_task(self): User.task_run_count += 1 environment = Environment(options=mocked_options()) def on_locust_error(*args, **kwargs): User.locust_error_count += 1 environment.events.locust_error.add_listener(on_locust_error) runner = LocalLocustRunner(environment, locust_classes=[User]) runner.start(locust_count=3, hatch_rate=3, wait=False) runner.hatching_greenlet.get(timeout=3) self.assertEqual(1, User.setup_run_count) self.assertEqual(1, User.locust_error_count) self.assertEqual(3, User.task_run_count)
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 environment = create_environment(mocked_options()) environment.stop_timeout = short_time runner = LocalLocustRunner(environment, [MyTestLocust]) runner.start(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_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_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_start_event(self): class User(Locust): wait_time = constant(1) task_run_count = 0 @task def my_task(self): User.task_run_count += 1 test_start_run = [0] environment = Environment(options=mocked_options()) def on_test_start(*args, **kwargs): test_start_run[0] += 1 environment.events.test_start.add_listener(on_test_start) runner = LocalLocustRunner(environment, locust_classes=[User]) runner.start(locust_count=3, hatch_rate=3, wait=False) runner.hatching_greenlet.get(timeout=3) self.assertEqual(1, test_start_run[0]) self.assertEqual(3, User.task_run_count)
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)
class task_set(TaskSet): @task def my_task(self): self.client.get("/") @task def task_404(self): self.client.get("/non-existing-path") # setup Environment and Runner env = Environment() runner = LocalLocustRunner(environment=env, locust_classes=[User]) # start a WebUI instance web_ui = WebUI(environment=env) gevent.spawn(lambda: web_ui.start("127.0.0.1", 8089)) # TODO: fix #def on_request_success(request_type, name, response_time, response_length, **kwargs): # report_to_grafana("%_%s" % (request_type, name), response_time) #env.events.request_succes.add_listener(on_request_success) # start a greenlet that periodically outputs the current stats gevent.spawn(stats_printer(runner.stats)) # start the test runner.start(1, hatch_rate=10) # wait for the greenlets (indefinitely) runner.greenlet.join()