-
Notifications
You must be signed in to change notification settings - Fork 1
/
flooding_async.rs
157 lines (129 loc) · 3.64 KB
/
flooding_async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use clap::Parser;
use netsim_async::{HasBytesSize, SimConfiguration, SimId, SimSocket};
use netsim_core::{time::Duration, Bandwidth, Edge, EdgePolicy, Latency, NodePolicy, PacketLoss};
use tokio::time::{sleep, Instant};
type SimContext = netsim_async::SimContext<Msg>;
#[derive(Parser)]
struct Command {
#[arg(long, default_value = "60")]
time: Duration,
#[arg(long, default_value = "10")]
every: Duration,
#[arg(long, default_value = "10")]
idle: Duration,
}
const LATENCY: std::time::Duration = std::time::Duration::from_millis(60);
#[tokio::main]
async fn main() {
let cmd = Command::parse();
let configuration = SimConfiguration {
idle_duration: cmd.idle.into_duration(),
..SimConfiguration::default()
};
let mut context: SimContext = SimContext::with_config(configuration);
let sink = Sink {
socket: context.open().unwrap(),
};
let tap = Tap {
socket: context.open().unwrap(),
sink_id: sink.socket.id(),
every: cmd.every,
};
context
.set_node_policy(
sink.socket.id(),
NodePolicy {
bandwidth_down: Bandwidth::MAX,
bandwidth_up: Bandwidth::MAX,
location: None,
},
)
.unwrap();
context
.set_node_policy(
tap.socket.id(),
NodePolicy {
bandwidth_down: Bandwidth::MAX,
bandwidth_up: Bandwidth::MAX,
location: None,
},
)
.unwrap();
context
.set_edge_policy(
Edge::new((tap.socket.id(), sink.socket.id())),
EdgePolicy {
latency: Latency::new(LATENCY),
packet_loss: PacketLoss::NONE,
..Default::default()
},
)
.unwrap();
let sink = tokio::spawn(sink.work());
let tap = tokio::spawn(tap.work());
sleep(cmd.time.into_duration()).await;
context.shutdown().unwrap();
sink.await.unwrap();
tap.await.unwrap();
}
struct Sink {
socket: SimSocket<Msg>,
}
impl Sink {
async fn work(mut self) {
let mut delays = Vec::with_capacity(1_000_000);
while let Some((_from, msg)) = self.socket.recv().await {
let latency = msg.time.elapsed();
let diff = if latency < LATENCY {
LATENCY - latency
} else {
latency - LATENCY
};
delays.push(diff);
}
let len = delays.len();
let total = delays.iter().copied().sum::<std::time::Duration>();
let avg = total / delays.len() as u32;
println!("sent {len} messages over. Msg received with an average of {avg:?} delays to the expected LATENCY");
for (i, delay) in delays.iter().copied().enumerate().take(10) {
println!("[{i}]: additional latency of {delay:?}");
}
println!("...");
}
}
struct Tap {
socket: SimSocket<Msg>,
sink_id: SimId,
every: Duration,
}
impl Tap {
fn send_msg(&mut self) -> bool {
let msg = Msg::new(1);
self.socket.send_to(self.sink_id, msg).is_ok()
}
async fn work(mut self) {
while self.send_msg() {
let now = Instant::now();
sleep(self.every.into_duration()).await;
let elapsed = now.elapsed();
println!("{elapsed:?}");
}
}
}
struct Msg {
time: Instant,
size: u64,
}
impl Msg {
fn new(size: u64) -> Self {
Self {
time: Instant::now(),
size,
}
}
}
impl HasBytesSize for Msg {
fn bytes_size(&self) -> u64 {
self.size
}
}