-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmain.rs
More file actions
418 lines (366 loc) · 14 KB
/
main.rs
File metadata and controls
418 lines (366 loc) · 14 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//! Compare the current engine control-channel path against the standalone
//! control-aware channel under heavy Ack/Nack traffic.
#![allow(missing_docs)]
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use otap_df_channel::mpsc;
use otap_df_control_channel::{
AckMsg as ControlAwareAckMsg, CompletionMsg as ControlAwareCompletionMsg, ControlChannelConfig,
ControlCmd, NackMsg as ControlAwareNackMsg, NodeControlEvent, NodeControlReceiver,
NodeControlSender, node_channel,
};
use otap_df_engine::control::{AckMsg, NackMsg, NodeControlMsg};
use otap_df_engine::local::message::{LocalReceiver as CurrentLocalReceiver, LocalSender};
use otap_df_engine::shared::message::{
SharedReceiver as CurrentSharedReceiver, SharedSender as CurrentSharedSender,
};
use otap_df_telemetry::reporter::MetricsReporter;
use std::future::{Future, poll_fn};
use std::hint::black_box;
use std::pin::Pin;
use std::task::Poll;
use tokio::runtime::Builder;
use tokio::task::LocalSet;
#[cfg(not(windows))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(windows))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
const COMPLETION_COUNT: usize = 100_000;
const CHANNEL_CAPACITY: usize = 1_024;
const COMPLETION_BATCH_MAX: usize = 32;
const NACK_EVERY: usize = 8;
const TIMER_EVERY: usize = 64;
const TELEMETRY_EVERY: usize = 256;
const CONFIG_EVERY: usize = 1_024;
const CANCELED_BLOCKED_SENDS: usize = 100_000;
#[derive(Clone, Copy, Debug)]
enum Scenario {
AckNackOnly,
AckNackWithControlNoise,
}
impl Scenario {
const fn bench_name(self) -> &'static str {
match self {
Self::AckNackOnly => "ack_nack_only",
Self::AckNackWithControlNoise => "ack_nack_with_control_noise",
}
}
const fn has_control_noise(self) -> bool {
matches!(self, Self::AckNackWithControlNoise)
}
}
#[derive(Debug, Default)]
struct ObservedWork {
completions: usize,
completion_batches: usize,
timer_ticks: usize,
telemetry_ticks: usize,
configs: usize,
}
fn control_aware_channel_config() -> ControlChannelConfig {
ControlChannelConfig {
completion_msg_capacity: CHANNEL_CAPACITY,
completion_batch_max: COMPLETION_BATCH_MAX,
completion_burst_limit: COMPLETION_BATCH_MAX,
}
}
async fn is_pending_once<F>(mut future: Pin<&mut F>) -> bool
where
F: Future,
{
poll_fn(|cx| Poll::Ready(future.as_mut().poll(cx).is_pending())).await
}
fn build_metrics_reporter() -> MetricsReporter {
MetricsReporter::create_new_and_receiver(16).1
}
async fn produce_current_local(tx: LocalSender<NodeControlMsg<usize>>, scenario: Scenario) {
let metrics_reporter = build_metrics_reporter();
for idx in 0..COMPLETION_COUNT {
if idx % NACK_EVERY == 0 {
tx.send(NodeControlMsg::Nack(NackMsg::new("temporary", idx)))
.await
.expect("nack should enqueue");
} else {
tx.send(NodeControlMsg::Ack(AckMsg::new(idx)))
.await
.expect("ack should enqueue");
}
if scenario.has_control_noise() {
if idx % TIMER_EVERY == 0 {
tx.send(NodeControlMsg::TimerTick {})
.await
.expect("timer tick should enqueue");
}
if idx % TELEMETRY_EVERY == 0 {
tx.send(NodeControlMsg::CollectTelemetry {
metrics_reporter: metrics_reporter.clone(),
})
.await
.expect("telemetry collection should enqueue");
}
if idx % CONFIG_EVERY == 0 {
tx.send(NodeControlMsg::Config {
config: serde_json::json!({ "seq": idx }),
})
.await
.expect("config should enqueue");
}
}
}
}
async fn consume_current_local(
mut rx: CurrentLocalReceiver<NodeControlMsg<usize>>,
) -> ObservedWork {
let mut observed = ObservedWork::default();
while let Ok(msg) = rx.recv().await {
match msg {
NodeControlMsg::Ack(_) | NodeControlMsg::Nack(_) => observed.completions += 1,
NodeControlMsg::TimerTick {} => observed.timer_ticks += 1,
NodeControlMsg::CollectTelemetry { .. } => observed.telemetry_ticks += 1,
NodeControlMsg::Config { .. } => observed.configs += 1,
NodeControlMsg::DrainIngress { .. }
| NodeControlMsg::MemoryPressureChanged { .. }
| NodeControlMsg::Shutdown { .. }
| NodeControlMsg::DelayedData { .. } => {
panic!("unexpected message in benchmark current local receiver");
}
}
}
observed
}
async fn produce_current_shared(
tx: CurrentSharedSender<NodeControlMsg<usize>>,
scenario: Scenario,
) {
let metrics_reporter = build_metrics_reporter();
for idx in 0..COMPLETION_COUNT {
if idx % NACK_EVERY == 0 {
tx.send(NodeControlMsg::Nack(NackMsg::new("temporary", idx)))
.await
.expect("nack should enqueue");
} else {
tx.send(NodeControlMsg::Ack(AckMsg::new(idx)))
.await
.expect("ack should enqueue");
}
if scenario.has_control_noise() {
if idx % TIMER_EVERY == 0 {
tx.send(NodeControlMsg::TimerTick {})
.await
.expect("timer tick should enqueue");
}
if idx % TELEMETRY_EVERY == 0 {
tx.send(NodeControlMsg::CollectTelemetry {
metrics_reporter: metrics_reporter.clone(),
})
.await
.expect("telemetry collection should enqueue");
}
if idx % CONFIG_EVERY == 0 {
tx.send(NodeControlMsg::Config {
config: serde_json::json!({ "seq": idx }),
})
.await
.expect("config should enqueue");
}
}
}
}
async fn consume_current_shared(
mut rx: CurrentSharedReceiver<NodeControlMsg<usize>>,
) -> ObservedWork {
let mut observed = ObservedWork::default();
while let Ok(msg) = rx.recv().await {
match msg {
NodeControlMsg::Ack(_) | NodeControlMsg::Nack(_) => observed.completions += 1,
NodeControlMsg::TimerTick {} => observed.timer_ticks += 1,
NodeControlMsg::CollectTelemetry { .. } => observed.telemetry_ticks += 1,
NodeControlMsg::Config { .. } => observed.configs += 1,
NodeControlMsg::DrainIngress { .. }
| NodeControlMsg::MemoryPressureChanged { .. }
| NodeControlMsg::Shutdown { .. }
| NodeControlMsg::DelayedData { .. } => {
panic!("unexpected message in benchmark current shared receiver");
}
}
}
observed
}
async fn send_control_aware_completion(tx: &NodeControlSender<usize>, idx: usize) {
let result = if idx.is_multiple_of(NACK_EVERY) {
tx.send(ControlCmd::Nack(ControlAwareNackMsg::new("temporary", idx)))
.await
} else {
tx.send(ControlCmd::Ack(ControlAwareAckMsg::new(idx))).await
};
let _ = result.expect("control-aware completion send should succeed");
}
async fn produce_control_aware(tx: NodeControlSender<usize>, scenario: Scenario) {
for idx in 0..COMPLETION_COUNT {
send_control_aware_completion(&tx, idx).await;
if scenario.has_control_noise() {
if idx % TIMER_EVERY == 0 {
let result = tx.try_send(ControlCmd::TimerTick);
assert!(result.is_ok(), "timer tick should not fail");
}
if idx % TELEMETRY_EVERY == 0 {
let result = tx.try_send(ControlCmd::CollectTelemetry);
assert!(result.is_ok(), "telemetry tick should not fail");
}
if idx % CONFIG_EVERY == 0 {
let result = tx.try_send(ControlCmd::Config {
config: serde_json::json!({ "seq": idx }),
});
assert!(result.is_ok(), "config should not fail");
}
}
}
}
async fn consume_control_aware(mut rx: NodeControlReceiver<usize>) -> ObservedWork {
let mut observed = ObservedWork::default();
while let Some(event) = rx.recv().await {
match event {
NodeControlEvent::CompletionBatch(batch) => {
observed.completion_batches += 1;
observed.completions += batch.len();
for completion in batch {
match completion {
ControlAwareCompletionMsg::Ack(_) | ControlAwareCompletionMsg::Nack(_) => {}
}
}
}
NodeControlEvent::TimerTick => observed.timer_ticks += 1,
NodeControlEvent::CollectTelemetry => observed.telemetry_ticks += 1,
NodeControlEvent::Config { .. } => observed.configs += 1,
NodeControlEvent::Shutdown(_) => {
panic!("unexpected event in control-aware benchmark receiver");
}
}
}
observed
}
async fn run_current_local_workload(scenario: Scenario) -> ObservedWork {
let (tx_raw, rx_raw) = mpsc::Channel::new(CHANNEL_CAPACITY);
let tx = LocalSender::mpsc(tx_raw);
let rx = CurrentLocalReceiver::mpsc(rx_raw);
let ((), observed) = tokio::join!(
produce_current_local(tx, scenario),
consume_current_local(rx)
);
assert_eq!(observed.completions, COMPLETION_COUNT);
observed
}
async fn run_current_shared_workload(scenario: Scenario) -> ObservedWork {
let (tx_raw, rx_raw) = tokio::sync::mpsc::channel(CHANNEL_CAPACITY);
let tx = CurrentSharedSender::mpsc(tx_raw);
let rx = CurrentSharedReceiver::mpsc(rx_raw);
let ((), observed) = tokio::join!(
produce_current_shared(tx, scenario),
consume_current_shared(rx)
);
assert_eq!(observed.completions, COMPLETION_COUNT);
observed
}
async fn run_control_aware_workload(scenario: Scenario) -> ObservedWork {
let (tx, rx) =
node_channel(control_aware_channel_config()).expect("control-aware channel config valid");
let ((), observed) = tokio::join!(
produce_control_aware(tx, scenario),
consume_control_aware(rx)
);
assert_eq!(observed.completions, COMPLETION_COUNT);
observed
}
async fn run_control_aware_canceled_sender_churn() -> usize {
let (tx, mut rx) =
node_channel(control_aware_channel_config()).expect("control-aware channel config valid");
let _ = tx
.try_send(ControlCmd::Ack(ControlAwareAckMsg::new(0)))
.expect("seed completion should enqueue");
for idx in 0..CANCELED_BLOCKED_SENDS {
let mut blocked =
std::pin::pin!(tx.send(ControlCmd::Ack(ControlAwareAckMsg::new(idx + 1))));
assert!(is_pending_once(blocked.as_mut()).await);
}
let mut live = std::pin::pin!(tx.send(ControlCmd::Ack(ControlAwareAckMsg::new(
CANCELED_BLOCKED_SENDS + 1,
))));
assert!(is_pending_once(live.as_mut()).await);
let first_batch = rx.recv().await;
assert!(matches!(
first_batch,
Some(NodeControlEvent::CompletionBatch(_))
));
let _ = live
.await
.expect("live blocked send should complete after capacity is freed");
let second_batch = rx.recv().await;
assert!(matches!(
second_batch,
Some(NodeControlEvent::CompletionBatch(_))
));
CANCELED_BLOCKED_SENDS
}
fn bench_control_channels(c: &mut Criterion) {
let rt = Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to build tokio runtime");
let cores = core_affinity::get_core_ids().expect("couldn't get core IDs");
let core = cores.iter().last().expect("no cores found");
_ = core_affinity::set_for_current(*core);
let mut group = c.benchmark_group("control_channel_ack_nack");
_ = group.throughput(Throughput::Elements(COMPLETION_COUNT as u64));
for scenario in [Scenario::AckNackOnly, Scenario::AckNackWithControlNoise] {
let _ = group.bench_function(
BenchmarkId::new("current_local", scenario.bench_name()),
|b| {
b.to_async(&rt).iter(|| async {
let local = LocalSet::new();
let observed = local
.run_until(async { run_current_local_workload(scenario).await })
.await;
let _ = black_box(observed);
});
},
);
let _ = group.bench_function(
BenchmarkId::new("control_aware", scenario.bench_name()),
|b| {
b.to_async(&rt).iter(|| async {
let local = LocalSet::new();
let observed = local
.run_until(async { run_control_aware_workload(scenario).await })
.await;
let _ = black_box(observed);
});
},
);
let _ = group.bench_function(
BenchmarkId::new("current_shared", scenario.bench_name()),
|b| {
b.to_async(&rt).iter(|| async {
let observed = run_current_shared_workload(scenario).await;
let _ = black_box(observed);
});
},
);
}
group.finish();
let mut churn_group = c.benchmark_group("control_channel_blocked_sender_churn");
let _ = churn_group.throughput(Throughput::Elements(CANCELED_BLOCKED_SENDS as u64));
let _ = churn_group.bench_function("control_aware", |b| {
b.to_async(&rt).iter(|| async {
let local = LocalSet::new();
let churned = local
.run_until(async { run_control_aware_canceled_sender_churn().await })
.await;
let _ = black_box(churned);
});
});
churn_group.finish();
}
criterion_group!(benches, bench_control_channels);
criterion_main!(benches);