def test_distributed_integration_run(self): """ Full integration test that starts both a MasterRunner and three WorkerRunner instances and makes sure that their stats is sent to the Master. """ class TestUser(User): 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(user_classes=[TestUser]) master = master_env.create_master_runner("*", 0) sleep(0) # start 3 Worker runners workers = [] for i in range(3): worker_env = Environment(user_classes=[TestUser]) worker = worker_env.create_worker_runner( "127.0.0.1", 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_stats_history(self): env1 = Environment(events=locust.events, catch_exceptions=False) runner1 = env1.create_master_runner("127.0.0.1", 5558) env2 = Environment(events=locust.events, catch_exceptions=False) runner2 = env2.create_worker_runner("127.0.0.1", 5558) greenlet1 = gevent.spawn(stats_history, runner1) greenlet2 = gevent.spawn(stats_history, runner2) gevent.sleep(1) hs1 = runner1.stats.history hs2 = runner2.stats.history gevent.kill(greenlet1) gevent.kill(greenlet2) self.assertEqual(1, len(hs1)) self.assertEqual(0, len(hs2))
from locust.env import Environment from locust import HttpUser, task, SequentialTaskSet from locust_plugins.transaction_manager import TransactionManager class ExampleSequentialTaskSet(SequentialTaskSet): def on_start(self): self.tm = TransactionManager() @task def home(self): self.tm.start_transaction("startup") self.client.get("/", name="01 /") @task def get_config_json(self): self.client.get("/config.json", name="02 /config.json") self.tm.end_transaction("startup") class TranactionExample(HttpUser): host = "https://www.demoblaze.com" tasks = [ExampleSequentialTaskSet] env = Environment(user_classes=[TranactionExample]) env.create_worker_runner("127.0.0.1", 5557) locust.events.init.fire(environment=env, runner=env.runner) env.events.report_to_master.add_listener(TransactionManager._report_to_master) env.runner.greenlet.join()