def test_spawn_uneven_locusts(self): """ Tests that we can accurately spawn a certain number of locusts, even if it's not an even number of the connected slaves """ class MyTestLocust(Locust): pass with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) for i in range(5): server.mocked_send( Message("client_ready", None, "fake_client%i" % i)) master.start_hatching(7, 7) self.assertEqual(5, len(server.outbox)) num_clients = 0 for msg in server.outbox: num_clients += Message.unserialize(msg).data["num_clients"] self.assertEqual( 7, num_clients, "Total number of locusts that would have been spawned is not 7" )
def test_master_total_stats(self): class MyTestLocust(Locust): pass with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) server.mocked_send(Message("client_ready", None, "fake_client")) stats = RequestStats() stats.log_request("GET", "/1", 100, 3546) stats.log_request("GET", "/1", 800, 56743) stats2 = RequestStats() stats2.log_request("GET", "/2", 700, 2201) server.mocked_send( Message( "stats", { "stats": stats.serialize_stats(), "stats_total": stats.total.serialize(), "errors": stats.serialize_errors(), "user_count": 1, }, "fake_client")) server.mocked_send( Message( "stats", { "stats": stats2.serialize_stats(), "stats_total": stats2.total.serialize(), "errors": stats2.serialize_errors(), "user_count": 2, }, "fake_client")) self.assertEqual(700, master.stats.total.median_response_time)
def test_slave_stats_report_median(self): class MyTestLocust(Locust): pass with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) server.mocked_send(Message("client_ready", None, "fake_client")) master.stats.get("/", "GET").log(100, 23455) master.stats.get("/", "GET").log(800, 23455) master.stats.get("/", "GET").log(700, 23455) data = {"user_count": 1} events.report_to_master.fire(client_id="fake_client", data=data) master.stats.clear_all() server.mocked_send(Message("stats", data, "fake_client")) s = master.stats.get("/", "GET") self.assertEqual(700, s.median_response_time)
def test_spawn_fewer_locusts_than_slaves(self): class MyTestLocust(Locust): pass with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) for i in range(5): server.mocked_send( Message("client_ready", None, "fake_client%i" % i)) master.start_hatching(2, 2) self.assertEqual(5, len(server.outbox)) num_clients = 0 for msg in server.outbox: num_clients += Message.unserialize(msg).data["num_clients"] self.assertEqual( 2, num_clients, "Total number of locusts that would have been spawned is not 2" )
def test_slave_connect(self): class MyTestLocust(Locust): pass with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) server.mocked_send( Message("client_ready", None, "zeh_fake_client1")) self.assertEqual(1, len(master.clients)) self.assertTrue( "zeh_fake_client1" in master.clients, "Could not find fake client in master instance's clients dict") server.mocked_send( Message("client_ready", None, "zeh_fake_client2")) server.mocked_send( Message("client_ready", None, "zeh_fake_client3")) server.mocked_send( Message("client_ready", None, "zeh_fake_client4")) self.assertEqual(4, len(master.clients)) server.mocked_send(Message("quit", None, "zeh_fake_client3")) self.assertEqual(3, len(master.clients))
def test_message_serialize(self): msg = Message("client_ready", None, "my_id") rebuilt = Message.unserialize(msg.serialize()) self.assertEqual(msg.type, rebuilt.type) self.assertEqual(msg.data, rebuilt.data) self.assertEqual(msg.node_id, rebuilt.node_id)
def recv(self): results = self.queue.get() return Message.unserialize(results)
def test_master_current_response_times(self): class MyTestLocust(Locust): pass start_time = 1 with mock.patch("time.time") as mocked_time: mocked_time.return_value = start_time global_stats.reset_all() with mock.patch("orange_locust.rpc.rpc.Server", mocked_rpc_server()) as server: master = MasterLocustRunner(MyTestLocust, self.options) mocked_time.return_value += 1 server.mocked_send(Message("client_ready", None, "fake_client")) stats = RequestStats() stats.log_request("GET", "/1", 100, 3546) stats.log_request("GET", "/1", 800, 56743) server.mocked_send( Message( "stats", { "stats": stats.serialize_stats(), "stats_total": stats.total.get_stripped_report(), "errors": stats.serialize_errors(), "user_count": 1, }, "fake_client")) mocked_time.return_value += 1 stats2 = RequestStats() stats2.log_request("GET", "/2", 400, 2201) server.mocked_send( Message( "stats", { "stats": stats2.serialize_stats(), "stats_total": stats2.total.get_stripped_report(), "errors": stats2.serialize_errors(), "user_count": 2, }, "fake_client")) mocked_time.return_value += 4 self.assertEqual( 400, master.stats.total.get_current_response_time_percentile( 0.5)) self.assertEqual( 800, master.stats.total.get_current_response_time_percentile( 0.95)) # let 10 second pass, do some more requests, send it to the master and make # sure the current response time percentiles only accounts for these new requests mocked_time.return_value += 10 stats.log_request("GET", "/1", 20, 1) stats.log_request("GET", "/1", 30, 1) stats.log_request("GET", "/1", 3000, 1) server.mocked_send( Message( "stats", { "stats": stats.serialize_stats(), "stats_total": stats.total.get_stripped_report(), "errors": stats.serialize_errors(), "user_count": 2, }, "fake_client")) self.assertEqual( 30, master.stats.total.get_current_response_time_percentile( 0.5)) self.assertEqual( 3000, master.stats.total.get_current_response_time_percentile( 0.95))