From 90009245904257ee3d7fe0b44eef47aae333b589 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 25 Feb 2022 18:06:07 -0500 Subject: [PATCH] Mostly completed the bandwidth test --- Cargo.toml | 1 + src/nettest.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 67495ba..a146381 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ path = "src/nettest.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.19" clap = { version = "3.1.2", features = ["derive"] } diff --git a/src/nettest.rs b/src/nettest.rs index 9acbd4b..930e2a8 100644 --- a/src/nettest.rs +++ b/src/nettest.rs @@ -1,5 +1,6 @@ +use chrono::prelude::*; use clap::{Parser, Subcommand}; -use std::process; +use std::{fs,process}; #[derive(Parser)] #[clap(name = "Bit Goblin Network Tester", author, version, about = "Network testing app.", long_about = None)] @@ -16,6 +17,14 @@ enum Commands { #[clap(default_value_t = String::from("8.8.8.8"))] host: String }, + + // bandwidth test subcommand + Bandwidth { + #[clap(default_value_t = String::from("https://www.bitgoblin.tech/hardware-tests/export-01.mp4"))] + download: String, + #[clap(default_value_t = String::from("./tempfile"))] + output: String, + }, } fn main() { @@ -23,7 +32,8 @@ fn main() { // map subcommands back to the main command match &cli.command { - Commands::Ping { host } => ping_host(host) + Commands::Ping { host } => ping_host(host), + Commands::Bandwidth { download, output } => bandwidth_test(download, output) } } @@ -44,3 +54,25 @@ fn ping_host(host: &str) { // print out the ping results from stdout println!("{}", String::from_utf8_lossy(&output.stdout)); } + +// timed file copy test to guage bandwidth speeds +fn bandwidth_test(download: &str, output: &str) { + println!("Testing network bandwidth by downloading {}", download, output); + + // get start time so we can track how long it takes to complete + let start_time = Utc::now(); + + // do the download + + // get finish time + let finish_time = Utc::now(); + // compute time to complete + let comp_time = finish_time - start_time; + println!("{}", comp_time.num_milliseconds()); + + // clean up the test file + match fs::remove_file(output) { + Ok(()) => println!("Cleaning up..."), + Err(e) => println!("There was a problem during cleanup - {}", e), + } +}