diff --git a/tests/integration/clients/test_websocket_client.py b/tests/integration/clients/test_websocket_client.py new file mode 100644 index 000000000..c2fa86def --- /dev/null +++ b/tests/integration/clients/test_websocket_client.py @@ -0,0 +1,52 @@ +"""Performance testing of Websocket clients.""" + +from __future__ import annotations + +from tests.integration.integration_test_case import IntegrationTestCase +from tests.integration.it_utils import test_async_and_sync +from xrpl.models.currencies import XRP, IssuedCurrency +from xrpl.models.requests import BookOffers +from xrpl.models.response import ResponseStatus + + +class TestWebsocketClient(IntegrationTestCase): + """Memory usage of websocket client""" + + @test_async_and_sync(globals(), websockets_only=True) + async def test_msg_queue_growth_websocket_client(self, client): + """Test the rate of growth of the Message queue in websocket_client under + persistent load. Admittedly, this is not a precise measure, rather its a proxy + to measure the memory footprint of the client + """ + + for _ in range(5): + response = await client.request( + BookOffers( + ledger_index="current", + taker_gets=XRP(), + taker_pays=IssuedCurrency( + currency="USD", issuer="rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq" + ), + limit=500, + ) + ) + + self.assertEqual(response.status, ResponseStatus.SUCCESS) + + response = await client.request( + BookOffers( + ledger_index="current", + taker_gets=IssuedCurrency( + currency="USD", issuer="rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq" + ), + taker_pays=XRP(), + limit=500, + ) + ) + self.assertEqual(response.status, ResponseStatus.SUCCESS) + + self.assertEqual(client._messages.qsize(), 0) + + # the messages queue has not increased in proportion to the requests/responses + # input load + self.assertEqual(client._messages.qsize(), 0) diff --git a/xrpl/asyncio/clients/websocket_base.py b/xrpl/asyncio/clients/websocket_base.py index 339a33fb9..a4cea679a 100644 --- a/xrpl/asyncio/clients/websocket_base.py +++ b/xrpl/asyncio/clients/websocket_base.py @@ -136,9 +136,8 @@ async def _handler(self: Self) -> None: # if this response corresponds to request, fulfill the Future if "id" in response_dict and response_dict["id"] in self._open_requests: self._open_requests[response_dict["id"]].set_result(response_dict) - - # enqueue the response for the message queue - cast(_MESSAGES_TYPE, self._messages).put_nowait(response_dict) + else: + cast(_MESSAGES_TYPE, self._messages).put_nowait(response_dict) def _set_up_future(self: Self, request: Request) -> None: """