Skip to content

Commit b58e29a

Browse files
fix: show measurements incrementally and remove status code from display
1 parent 356d7ef commit b58e29a

3 files changed

Lines changed: 43 additions & 38 deletions

File tree

examples/download_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cfspeedtest::OutputFormat;
44
fn main() {
55
println!("Testing download speed with 10MB of payload");
66

7-
let download_speed = test_download(
7+
let (download_speed, _) = test_download(
88
&reqwest::blocking::Client::new(),
99
10_000_000,
1010
OutputFormat::None, // don't write to stdout while running the test

src/progress.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Progress {
99
let bar = ProgressBar::new(max as u64);
1010
bar.set_style(
1111
ProgressStyle::default_bar()
12-
.template("{prefix:<15} [{bar:30}]")
12+
.template("{prefix:<15} [{bar:30}] {msg}")
1313
.unwrap()
1414
.progress_chars("=-"),
1515
);
@@ -24,4 +24,8 @@ impl Progress {
2424
pub fn finish(&self) {
2525
self.bar.finish();
2626
}
27+
28+
pub fn set_message(&self, msg: impl Into<std::borrow::Cow<'static, str>>) {
29+
self.bar.set_message(msg);
30+
}
2731
}

src/speedtest.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::OutputFormat;
77
use crate::SpeedTestCLIOptions;
88
use log;
99
use regex::Regex;
10-
use reqwest::{blocking::Client, StatusCode};
10+
use reqwest::blocking::Client;
1111
use serde::Serialize;
1212
use std::{
1313
fmt::Display,
@@ -233,7 +233,7 @@ const TIME_THRESHOLD: Duration = Duration::from_secs(5);
233233

234234
pub fn run_tests(
235235
client: &Client,
236-
test_fn: fn(&Client, usize, OutputFormat) -> f64,
236+
test_fn: fn(&Client, usize, OutputFormat) -> (f64, String),
237237
test_type: TestType,
238238
payload_sizes: Vec<usize>,
239239
nr_tests: u32,
@@ -255,10 +255,11 @@ pub fn run_tests(
255255
};
256256

257257
for i in 0..nr_tests {
258+
let (mbit, message) = test_fn(client, payload_size, output_format);
258259
if let Some(ref pb) = progress {
259-
pb.set_position(i);
260+
pb.set_position(i + 1);
261+
pb.set_message(message);
260262
}
261-
let mbit = test_fn(client, payload_size, output_format);
262263
measurements.push(Measurement {
263264
test_type,
264265
payload_size,
@@ -267,7 +268,6 @@ pub fn run_tests(
267268
}
268269

269270
if let Some(pb) = progress {
270-
pb.set_position(nr_tests);
271271
pb.finish();
272272
println!()
273273
}
@@ -283,62 +283,63 @@ pub fn run_tests(
283283
measurements
284284
}
285285

286-
pub fn test_upload(client: &Client, payload_size_bytes: usize, output_format: OutputFormat) -> f64 {
286+
pub fn test_upload(
287+
client: &Client,
288+
payload_size_bytes: usize,
289+
output_format: OutputFormat,
290+
) -> (f64, String) {
287291
let url = &format!("{BASE_URL}/{UPLOAD_URL}");
288292
let payload: Vec<u8> = vec![1; payload_size_bytes];
289293
let req_builder = client.post(url).body(payload);
290-
let (mut response, status_code, mbits, duration) = {
294+
let (mut response, mbits, duration) = {
291295
let start = Instant::now();
292296
let response = req_builder.send().expect("failed to get response");
293-
let status_code = response.status();
294297
let duration = start.elapsed();
295298
let mbits = (payload_size_bytes as f64 * 8.0 / 1_000_000.0) / duration.as_secs_f64();
296-
(response, status_code, mbits, duration)
299+
(response, mbits, duration)
297300
};
298301
// Drain response after timing so we don't skew upload measurement.
299302
let _ = std::io::copy(&mut response, &mut std::io::sink());
300-
if output_format == OutputFormat::StdOut {
301-
print_current_speed(mbits, duration, status_code, payload_size_bytes);
302-
}
303-
mbits
303+
let message = if output_format == OutputFormat::StdOut {
304+
format!(
305+
" {:>6.2} mbit/s | {:>5} in {:>4}ms",
306+
mbits,
307+
format_bytes(payload_size_bytes),
308+
duration.as_millis()
309+
)
310+
} else {
311+
String::new()
312+
};
313+
(mbits, message)
304314
}
305315

306316
pub fn test_download(
307317
client: &Client,
308318
payload_size_bytes: usize,
309319
output_format: OutputFormat,
310-
) -> f64 {
320+
) -> (f64, String) {
311321
let url = &format!("{BASE_URL}/{DOWNLOAD_URL}{payload_size_bytes}");
312322
let req_builder = client.get(url);
313-
let (status_code, mbits, duration) = {
323+
let (mbits, duration) = {
314324
let start = Instant::now();
315325
let mut response = req_builder.send().expect("failed to get response");
316-
let status_code = response.status();
317326
// Stream the body to avoid buffering the full payload in memory.
318327
let _ = std::io::copy(&mut response, &mut std::io::sink());
319328
let duration = start.elapsed();
320329
let mbits = (payload_size_bytes as f64 * 8.0 / 1_000_000.0) / duration.as_secs_f64();
321-
(status_code, mbits, duration)
330+
(mbits, duration)
322331
};
323-
if output_format == OutputFormat::StdOut {
324-
print_current_speed(mbits, duration, status_code, payload_size_bytes);
325-
}
326-
mbits
327-
}
328-
329-
fn print_current_speed(
330-
mbits: f64,
331-
duration: Duration,
332-
status_code: StatusCode,
333-
payload_size_bytes: usize,
334-
) {
335-
print!(
336-
" {:>6.2} mbit/s | {:>5} in {:>4}ms -> status: {} ",
337-
mbits,
338-
format_bytes(payload_size_bytes),
339-
duration.as_millis(),
340-
status_code
341-
);
332+
let message = if output_format == OutputFormat::StdOut {
333+
format!(
334+
" {:>6.2} mbit/s | {:>5} in {:>4}ms",
335+
mbits,
336+
format_bytes(payload_size_bytes),
337+
duration.as_millis()
338+
)
339+
} else {
340+
String::new()
341+
};
342+
(mbits, message)
342343
}
343344

344345
pub fn fetch_metadata(client: &Client) -> Result<Metadata, reqwest::Error> {

0 commit comments

Comments
 (0)