Skip to content

Commit 2012909

Browse files
committed
Add module for benchmarking getloadavg
1 parent 3e2dc34 commit 2012909

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎.idea/rust-stuff.iml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.lock‎

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"base64_jdk",
66
"base64_jni",
77
"base64_manual_bench",
8+
"getloadavg_bench",
89
]
910

1011
[profile.dev]

‎getloadavg_bench/Cargo.toml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "getloadavg_bench"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
libc = "0.2.135"

‎getloadavg_bench/src/main.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std;
2+
use std::time::SystemTime;
3+
4+
use libc;
5+
6+
/*
7+
$ cargo run -r --bin getloadavg_bench && uptime
8+
Finished release [optimized] target(s) in 0.05s
9+
Running `target/release/getloadavg_bench`
10+
LA: 1.42 1.18 0.92
11+
getloadavg 469
12+
LA: 1.38 1.17 0.92
13+
21:07:56 up 18 days, 11:49, 1 user, load average: 1.38, 1.17, 0.92
14+
*/
15+
fn main() {
16+
let mut output = vec![0.0; 3];
17+
get_and_print();
18+
run_benchmark(output.as_mut_ptr());
19+
get_and_print();
20+
}
21+
22+
#[inline]
23+
fn getloadavg(output: *mut libc::c_double) {
24+
unsafe {
25+
let r = libc::getloadavg(output, 3);
26+
assert_eq!(r, 3);
27+
}
28+
}
29+
30+
fn get_and_print() {
31+
let mut output = vec![0.0; 3];
32+
getloadavg(output.as_mut_ptr());
33+
std::println!("LA: {:.2} {:.2} {:.2}", output[0], output[1], output[2]);
34+
}
35+
36+
fn run_benchmark(output: *mut libc::c_double) {
37+
static N: usize = 1_000_000;
38+
39+
let warmup_start = SystemTime::now();
40+
while SystemTime::now().duration_since(warmup_start).unwrap().as_secs() < 3 {
41+
for _ in 0..10_000 {
42+
getloadavg(output);
43+
}
44+
}
45+
46+
let start = SystemTime::now();
47+
for _ in 0..N {
48+
getloadavg(output);
49+
}
50+
let end = SystemTime::now();
51+
52+
let duration = end.duration_since(start).unwrap();
53+
let nanos = duration.as_nanos();
54+
55+
std::println!("{} {:>8}", "getloadavg", nanos / (N as u128));
56+
}

0 commit comments

Comments
 (0)