Skip to content

Commit 9430a5d

Browse files
author
Sudan Landge
committed
test(metrics): add basic unit test for vhost-user
Add basic test to check shared inc and store metrics. Signed-off-by: Sudan Landge <[email protected]>
1 parent 3891f1c commit 9430a5d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/vmm/src/devices/virtio/vhost_user_metrics.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,47 @@ pub struct VhostUserDeviceMetrics {
140140
// Vhost-user activate time in microseconds.
141141
pub activate_time_us: SharedStoreMetric,
142142
}
143+
144+
#[cfg(test)]
145+
pub mod tests {
146+
use super::*;
147+
use crate::logger::{IncMetric, StoreMetric};
148+
149+
// vhost-user metrics has both SharedIncMetrics and SharedStoreMetrics
150+
// In this test we try to test one field for each type by creating a
151+
// dummy vhost_user_block metric named `vhost_user_block_drvN`.
152+
// There is no specific reason to storing the measured time taken vs a
153+
// random number in `init_time_us`.
154+
// We add an additional test to confirm that `vhost_user_metrics::METRICS`
155+
// actually has an entry for `vhost_user_block_drvN` and compare it.
156+
// We chose serde_json to compare because that seemed easiest to compare
157+
// the entire struct format and serialization of VhostUserDeviceMetrics.
158+
#[test]
159+
fn test_vhost_user_basic_metrics() {
160+
let vhost_user_dev_name: String = String::from("vhost_user_block_drvN");
161+
let start_time = utils::time::get_time_us(utils::time::ClockType::Monotonic);
162+
let vhost_user_metrics: Arc<VhostUserDeviceMetrics> =
163+
VhostUserMetricsPerDevice::alloc(vhost_user_dev_name.clone());
164+
let delta_us = utils::time::get_time_us(utils::time::ClockType::Monotonic) - start_time;
165+
vhost_user_metrics.activate_fails.inc();
166+
assert_eq!(vhost_user_metrics.activate_fails.count(), 1);
167+
168+
vhost_user_metrics.init_time_us.store(delta_us);
169+
assert_eq!(vhost_user_metrics.init_time_us.fetch(), delta_us);
170+
171+
// fill another local variable with the same data and use it to compare with the METRICS
172+
// entry
173+
let vhost_user_metrics_backup: VhostUserDeviceMetrics = VhostUserDeviceMetrics::default();
174+
vhost_user_metrics_backup.activate_fails.inc();
175+
vhost_user_metrics_backup.init_time_us.store(delta_us);
176+
177+
// serializing METRICS also flushes the SharedIncMetric data so we have to use _backup
178+
// variable for comparison.
179+
let vhost_user_metrics_global: String =
180+
serde_json::to_string(&METRICS.read().unwrap().metrics.get(&vhost_user_dev_name))
181+
.unwrap();
182+
let vhost_user_metrics_local: String =
183+
serde_json::to_string(&vhost_user_metrics_backup).unwrap();
184+
assert_eq!(vhost_user_metrics_local, vhost_user_metrics_global);
185+
}
186+
}

0 commit comments

Comments
 (0)