forked from agntcy/slim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_stream_common.rs
More file actions
105 lines (93 loc) · 3.42 KB
/
Copy pathprocess_stream_common.rs
File metadata and controls
105 lines (93 loc) · 3.42 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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
//! Shared setup for `process_stream` benchmarks (Criterion timing and allocation harness).
use std::sync::OnceLock;
use std::time::Duration;
use slim_datapath::api::{ProtoMessage, ProtoName};
use slim_datapath::message_processing::MessageProcessor;
use tokio::runtime::Runtime;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;
pub fn runtime() -> &'static Runtime {
static RT: OnceLock<Runtime> = OnceLock::new();
RT.get_or_init(|| Runtime::new().expect("tokio runtime"))
}
pub fn bench_destination() -> ProtoName {
ProtoName::from_strings(["org", "ns", "bench-dst"]).with_id(2)
}
/// Inbound subscribe from a remote peer (valid SLIM header so rebuild returns `Some`).
pub fn make_remote_subscribe_for_cp_mirror() -> ProtoMessage {
let source = ProtoName::from_strings(["org", "ns", "bench-src"]).with_id(1);
ProtoMessage::builder()
.source(source)
.destination(bench_destination())
.subscription_id(42)
.build_subscribe()
.expect("valid subscribe for bench")
}
pub async fn assert_cp_mirror_rebuild_path() {
let processor = MessageProcessor::new();
let (cp_tx, mut cp_rx) = mpsc::channel(8);
processor.bench_set_control_plane_tx(cp_tx);
let conn_id = processor.bench_register_remote_connection();
let (in_tx, in_rx) = mpsc::channel(4);
let cancel = CancellationToken::new();
let handle = processor
.bench_process_stream(
ReceiverStream::new(in_rx),
conn_id,
None,
cancel.clone(),
false,
false,
)
.expect("bench_process_stream");
let msg = make_remote_subscribe_for_cp_mirror();
let expected_dst = msg.get_dst();
in_tx.send(Ok(msg)).await.expect("send inbound");
let mirrored = cp_rx
.recv()
.await
.expect("control plane should receive message")
.expect("mirror Ok");
assert_eq!(
mirrored.get_dst(),
expected_dst,
"mirrored message must carry subscription destination for the control plane"
);
assert!(
mirrored.get_subscription_id().is_some(),
"mirrored subscribe should carry subscription_id"
);
cancel.cancel();
match tokio::time::timeout(Duration::from_secs(2), handle).await {
Ok(Ok(())) => {}
Ok(Err(e)) => panic!("bench_process_stream task failed: {e}"),
Err(_) => panic!("join timeout"),
}
}
pub async fn one_iteration_cp_mirror() {
let processor = MessageProcessor::new();
let (cp_tx, mut cp_rx) = mpsc::channel(8);
processor.bench_set_control_plane_tx(cp_tx);
let conn_id = processor.bench_register_remote_connection();
let (in_tx, in_rx) = mpsc::channel(4);
let cancel = CancellationToken::new();
let handle = processor
.bench_process_stream(
ReceiverStream::new(in_rx),
conn_id,
None,
cancel.clone(),
false,
false,
)
.expect("bench_process_stream");
let msg = make_remote_subscribe_for_cp_mirror();
in_tx.send(Ok(msg)).await.expect("send inbound");
let _ = cp_rx.recv().await;
cancel.cancel();
let _join: Result<Result<(), tokio::task::JoinError>, tokio::time::error::Elapsed> =
tokio::time::timeout(Duration::from_secs(2), handle).await;
}