Skip to content

Commit 3718ef1

Browse files
committed
WIP add test to measure initial connection speed without relay.
1 parent f3fd988 commit 3718ef1

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

iroh/src/endpoint.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ mod tests {
22502250
};
22512251

22522252
use iroh_base::{NodeAddr, NodeId, SecretKey};
2253-
use n0_future::{task::AbortOnDropHandle, StreamExt};
2253+
use n0_future::{stream, task::AbortOnDropHandle, BufferedStreamExt, StreamExt};
22542254
use n0_snafu::{Error, Result, ResultExt};
22552255
use n0_watcher::Watcher;
22562256
use quinn::ConnectionError;
@@ -2260,7 +2260,9 @@ mod tests {
22602260

22612261
use super::Endpoint;
22622262
use crate::{
2263+
discovery::static_provider::StaticProvider,
22632264
endpoint::{ConnectOptions, Connection, ConnectionType, RemoteInfo},
2265+
protocol::{AcceptError, ProtocolHandler, Router},
22642266
test_utils::{run_relay_server, run_relay_server_with},
22652267
RelayMode,
22662268
};
@@ -3190,4 +3192,83 @@ mod tests {
31903192

31913193
Ok(())
31923194
}
3195+
3196+
/// Test that peers are properly restored
3197+
#[tokio::test]
3198+
// #[traced_test]
3199+
async fn connect_multi() -> Result {
3200+
let n = 32;
3201+
3202+
const NOOP_ALPN: &[u8] = b"noop";
3203+
3204+
#[derive(Debug, Clone)]
3205+
struct Noop;
3206+
3207+
impl ProtocolHandler for Noop {
3208+
async fn accept(&self, connection: Connection) -> Result<(), AcceptError> {
3209+
connection.closed().await;
3210+
Ok(())
3211+
}
3212+
}
3213+
3214+
async fn noop_server() -> Result<(Router, NodeAddr)> {
3215+
let endpoint = Endpoint::builder()
3216+
.relay_mode(RelayMode::Disabled)
3217+
.bind()
3218+
.await
3219+
.e()?;
3220+
let addr = endpoint.node_addr().initialized().await.e()?;
3221+
let router = Router::builder(endpoint).accept(NOOP_ALPN, Noop).spawn();
3222+
Ok((router, addr))
3223+
}
3224+
3225+
let routers = stream::iter(0..n)
3226+
.map(|_| noop_server())
3227+
.buffered_unordered(32)
3228+
.collect::<Vec<_>>()
3229+
.await
3230+
.into_iter()
3231+
.collect::<Result<Vec<_>, _>>()
3232+
.e()?;
3233+
3234+
let addrs = routers
3235+
.iter()
3236+
.map(|(_, addr)| addr.clone())
3237+
.collect::<Vec<_>>();
3238+
let ids = addrs.iter().map(|addr| addr.node_id).collect::<Vec<_>>();
3239+
let discovery = StaticProvider::from_node_info(addrs);
3240+
let endpoint = Endpoint::builder()
3241+
.relay_mode(RelayMode::Disabled)
3242+
.discovery(discovery)
3243+
.bind()
3244+
.await
3245+
.e()?;
3246+
// wait for the endpoint to be initialized. This should not be needed,
3247+
// but we don't want to measure endpoint init time but connection time
3248+
// from a fully initialized endpoint.
3249+
endpoint.node_addr().initialized().await.e()?;
3250+
let t0 = Instant::now();
3251+
for id in &ids {
3252+
let conn = endpoint.connect(*id, NOOP_ALPN).await?;
3253+
conn.close(0u32.into(), b"done");
3254+
}
3255+
let dt0 = t0.elapsed();
3256+
let t1 = Instant::now();
3257+
for id in &ids {
3258+
let conn = endpoint.connect(*id, NOOP_ALPN).await?;
3259+
conn.close(0u32.into(), b"done");
3260+
}
3261+
let dt1 = t1.elapsed();
3262+
println!(
3263+
"Round 0: {}s, {}s per connection",
3264+
dt0.as_secs_f64(),
3265+
dt0.as_secs_f64() / (n as f64)
3266+
);
3267+
println!(
3268+
"Round 1: {}s, {}s per connection",
3269+
dt1.as_secs_f64(),
3270+
dt1.as_secs_f64() / (n as f64)
3271+
);
3272+
Ok(())
3273+
}
31933274
}

0 commit comments

Comments
 (0)