Skip to content

Commit 0278112

Browse files
committed
ping in batches of 100 and display results staggered by response time
1 parent 1d8f902 commit 0278112

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

src/display/ping_display.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,31 @@ pub fn display_success_ping(
3535
"│ 64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms",
3636
jobres.ip_address, i, time
3737
));
38-
std::thread::sleep(std::time::Duration::from_millis(500));
38+
std::thread::sleep(std::time::Duration::from_millis(time as u64));
3939
}
4040

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

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

46+
std::thread::sleep(std::time::Duration::from_millis(250));
47+
4648
// Print packet loss information
4749
pb.println(format!(
4850
"│ {} packets transmitted, {} packets received, {:.1}% packet loss",
4951
jobres.packets_sent,
5052
jobres.packets_recv,
5153
jobres.packet_loss * 100.0
5254
));
55+
std::thread::sleep(std::time::Duration::from_millis(250));
5356

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

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

91+
std::thread::sleep(std::time::Duration::from_millis(250));
92+
8793
// 5 packets transmitted, 0 packets received, 100.0% packet loss
8894
let error_string = if let Some(result) = &jobres.result {
8995
format!(
@@ -98,7 +104,10 @@ pub fn display_failed_ping(
98104
attempts
99105
)
100106
};
107+
std::thread::sleep(std::time::Duration::from_millis(250));
108+
101109
pb.println(format!("{}", error_string.color(Color::Red)));
110+
std::thread::sleep(std::time::Duration::from_millis(250));
102111

103112
print_footer(pb, width);
104113
}

src/main.rs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use models::{
1010
Client,
1111
};
1212
use options::Opts;
13+
use progenitor::progenitor_client::ResponseValue;
1314
use rand::seq::SliceRandom;
1415
use reqwest::header::{HeaderMap, HeaderValue};
1516
use std::{
@@ -70,65 +71,69 @@ async fn main() -> eyre::Result<()> {
7071

7172
pb.set_style(spinner_style);
7273

73-
let mut set = JoinSet::new();
74-
7574
for region in &APP_CONFIG.regions {
76-
for _ in 0..APP_CONFIG.attempts {
77-
let pb = pb.clone();
78-
set.spawn(async move {
79-
let (country_code, continent_code) = match region {
80-
options::EarthRegion::Country(c) => {
81-
(Some(c.to_country().alpha2().to_string()), None)
82-
}
83-
options::EarthRegion::Continent(con) => (
84-
None,
85-
Some(
86-
match con {
87-
keshvar::Continent::Africa => "AF",
88-
keshvar::Continent::Antarctica => "AN",
89-
keshvar::Continent::Asia => "AS",
90-
keshvar::Continent::Australia => "OC",
91-
keshvar::Continent::Europe => "EU",
92-
keshvar::Continent::NorthAmerica => "NA",
93-
keshvar::Continent::SouthAmerica => "SA",
94-
}
95-
.to_string(),
75+
for chunk in (0..APP_CONFIG.attempts).collect::<Vec<_>>().chunks(100) {
76+
let mut chunk_set = JoinSet::new();
77+
78+
for _ in chunk {
79+
let pb = pb.clone();
80+
let region = region.clone();
81+
let endpoint = endpoint.clone();
82+
83+
chunk_set.spawn(async move {
84+
let (country_code, continent_code) = match region {
85+
options::EarthRegion::Country(c) => {
86+
(Some(c.to_country().alpha2().to_string()), None)
87+
}
88+
options::EarthRegion::Continent(con) => (
89+
None,
90+
Some(
91+
match con {
92+
keshvar::Continent::Africa => "AF",
93+
keshvar::Continent::Antarctica => "AN",
94+
keshvar::Continent::Asia => "AS",
95+
keshvar::Continent::Australia => "OC",
96+
keshvar::Continent::Europe => "EU",
97+
keshvar::Continent::NorthAmerica => "NA",
98+
keshvar::Continent::SouthAmerica => "SA",
99+
}
100+
.to_string(),
101+
),
96102
),
97-
),
98-
_ => (None, None),
99-
};
100-
101-
debug!(?country_code, "Sending job to country");
102-
103-
let result = API_CLIENT
104-
.perform_icmp(&PerformIcmpBody {
105-
configuration: Some(PerformIcmpBodyConfiguration {
106-
payload_size: Some(56.0),
107-
timeout_millis: None,
108-
attempts: Some(APP_CONFIG.count as f64),
109-
}),
110-
country_code,
111-
continent_code,
112-
hostnames: vec![endpoint.to_string()],
113-
isp_regex: None,
114-
})
115-
.await
116-
.context("Failed to send job");
117-
118-
pb.inc(1);
119-
120-
result
121-
});
103+
_ => (None, None),
104+
};
105+
106+
debug!(?country_code, "Sending job to country");
107+
108+
let result = API_CLIENT
109+
.perform_icmp(&PerformIcmpBody {
110+
configuration: Some(PerformIcmpBodyConfiguration {
111+
payload_size: Some(56.0),
112+
timeout_millis: None,
113+
attempts: Some(APP_CONFIG.count as f64),
114+
}),
115+
country_code,
116+
continent_code,
117+
hostnames: vec![endpoint.to_string()],
118+
isp_regex: None,
119+
})
120+
.await
121+
.context("Failed to send job");
122+
123+
pb.inc(1);
124+
125+
result
126+
});
127+
}
128+
129+
while let Some(res) = chunk_set.join_next().await {
130+
let out = res??;
131+
tracing::debug!("Response {:?}", out);
132+
display_job(&pb, &APP_CONFIG, &out).await;
133+
}
122134
}
123135
}
124136

125-
while let Some(res) = set.join_next().await {
126-
let out = res??;
127-
tracing::debug!("Response {:?}", out);
128-
129-
display_job(&pb, &APP_CONFIG, &out).await;
130-
}
131-
132137
pb.finish();
133138
Ok(())
134139
}

0 commit comments

Comments
 (0)