def get_runner(self, environment=None, locust_classes=[]): if environment is None: environment = self.environment return WorkerLocustRunner(environment, locust_classes, master_host="localhost", master_port=5557)
def test_on_hatch(self): import mock class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): task_set = MyTaskSet min_wait = 100 max_wait = 100 with mock.patch("locust.rpc.rpc.WorkerClient", mocked_rpc_server()) as client: self.worker = WorkerLocustRunner([MyTestLocust], config.locust_config()) data = { "hatch_rate": 10, "num_clients": 10, "num_requests": None, "host": 'host', "stop_timeout": None } client.mocked_send('all', Message("hatch", data, "slave")) sleep(2) self.assertEqual(10, self.worker.user_count)
def test_stats_reporting(self): import mock class MyTestLocust(Locust): pass with mock.patch("locust.rpc.rpc.WorkerClient", mocked_rpc_server()) as client: self.worker = WorkerLocustRunner([MyTestLocust], config.locust_config()) self.worker.stats.get("Task", "/", "GET").log(100, 23455) self.worker.stats.get("Task", "/", "GET").log(800, 23455) self.worker.stats.get("Task", "/", "GET").log(700, 23455) sleep(runners.worker.WORKER_STATS_INTERVAL) data = Message.unserialize(client.outbox_all[-1]).data self.assertEqual({ 800: 1, 100: 1, 700: 1 }, data['stats'][0]['response_times']) self.assertEqual(3, data['stats'][0]['num_requests']) self.assertEqual(1600, data['stats'][0]['total_response_time'])
def test_distributed_integration_run(self): """ Full integration test that starts both a MasterLocustRunner and three WorkerLocustRunner instances and makes sure that their stats is sent to the Master. """ class TestUser(Locust): wait_time = constant(0.1) @task def incr_stats(l): l.environment.events.request_success.fire( request_type="GET", name="/", response_time=1337, response_length=666, ) with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3): # start a Master runner master_env = Environment() master = MasterLocustRunner(master_env, [TestUser], master_bind_host="*", master_bind_port=0) sleep(0) # start 3 Worker runners workers = [] for i in range(3): worker_env = Environment() worker = WorkerLocustRunner(worker_env, [TestUser], master_host="127.0.0.1", master_port=master.server.port) workers.append(worker) # give workers time to connect sleep(0.1) # issue start command that should trigger TestUsers to be spawned in the Workers master.start(6, hatch_rate=1000) sleep(0.1) # check that slave nodes have started locusts for worker in workers: self.assertEqual(2, worker.user_count) # give time for users to generate stats, and stats to be sent to master sleep(1) master.quit() # make sure users are killed for worker in workers: self.assertEqual(0, worker.user_count) # check that stats are present in master self.assertGreater( master_env.runner.stats.total.num_requests, 20, "For some reason the master node's stats has not come in", )
def test_on_ping(self): import mock class MyTestLocust(Locust): pass with mock.patch("locust.rpc.rpc.WorkerClient", mocked_rpc_server()) as client: self.worker = WorkerLocustRunner(MyTestLocust, config.locust_config()) sleep(0) self.assertEqual(2, len(client.outbox_all)) client.mocked_send('all', Message("ping", None, "master")) sleep(0) self.assertEqual(3, len(client.outbox_all))