Skip to content

Commit

Permalink
Update experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Jan 1, 2024
1 parent 26de2d5 commit 46e5ad7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 0 additions & 1 deletion experiments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ publish = false

[dependencies]
spin_sleep = { path = ".." }
num_cpus = "1.13.1"
rand = "0.8.5"
11 changes: 6 additions & 5 deletions experiments/src/bin/native_sleep_accuracy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Call OS native sleep for **1ns** and see how long it actually takes.
use std::time::{Duration, Instant};

fn main() {
Expand All @@ -6,11 +7,13 @@ fn main() {
std::process::exit(1);
}

const ITS: u32 = 1000;

let mut best = Duration::from_secs(100);
let mut sum = Duration::from_secs(0);
let mut worst = Duration::from_secs(0);

for _ in 0..100 {
for _ in 0..ITS {
let before = Instant::now();
spin_sleep::native_sleep(Duration::new(0, 1));
let elapsed = before.elapsed();
Expand All @@ -24,9 +27,7 @@ fn main() {
}

println!(
"average: {:?}, best : {:?}, worst: {:?}",
Duration::from_nanos(u64::try_from(sum.as_nanos() / 100).unwrap()),
best,
worst,
"average: {:?}, best : {best:?}, worst: {worst:?}",
sum / ITS
);
}
5 changes: 3 additions & 2 deletions experiments/src/bin/spin_strategy_latency.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Measure `SpinStrategy` latencies and spin counts across various wait durations _5ms, 900µs, 5µs, 100ns_.
use std::time::{Duration, Instant};

fn main() {
Expand All @@ -7,7 +8,7 @@ fn main() {
}

if std::env::args().nth(1).as_deref() == Some("load") {
let cpus = num_cpus::get();
let cpus = std::thread::available_parallelism().unwrap().into();
eprintln!("Simulating {cpus} thread load");
for _ in 0..cpus {
std::thread::spawn(|| {
Expand Down Expand Up @@ -57,7 +58,7 @@ fn main() {
"{duration: <6?} {: <13} avg-spins: {:<8} avg-actual: {:?}",
format!("{strategy:?}"),
spins / 100,
Duration::from_nanos(u64::try_from(sum.as_nanos() / 100).unwrap()),
sum / 100,
);
}
}
Expand Down

0 comments on commit 46e5ad7

Please sign in to comment.