Skip to content

Commit

Permalink
Benchmarks: easynetwork server use exposed API for line serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Jun 21, 2024
1 parent 2ed0697 commit 6f8d883
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions benchmark_server/servers/easynetwork_tcp_echoserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

from easynetwork.protocol import StreamProtocol
from easynetwork.serializers.abc import BufferedIncrementalPacketSerializer
from easynetwork.serializers.base_stream import _buffered_readuntil
from easynetwork.serializers.tools import GeneratorStreamReader
from easynetwork.serializers.base_stream import AutoSeparatedPacketSerializer
from easynetwork.servers.handlers import AsyncStreamClient, AsyncStreamRequestHandler
from easynetwork.servers.standalone_tcp import StandaloneTCPNetworkServer

Expand All @@ -36,26 +35,17 @@ def buffered_incremental_deserialize(self, buffer: memoryview) -> Generator[int
return bytes(buffer[:offset]), b""


class LineSerializer(BufferedIncrementalPacketSerializer[bytes, bytes, bytearray]):
class LineSerializer(AutoSeparatedPacketSerializer[bytes, bytes]):
__slots__ = ()

def incremental_serialize(self, packet: bytes) -> Generator[bytes, None, None]:
yield packet
def __init__(self) -> None:
super().__init__(separator=b"\n", incremental_serialize_check_separator=False, limit=65536)

def incremental_deserialize(self) -> Generator[None, bytes, tuple[bytes, bytes]]:
reader = GeneratorStreamReader()
packet = yield from reader.read_until(b"\n", limit=65536)
remainder = reader.read_all()
return packet, remainder

def create_deserializer_buffer(self, sizehint: int) -> bytearray:
return bytearray(sizehint)

def buffered_incremental_deserialize(self, buffer: bytearray) -> Generator[int | None, int, tuple[bytes, memoryview]]:
with memoryview(buffer) as buffer_view:
_, offset, buflen = yield from _buffered_readuntil(buffer, b"\n")
del buffer
return bytes(buffer_view[:offset]), buffer_view[offset:buflen]
def serialize(self, packet: bytes) -> bytes:
return packet

def deserialize(self, data: bytes) -> bytes:
return data


class EchoRequestHandler(AsyncStreamRequestHandler[Any, Any]):
Expand Down

0 comments on commit 6f8d883

Please sign in to comment.