randomize network latency

This commit is contained in:
Youngjoon Lee 2024-07-05 17:29:20 +09:00
parent e6db41fbfd
commit 2e58207cf0
No known key found for this signature in database
GPG Key ID: B4253AFBA618BF4D
4 changed files with 23 additions and 9 deletions

View File

@ -38,11 +38,21 @@ class Config:
@dataclass @dataclass
class SimulationConfig: class SimulationConfig:
duration_sec: int duration_sec: int
net_latency_sec: float network: NetworkConfig
def validate(self): def validate(self):
assert self.duration_sec > 0 assert self.duration_sec > 0
assert self.net_latency_sec > 0 self.network.validate()
@dataclass
class NetworkConfig:
max_latency_sec: float
seed: random.Random
def validate(self):
assert self.max_latency_sec > 0
assert self.seed is not None
@dataclass @dataclass

View File

@ -1,6 +1,8 @@
simulation: simulation:
duration_sec: 1000 duration_sec: 1000
net_latency_sec: 0.01 network:
max_latency_sec: 0.01
seed: 0
logic: logic:
sender_lottery: sender_lottery:

View File

@ -5,12 +5,12 @@ import pandas
from mixnet.connection import SimplexConnection from mixnet.connection import SimplexConnection
from mixnet.framework.framework import Framework, Queue from mixnet.framework.framework import Framework, Queue
from mixnet.sim.config import SimulationConfig from mixnet.sim.config import NetworkConfig
class MeteredRemoteSimplexConnection(SimplexConnection): class MeteredRemoteSimplexConnection(SimplexConnection):
framework: Framework framework: Framework
config: SimulationConfig latency: float
outputs: Queue outputs: Queue
conn: Queue conn: Queue
inputs: Queue inputs: Queue
@ -19,9 +19,9 @@ class MeteredRemoteSimplexConnection(SimplexConnection):
input_task: Awaitable input_task: Awaitable
input_meters: list[int] input_meters: list[int]
def __init__(self, config: SimulationConfig, framework: Framework): def __init__(self, config: NetworkConfig, framework: Framework):
self.framework = framework self.framework = framework
self.config = config self.latency = config.seed.random() * config.max_latency_sec
self.outputs = framework.queue() self.outputs = framework.queue()
self.conn = framework.queue() self.conn = framework.queue()
self.inputs = framework.queue() self.inputs = framework.queue()
@ -49,7 +49,7 @@ class MeteredRemoteSimplexConnection(SimplexConnection):
data = await self.conn.get() data = await self.conn.get()
if data is None: if data is None:
break break
await self.framework.sleep(self.config.net_latency_sec) await self.framework.sleep(self.latency)
self.__update_meter(self.input_meters, len(data), start_time) self.__update_meter(self.input_meters, len(data), start_time)
await self.inputs.put(data) await self.inputs.put(data)

View File

@ -64,7 +64,9 @@ class Simulation:
return nodes, conn_stats return nodes, conn_stats
def create_conn(self) -> MeteredRemoteSimplexConnection: def create_conn(self) -> MeteredRemoteSimplexConnection:
return MeteredRemoteSimplexConnection(self.config.simulation, self.framework) return MeteredRemoteSimplexConnection(
self.config.simulation.network, self.framework
)
async def run_logic(self, node: Node): async def run_logic(self, node: Node):
lottery_config = self.config.logic.sender_lottery lottery_config = self.config.logic.sender_lottery