Optimized pydantic use
All protocol classes have now been duplicated in order to minimize the use of pydantic validation The "pure" classes are named what the previous validated classes were, now each transport has its transport name prefixed on the class. For example before we had Heartbeat, now we have Heartbeat and JsonHeartbeat This new architecture allows us to reduce the number of useless backend validation steps created when copying and processing data, reducing CPU stress by roughly 10-20% in real world testing
This commit is contained in:
11 files changed
+359
-88
No files matched your search
+13
-6
@@ -13,6 +13,13 @@ from rich.logging import RichHandler
|
||||
from dynalab_core.protocols.common import VersionDescriptor
|
||||
from dynalab_core.protocols.constants import PROTOCOL_VERSION
|
||||
from dynalab_core.protocols.json.wire import read_message, write_message
|
||||
from dynalab_core.protocols.packets import (
|
||||
JsonConnectorHello,
|
||||
JsonDynaLabHello,
|
||||
JsonHeartbeat,
|
||||
JsonValueBatch,
|
||||
JsonValueDescriptor,
|
||||
)
|
||||
from dynalab_core.protocols.packets.data import ValueBatch, ValueDescriptor
|
||||
from dynalab_core.protocols.packets.handshake import (
|
||||
ConnectorHello,
|
||||
@@ -124,13 +131,13 @@ def value_sender_thread(
|
||||
tau * SINE_FREQUENCY_HZ * (perf_counter() - sine_start_time)
|
||||
)
|
||||
timestamp = monotonic_ns()
|
||||
message = ValueBatch(values=[])
|
||||
value1 = ValueDescriptor(
|
||||
message = JsonValueBatch(values=[])
|
||||
value1 = JsonValueDescriptor(
|
||||
signal_id=signal_id_1,
|
||||
value=2.0 * sine_value,
|
||||
timestamp=timestamp,
|
||||
)
|
||||
value2 = ValueDescriptor(
|
||||
value2 = JsonValueDescriptor(
|
||||
signal_id=signal_id_2,
|
||||
value=sine_value,
|
||||
timestamp=timestamp,
|
||||
@@ -203,7 +210,7 @@ async def perform_handshake(
|
||||
except TimeoutError as exc:
|
||||
raise TimeoutError("Timed out waiting for server hello") from exc
|
||||
|
||||
if not isinstance(message, DynaLabHello):
|
||||
if not isinstance(message, JsonDynaLabHello):
|
||||
raise RuntimeError(f"Expected DynaLabHello, received {type(message).__name__}")
|
||||
|
||||
log.debug(
|
||||
@@ -211,7 +218,7 @@ async def perform_handshake(
|
||||
message.instance_id,
|
||||
)
|
||||
|
||||
connector_hello = ConnectorHello(
|
||||
connector_hello = JsonConnectorHello(
|
||||
connector_uuid=uuid4(),
|
||||
protocol_version=PROTOCOL_VERSION,
|
||||
connector_name="Test connector",
|
||||
@@ -252,7 +259,7 @@ async def receive_messages(
|
||||
while True:
|
||||
message = await read_message(reader)
|
||||
|
||||
if isinstance(message, Heartbeat):
|
||||
if isinstance(message, JsonHeartbeat):
|
||||
message.return_timestamp = monotonic_ms()
|
||||
|
||||
await send_message(
|
||||
|
||||
@@ -10,7 +10,12 @@ from dynalab_core.config import CoreConfig
|
||||
from dynalab_core.errors import JsonServerError
|
||||
from dynalab_core.protocols.constants import PROTOCOL_VERSION
|
||||
from dynalab_core.protocols.common import VersionDescriptor
|
||||
from dynalab_core.protocols.packets.handshake import ConnectorHello, SignalDescriptor
|
||||
from dynalab_core.protocols.packets import JsonConnectorHello
|
||||
from dynalab_core.protocols.packets.handshake import (
|
||||
ConnectorHello,
|
||||
JsonSignalDescriptor,
|
||||
SignalDescriptor,
|
||||
)
|
||||
from test.common import find_available_port
|
||||
|
||||
|
||||
@@ -141,12 +146,12 @@ def test_rejected_handshake_is_not_logged_as_connected(
|
||||
port = find_available_port(8765)
|
||||
core = Core(CoreConfig(port=port))
|
||||
connector_uuid = uuid4()
|
||||
connector_hello = ConnectorHello(
|
||||
connector_hello = JsonConnectorHello(
|
||||
connector_uuid=connector_uuid,
|
||||
protocol_version=VersionDescriptor(type="alpha", major=999, minor=0, patch=0),
|
||||
connector_name="Incompatible connector",
|
||||
connector_version="0.1.0-test",
|
||||
signals=[SignalDescriptor(id=uuid4(), name="Dummy signal", type="number")],
|
||||
signals=[JsonSignalDescriptor(id=uuid4(), name="Dummy signal", type="number")],
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger="dynalab_core"):
|
||||
@@ -176,12 +181,12 @@ def test_json_server_waits_for_connection_handlers_on_stop(
|
||||
port = find_available_port(8765)
|
||||
core = Core(CoreConfig(port=port))
|
||||
connector_uuid = uuid4()
|
||||
connector_hello = ConnectorHello(
|
||||
connector_hello = JsonConnectorHello(
|
||||
connector_uuid=connector_uuid,
|
||||
protocol_version=PROTOCOL_VERSION,
|
||||
connector_name="Test connector",
|
||||
connector_version="0.1.0-test",
|
||||
signals=[SignalDescriptor(id=uuid4(), name="Dummy signal", type="number")],
|
||||
signals=[JsonSignalDescriptor(id=uuid4(), name="Dummy signal", type="number")],
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.DEBUG, logger="dynalab_core"):
|
||||
|
||||
Reference in new issue
Block a user