From be549808b522e6a1c777c358af8ddc4544bb6414 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 5 Aug 2022 21:37:15 -0400 Subject: [PATCH] Added a disk benchmark test for sequential reads --- src/main.rs | 11 ++++++++++- src/tests/disk.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ff62a55..c55678f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,15 @@ struct Disk { #[derive(Subcommand)] enum DiskCommands { - #[clap(name = "write-test", about = "Write a large file to determine sequential disk speeds.")] + // disk read subcommand + #[clap(name = "read-test", about = "Read from /dev/zero to determine sequential disk read speeds.")] + ReadTest { + #[clap(short = 's', default_value_t = 15)] + size: u8, + }, + + // disk write subcommand + #[clap(name = "write-test", about = "Write a large file to determine sequential disk write speeds.")] WriteTest { #[clap(short = 't', default_value_t = String::from("/tmp/disk-test.tmp"))] tempfile: String, @@ -70,6 +78,7 @@ fn main() { // map subcommands back to the main command match &cli.command { Commands::Disk(args) => match &args.disk_commands { + DiskCommands::ReadTest { size } => tests::disk::disk_read_test(size), DiskCommands::WriteTest { tempfile, size } => tests::disk::disk_write_test(tempfile, size), } diff --git a/src/tests/disk.rs b/src/tests/disk.rs index 3521e8a..c9c3480 100644 --- a/src/tests/disk.rs +++ b/src/tests/disk.rs @@ -1,6 +1,33 @@ use std::fs; use std::process::Command; +// test disk read speeds by reading for /dev/zero and writing it to /dev/null +pub fn disk_read_test(size: &u8) { + // convert size in Gigabytes down to Megabytes + let size_gigs: u32 = (*size as u32 * 1024 * 8).into(); + + // run sync to clear out any disk caches prior to running + Command::new("sync"); + + // run the dd command + let output = Command::new("dd") + .arg("bs=128k") + .arg(format!("count={}", size_gigs)) + .arg("if=/dev/zero") + .arg("of=/dev/null") + .output() + .expect("Failed to execute command"); + + // check that the command succeeded + assert!(output.status.success()); + + // dd's output ends up in stderr + println!("{}", String::from_utf8_lossy(&output.stderr)); + + // run another sync to clear out the disk's cache + Command::new("sync"); +} + // test disk write speeds by continually writing zeroes to it pub fn disk_write_test(tempfile: &str, size: &u8) { // convert size in Gigabytes down to Megabytes