hardware-tests/src/tests/disk.rs

57 lines
1.7 KiB
Rust
Raw Normal View History

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
let size_gigs: u32 = (*size as u32 * 1024).into();
// run the dd command with a block size of 1 MB
let output = Command::new("dd")
.arg("bs=1M")
.arg(format!("count={}", size_gigs))
.arg("if=/dev/zero")
.arg(format!("of={}", tempfile))
.output()
.expect("Failed to execute command");
// check that the command succeeded
assert!(output.status.success());
// for whatever reason, `dd` output ends up in stderr
println!("{}", String::from_utf8_lossy(&output.stderr));
// remove the test file
match fs::remove_file(tempfile) {
Ok(()) => println!("Cleaning up..."),
Err(e) => println!("There was a problem during cleanup - {}", e),
}
}