Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bench): Add --metrics option printing iroh-net library metrics #2668

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iroh-net/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = "1.0.22"
bytes = "1"
hdrhistogram = { version = "7.2", default-features = false }
iroh-net = { path = "..", features = ["test-utils"] }
iroh-metrics = { path = "../../iroh-metrics" }
rcgen = "0.11.1"
rustls = { version = "0.21.0", default-features = false, features = ["quic"] }
clap = { version = "4", features = ["derive"] }
Expand Down
55 changes: 55 additions & 0 deletions iroh-net/bench/src/bin/bulk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use anyhow::Result;
use clap::Parser;

Expand Down Expand Up @@ -30,6 +32,19 @@ fn main() {
}

pub fn run_iroh(opt: Opt) -> Result<()> {
if opt.metrics {
// enable recording metrics
iroh_metrics::core::Core::try_init(|reg, metrics| {
use iroh_metrics::core::Metric;
metrics.insert(iroh_net::metrics::MagicsockMetrics::new(reg));
metrics.insert(iroh_net::metrics::NetcheckMetrics::new(reg));
metrics.insert(iroh_net::metrics::PortmapMetrics::new(reg));
if opt.with_relay {
metrics.insert(iroh_net::metrics::RelayMetrics::new(reg));
}
})?;
}

let server_span = tracing::error_span!("server");
let runtime = rt();

Expand Down Expand Up @@ -78,6 +93,30 @@ pub fn run_iroh(opt: Opt) -> Result<()> {
}
}

if opt.metrics {
// print metrics
let core =
iroh_metrics::core::Core::get().ok_or_else(|| anyhow::anyhow!("Missing metrics"))?;
println!("\nMetrics:");
collect_and_print(
"MagicsockMetrics",
core.get_collector::<iroh_net::metrics::MagicsockMetrics>(),
);
collect_and_print(
"NetcheckMetrics",
core.get_collector::<iroh_net::metrics::NetcheckMetrics>(),
);
collect_and_print(
"PortmapMetrics",
core.get_collector::<iroh_net::metrics::PortmapMetrics>(),
);
// if None, (this is the case if opt.with_relay is false), then this is skipped internally:
collect_and_print(
"RelayMetrics",
core.get_collector::<iroh_net::metrics::RelayMetrics>(),
);
}

server_thread.join().expect("server thread");

Ok(())
Expand Down Expand Up @@ -130,3 +169,19 @@ pub fn run_quinn(opt: Opt) -> Result<()> {
pub fn run_s2n(_opt: s2n::Opt) -> Result<()> {
unimplemented!()
}

fn collect_and_print(
category: &'static str,
metrics: Option<&impl iroh_metrics::struct_iterable::Iterable>,
) {
let Some(metrics) = metrics else {
return;
};
let mut map = BTreeMap::new();
for (name, counter) in metrics.iter() {
if let Some(counter) = counter.downcast_ref::<iroh_metrics::core::Counter>() {
map.insert(name.to_string(), counter.get());
}
}
println!("{category}: {map:#?}");
}
6 changes: 6 additions & 0 deletions iroh-net/bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ pub struct Opt {
/// Show connection stats the at the end of the benchmark
#[clap(long = "stats")]
pub stats: bool,
/// Show iroh-net library counter metrics at the end of the benchmark
///
/// These metrics are process-wide, so contain metrics for
/// clients and the server all summed up.
#[clap(long)]
pub metrics: bool,
/// Whether to use the unordered read API
#[clap(long = "unordered")]
pub read_unordered: bool,
Expand Down
1 change: 1 addition & 0 deletions iroh-net/src/discovery/pkarr/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ mod tests {
use testresult::TestResult;

#[tokio::test]
#[ignore = "flaky"]
async fn dht_discovery_smoke() -> TestResult {
let _ = tracing_subscriber::fmt::try_init();
let ep = crate::Endpoint::builder().bind(0).await?;
Expand Down
Loading