Skip to content

Commit d1a364b

Browse files
chore: use cancellation token
1 parent 94640f0 commit d1a364b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ic-agent/benches/perf_route_provider.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use ic_agent::agent::http_transport::{
1414
route_provider::{RoundRobinRouteProvider, RouteProvider},
1515
};
1616
use reqwest::Client;
17-
use tokio::{runtime::Handle, sync::oneshot, time::sleep};
17+
use tokio::{runtime::Handle, sync::oneshot};
18+
use tokio_util::sync::CancellationToken;
1819

1920
// To run the benchmark use the command:
2021
// $ cargo bench --bench perf_route_provider --features bench
@@ -39,7 +40,9 @@ fn benchmark_route_providers(c: &mut Criterion) {
3940
.expect("failed to create runtime");
4041

4142
// Setup all route providers
42-
let route_providers = setup_route_providers(nodes_count, runtime.handle().clone());
43+
let token = CancellationToken::new();
44+
let route_providers =
45+
setup_route_providers(nodes_count, runtime.handle().clone(), token.clone());
4346

4447
for (name, instance) in route_providers {
4548
group.bench_function(name, |b| {
@@ -48,6 +51,7 @@ fn benchmark_route_providers(c: &mut Criterion) {
4851
})
4952
});
5053
}
54+
token.cancel();
5155
group.finish();
5256
}
5357

@@ -96,6 +100,7 @@ async fn setup_dynamic_route_provider<S: RoutingSnapshot + 'static>(
96100
fn setup_route_providers(
97101
nodes_count: usize,
98102
runtime: Handle,
103+
cancellation_token: CancellationToken,
99104
) -> Vec<(String, Arc<dyn RouteProvider>)> {
100105
// Assemble all instances for benching.
101106
let mut route_providers = vec![];
@@ -106,10 +111,11 @@ fn setup_route_providers(
106111
));
107112
// Setup dynamic round-robin route provider
108113
let (tx, rx) = oneshot::channel();
114+
let token_cloned = cancellation_token.clone();
109115
runtime.spawn(async move {
110116
let rp = setup_dynamic_route_provider(nodes_count, RoundRobinRoutingSnapshot::new()).await;
111117
tx.send(rp).unwrap();
112-
sleep(Duration::from_secs(100000)).await;
118+
token_cloned.cancelled().await;
113119
});
114120
let route_provider = runtime.block_on(async { rx.await.unwrap() });
115121
route_providers.push((
@@ -118,10 +124,11 @@ fn setup_route_providers(
118124
));
119125
// Setup dynamic latency-based route provider
120126
let (tx, rx) = oneshot::channel();
127+
let token_cloned = cancellation_token.clone();
121128
runtime.spawn(async move {
122129
let rp = setup_dynamic_route_provider(nodes_count, LatencyRoutingSnapshot::new()).await;
123130
tx.send(rp).unwrap();
124-
sleep(Duration::from_secs(100000)).await;
131+
token_cloned.cancelled().await;
125132
});
126133
let route_provider = runtime.block_on(async { rx.await.unwrap() });
127134
route_providers.push((

0 commit comments

Comments
 (0)