Skip to content

Commit

Permalink
ping in batches of 100 and display results staggered by response time
Browse files Browse the repository at this point in the history
  • Loading branch information
Firaenix committed Nov 4, 2024
1 parent 1d8f902 commit 0278112
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
11 changes: 10 additions & 1 deletion src/display/ping_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,31 @@ pub fn display_success_ping(
"│ 64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms",
jobres.ip_address, i, time
));
std::thread::sleep(std::time::Duration::from_millis(500));
std::thread::sleep(std::time::Duration::from_millis(time as u64));
}

pb.println("│"); // Empty line for spacing

// Construct and print statistics line
pb.println(format!("│ --- {endpoint} ping statistics ---"));

std::thread::sleep(std::time::Duration::from_millis(250));

// Print packet loss information
pb.println(format!(
"│ {} packets transmitted, {} packets received, {:.1}% packet loss",
jobres.packets_sent,
jobres.packets_recv,
jobres.packet_loss * 100.0
));
std::thread::sleep(std::time::Duration::from_millis(250));

// Print round-trip statistics
pb.println(format!(
"│ round-trip min/avg/max/stddev = {:.3}/{:.3}/{:.3}/{:.3} ms",
jobres.min, jobres.avg, jobres.max, jobres.std_dev
));
std::thread::sleep(std::time::Duration::from_millis(250));

print_footer(pb, width);
}
Expand Down Expand Up @@ -84,6 +88,8 @@ pub fn display_failed_ping(
// --- asdasdasd.com ping statistics ---
pb.println(format!("│ --- {} ping statistics ---", jobres.endpoint));

std::thread::sleep(std::time::Duration::from_millis(250));

// 5 packets transmitted, 0 packets received, 100.0% packet loss
let error_string = if let Some(result) = &jobres.result {
format!(
Expand All @@ -98,7 +104,10 @@ pub fn display_failed_ping(
attempts
)
};
std::thread::sleep(std::time::Duration::from_millis(250));

pb.println(format!("{}", error_string.color(Color::Red)));
std::thread::sleep(std::time::Duration::from_millis(250));

print_footer(pb, width);
}
Expand Down
113 changes: 59 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use models::{
Client,
};
use options::Opts;
use progenitor::progenitor_client::ResponseValue;
use rand::seq::SliceRandom;
use reqwest::header::{HeaderMap, HeaderValue};
use std::{
Expand Down Expand Up @@ -70,65 +71,69 @@ async fn main() -> eyre::Result<()> {

pb.set_style(spinner_style);

let mut set = JoinSet::new();

for region in &APP_CONFIG.regions {
for _ in 0..APP_CONFIG.attempts {
let pb = pb.clone();
set.spawn(async move {
let (country_code, continent_code) = match region {
options::EarthRegion::Country(c) => {
(Some(c.to_country().alpha2().to_string()), None)
}
options::EarthRegion::Continent(con) => (
None,
Some(
match con {
keshvar::Continent::Africa => "AF",
keshvar::Continent::Antarctica => "AN",
keshvar::Continent::Asia => "AS",
keshvar::Continent::Australia => "OC",
keshvar::Continent::Europe => "EU",
keshvar::Continent::NorthAmerica => "NA",
keshvar::Continent::SouthAmerica => "SA",
}
.to_string(),
for chunk in (0..APP_CONFIG.attempts).collect::<Vec<_>>().chunks(100) {
let mut chunk_set = JoinSet::new();

for _ in chunk {
let pb = pb.clone();
let region = region.clone();
let endpoint = endpoint.clone();

chunk_set.spawn(async move {
let (country_code, continent_code) = match region {
options::EarthRegion::Country(c) => {
(Some(c.to_country().alpha2().to_string()), None)
}
options::EarthRegion::Continent(con) => (
None,
Some(
match con {
keshvar::Continent::Africa => "AF",
keshvar::Continent::Antarctica => "AN",
keshvar::Continent::Asia => "AS",
keshvar::Continent::Australia => "OC",
keshvar::Continent::Europe => "EU",
keshvar::Continent::NorthAmerica => "NA",
keshvar::Continent::SouthAmerica => "SA",
}
.to_string(),
),
),
),
_ => (None, None),
};

debug!(?country_code, "Sending job to country");

let result = API_CLIENT
.perform_icmp(&PerformIcmpBody {
configuration: Some(PerformIcmpBodyConfiguration {
payload_size: Some(56.0),
timeout_millis: None,
attempts: Some(APP_CONFIG.count as f64),
}),
country_code,
continent_code,
hostnames: vec![endpoint.to_string()],
isp_regex: None,
})
.await
.context("Failed to send job");

pb.inc(1);

result
});
_ => (None, None),
};

debug!(?country_code, "Sending job to country");

let result = API_CLIENT
.perform_icmp(&PerformIcmpBody {
configuration: Some(PerformIcmpBodyConfiguration {
payload_size: Some(56.0),
timeout_millis: None,
attempts: Some(APP_CONFIG.count as f64),
}),
country_code,
continent_code,
hostnames: vec![endpoint.to_string()],
isp_regex: None,
})
.await
.context("Failed to send job");

pb.inc(1);

result
});
}

while let Some(res) = chunk_set.join_next().await {
let out = res??;
tracing::debug!("Response {:?}", out);
display_job(&pb, &APP_CONFIG, &out).await;
}
}
}

while let Some(res) = set.join_next().await {
let out = res??;
tracing::debug!("Response {:?}", out);

display_job(&pb, &APP_CONFIG, &out).await;
}

pb.finish();
Ok(())
}
Expand Down

0 comments on commit 0278112

Please sign in to comment.