Skip to content

Commit

Permalink
Add test for disk I/O usage
Browse files Browse the repository at this point in the history
  • Loading branch information
samin-cf committed Nov 4, 2024
1 parent fd52c86 commit fc0bb2f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ procfs = "0.17.0"
[dev-dependencies]
serde_json = "1.0" # Used in documentation tests.
bstr = "1.9.0"
tempfile = "3.9"

[[example]]
name = "simple"
Expand Down
62 changes: 62 additions & 0 deletions tests/disk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[test]
#[cfg(all(feature = "system", feature = "disk"))]
fn test_disks() {
if sysinfo::IS_SUPPORTED_SYSTEM {
let s = sysinfo::System::new_all();
// If we don't have any physical core present, it's very likely that we're inside a VM...
if s.physical_core_count().unwrap_or_default() > 0 {
let mut disks = sysinfo::Disks::new();
assert!(disks.list().is_empty());
disks.refresh_list();
assert!(!disks.list().is_empty());
}
}
}

#[test]
#[cfg(feature = "disk")]
fn test_disks_usage() {
use std::io::Write;
use tempfile::NamedTempFile;

if !sysinfo::IS_SUPPORTED_SYSTEM {
return;
}

let mut disks = sysinfo::Disks::new_with_refreshed_list();

let mut file = NamedTempFile::new().unwrap();

// Writes 10mb worth of data to the temp file
let size = 10 * 1024 * 1024;
let data = vec![0u8; size];
file.write_all(&data).unwrap();
file.flush().unwrap();

// Read data back from the temp file
let _ = std::fs::read(file.path()).unwrap();

// Wait a bit before refreshing the disk usage stats
std::thread::sleep(std::time::Duration::from_millis(500));
disks.refresh();

// Depending on the OS and how disks are configured, the disk usage may be the exact same
// across multiple disks. To account for this, collect the disk usages and dedup
let mut disk_usages = disks.list().iter().map(|d| d.usage()).collect::<Vec<_>>();
disk_usages.dedup();

let mut read_bytes = 0;
let mut written_bytes = 0;
for disk_usage in disk_usages {
read_bytes += disk_usage.read_bytes;
written_bytes += disk_usage.written_bytes;
}

// We expect written_bytes to have increased by about 10mb but due to unknown reasons, it is sometimes
// less than 10mb. Settle in the middle with at least 5mb increase
assert!(written_bytes >= 5 * 1024 * 1024);
// Validating read_bytes is not as straightforward due to caching. We'll settle for any amount of increase.
assert!(read_bytes > 0);
}
16 changes: 0 additions & 16 deletions tests/disk_list.rs

This file was deleted.

0 comments on commit fc0bb2f

Please sign in to comment.