From 6ba090e8283692bcffa01887106f5fcb21920b35 Mon Sep 17 00:00:00 2001 From: Jonathan Strong Date: Wed, 14 Feb 2018 18:25:11 -0500 Subject: [PATCH] adds an n-thread test + comparisons between sleep and yield_now --- examples/hist-interval.rs | 64 +++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/examples/hist-interval.rs b/examples/hist-interval.rs index 80164b4..10428cd 100644 --- a/examples/hist-interval.rs +++ b/examples/hist-interval.rs @@ -1,3 +1,5 @@ +#![feature(duration_from_micros)] +#![allow(unused)] extern crate logging; @@ -6,13 +8,59 @@ use std::thread; use logging::hist::{Entry, HistLog, nanos}; -const N_SECS: u64 = 60 * 60; +const N_SECS: u64 = 30; + +//const N_THREADS: usize = 48; + +macro_rules! n_threads { + ($n:expr) => { + const N_THREADS: usize = $n; + const NAME: &'static str = concat!("sleeptest_", stringify!($n)); + } +} + +n_threads!(96); fn main() { + ::std::process::exit(test_n()); +} + +fn test_n() -> i32 { let start = Instant::now(); - let mut a = HistLog::new("test", "a", Duration::from_millis(1000)); - let mut b = a.clone_with_tag("b"); - let mut c = b.clone_with_tag("c"); + let hist = HistLog::new(NAME, "master", Duration::from_millis(100)); + for _ in 0..N_THREADS { + let mut a = hist.clone_with_tag("sleep_1ns"); + thread::spawn(move || { + + let mut prev = Instant::now(); + let mut loop_time = Instant::now(); + + loop { + prev = loop_time; + loop_time = Instant::now(); + a.record(nanos(loop_time - prev)); + a.check_send(loop_time); + + + if loop_time - start > Duration::from_secs(N_SECS) { break } + + thread::sleep(Duration::new(0, 1)); + //thread::yield_now(); + } + }); + } + + thread::sleep(Duration::from_secs(N_SECS)); + + 0 +} + +fn test_3() -> i32 { + let start = Instant::now(); + let hist = HistLog::new("sleeptest", "master", Duration::from_millis(100)); + let mut a = hist.clone_with_tag("yield"); + let mut b = hist.clone_with_tag("sleep_0"); + let mut c = hist.clone_with_tag("sleep_100ns"); thread::spawn(move || { @@ -31,7 +79,8 @@ fn main() { i += 1; //if i % 100 == 0 { thread::sleep(Duration::new(0, 0)); } - thread::sleep(Duration::new(0, 0)); + //thread::sleep(Duration::new(0, 0)); + thread::yield_now(); } }); @@ -70,7 +119,10 @@ fn main() { i += 1; //if i % 100_000 == 0 { thread::sleep(Duration::from_millis(10)); } //if i % 100 == 0 { thread::sleep(Duration::new(0, 0)); } - thread::sleep(Duration::new(0, 0)); + thread::sleep(Duration::new(0, 100)); } + + 0 } +