forked from spacejam/sled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_quiescent.rs
70 lines (60 loc) · 2.06 KB
/
test_quiescent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![cfg(all(target_os = "linux", not(miri)))]
mod common;
use std::time::{Duration, Instant};
use common::cleanup;
#[test]
fn quiescent_cpu_time() {
const DB_DIR: &str = "sleeper";
cleanup(DB_DIR);
fn run() {
let start = Instant::now();
let db = sled::open(DB_DIR).unwrap();
std::thread::sleep(Duration::from_secs(10));
drop(db);
let end = Instant::now();
let (user_cpu_time, system_cpu_time) = unsafe {
let mut resource_usage: libc::rusage = std::mem::zeroed();
let return_value = libc::getrusage(
libc::RUSAGE_SELF,
(&mut resource_usage) as *mut libc::rusage,
);
if return_value != 0 {
panic!("error {} from getrusage()", *libc::__errno_location());
}
(resource_usage.ru_utime, resource_usage.ru_stime)
};
let user_cpu_seconds =
user_cpu_time.tv_sec as f64 + user_cpu_time.tv_usec as f64 * 1e-6;
let system_cpu_seconds = system_cpu_time.tv_sec as f64
+ system_cpu_time.tv_usec as f64 * 1e-6;
let real_time_elapsed = end.duration_since(start);
if user_cpu_seconds + system_cpu_seconds > 1.0 {
panic!(
"Database used too much CPU during a quiescent workload. User: {}s, system: {}s (wall clock: {}s)",
user_cpu_seconds,
system_cpu_seconds,
real_time_elapsed.as_secs_f64(),
);
}
}
let child = unsafe { libc::fork() };
if child == 0 {
common::setup_logger();
if let Err(e) = std::thread::spawn(run).join() {
println!("test failed: {:?}", e);
std::process::exit(15);
} else {
std::process::exit(0);
}
} else {
let mut status = 0;
unsafe {
libc::waitpid(child, &mut status as *mut libc::c_int, 0);
}
if status != 0 {
cleanup(DB_DIR);
panic!("child exited abnormally");
}
}
cleanup(DB_DIR);
}