Skip to content

Commit

Permalink
introduce --download-only and --upload-only flags (#150)
Browse files Browse the repository at this point in the history
* introduce --download-only and --upload-only flags

* update simple speedtest example

* fix clippy issues

* make upload-only and download-only exclusive
  • Loading branch information
code-inflation authored Dec 1, 2024
1 parent ee35cf2 commit d2e4254
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Options:
Force usage of IPv6
-d, --disable-dynamic-max-payload-size
Disables dynamically skipping tests with larger payload sizes if the tests for the previous payload size took longer than 5 seconds
--download-only
Test download speed only
--upload-only
Test upload speed only
-h, --help
Print help
-V, --version
Expand Down
2 changes: 2 additions & 0 deletions examples/simple_speedtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main() {
ipv4: false, // don't force ipv4 usage
ipv6: false, // don't force ipv6 usage
verbose: false,
upload_only: false,
download_only: false,
nr_tests: 5,
nr_latency_tests: 20,
max_payload_size: PayloadSize::M10,
Expand Down
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ pub struct SpeedTestCLIOptions {
/// size took longer than 5 seconds
#[arg(short, long)]
pub disable_dynamic_max_payload_size: bool,

/// Test download speed only
#[arg(long, conflicts_with = "upload_only")]
pub download_only: bool,

/// Test upload speed only
#[arg(long, conflicts_with = "download_only")]
pub upload_only: bool,
}

impl SpeedTestCLIOptions {
/// Returns whether download tests should be performed
pub fn should_download(&self) -> bool {
self.download_only || !self.upload_only
}

/// Returns whether upload tests should be performed
pub fn should_upload(&self) -> bool {
self.upload_only || !self.download_only
}
}

fn parse_payload_size(input_string: &str) -> Result<PayloadSize, String> {
Expand Down
46 changes: 27 additions & 19 deletions src/speedtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,33 @@ pub fn speed_test(client: Client, options: SpeedTestCLIOptions) -> Vec<Measureme
println!("{metadata}");
}
run_latency_test(&client, options.nr_latency_tests, options.output_format);
let payload_sizes = PayloadSize::sizes_from_max(options.max_payload_size);
let mut measurements = run_tests(
&client,
test_download,
TestType::Download,
payload_sizes.clone(),
options.nr_tests,
options.output_format,
options.disable_dynamic_max_payload_size,
);
measurements.extend(run_tests(
&client,
test_upload,
TestType::Upload,
payload_sizes.clone(),
options.nr_tests,
options.output_format,
options.disable_dynamic_max_payload_size,
));
let payload_sizes = PayloadSize::sizes_from_max(options.max_payload_size.clone());
let mut measurements = Vec::new();

if options.should_download() {
measurements.extend(run_tests(
&client,
test_download,
TestType::Download,
payload_sizes.clone(),
options.nr_tests,
options.output_format,
options.disable_dynamic_max_payload_size,
));
}

if options.should_upload() {
measurements.extend(run_tests(
&client,
test_upload,
TestType::Upload,
payload_sizes.clone(),
options.nr_tests,
options.output_format,
options.disable_dynamic_max_payload_size,
));
}

log_measurements(
&measurements,
payload_sizes,
Expand Down

0 comments on commit d2e4254

Please sign in to comment.