[e5aba4a] | 1 | use std::sync::Arc; |
---|
| 2 | use std::sync::atomic::Ordering; |
---|
| 3 | use std::time::Instant; |
---|
| 4 | |
---|
| 5 | use tokio::runtime::Builder; |
---|
| 6 | use tokio::sync; |
---|
| 7 | use tokio::task; |
---|
| 8 | |
---|
| 9 | use clap::App; |
---|
| 10 | use num_format::{Locale, ToFormattedString}; |
---|
| 11 | |
---|
| 12 | #[path = "../bench.rs"] |
---|
| 13 | mod bench; |
---|
| 14 | |
---|
| 15 | // ================================================== |
---|
| 16 | struct Yielder { |
---|
| 17 | sem: sync::Semaphore, |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | async fn yield_main(idx: usize, others: Arc<Vec<Arc<Yielder>>>, exp: Arc<bench::BenchData> ) -> u64 { |
---|
| 21 | let this = &others[idx]; |
---|
| 22 | this.sem.acquire().await.forget(); |
---|
| 23 | |
---|
| 24 | let mut count:u64 = 0; |
---|
| 25 | loop { |
---|
| 26 | task::yield_now().await; |
---|
| 27 | count += 1; |
---|
| 28 | |
---|
| 29 | if exp.clock_mode && exp.stop.load(Ordering::Relaxed) { break; } |
---|
| 30 | if !exp.clock_mode && count >= exp.stop_count { break; } |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | exp.threads_left.fetch_sub(1, Ordering::SeqCst); |
---|
| 34 | count |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | // ================================================== |
---|
| 38 | fn main() { |
---|
[d0b9247] | 39 | let options = App::new("Yield Tokio") |
---|
[e5aba4a] | 40 | .args(&bench::args()) |
---|
| 41 | .get_matches(); |
---|
| 42 | |
---|
| 43 | let nthreads = options.value_of("nthreads").unwrap().parse::<usize>().unwrap(); |
---|
| 44 | let nprocs = options.value_of("nprocs").unwrap().parse::<usize>().unwrap(); |
---|
| 45 | |
---|
[821c534] | 46 | let exp = Arc::new(bench::BenchData::new(options, nthreads, None)); |
---|
[e5aba4a] | 47 | |
---|
| 48 | let s = (1000000 as u64).to_formatted_string(&Locale::en); |
---|
| 49 | assert_eq!(&s, "1,000,000"); |
---|
| 50 | |
---|
| 51 | let thddata : Arc<Vec<Arc<Yielder>>> = Arc::new( |
---|
[821c534] | 52 | (0..nthreads).map(|_i| { |
---|
[e5aba4a] | 53 | Arc::new(Yielder{ |
---|
| 54 | sem: sync::Semaphore::new(0), |
---|
| 55 | }) |
---|
| 56 | }).collect() |
---|
| 57 | ); |
---|
| 58 | |
---|
| 59 | let mut global_counter :u64 = 0; |
---|
| 60 | let mut duration : std::time::Duration = std::time::Duration::from_secs(0); |
---|
| 61 | let runtime = Builder::new_multi_thread() |
---|
| 62 | .worker_threads(nprocs) |
---|
| 63 | .enable_all() |
---|
| 64 | .build() |
---|
| 65 | .unwrap(); |
---|
| 66 | |
---|
| 67 | runtime.block_on(async { |
---|
| 68 | let threads: Vec<_> = (0..nthreads).map(|i| { |
---|
| 69 | tokio::spawn(yield_main(i, thddata.clone(), exp.clone())) |
---|
| 70 | }).collect(); |
---|
| 71 | println!("Starting"); |
---|
| 72 | |
---|
| 73 | let start = Instant::now(); |
---|
| 74 | |
---|
| 75 | for i in 0..nthreads { |
---|
| 76 | thddata[i].sem.add_permits(1); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | duration = exp.wait(&start).await; |
---|
| 80 | |
---|
| 81 | println!("\nDone"); |
---|
| 82 | |
---|
| 83 | for i in 0..nthreads { |
---|
| 84 | thddata[i].sem.add_permits(1); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | for t in threads { |
---|
| 88 | global_counter += t.await.unwrap(); |
---|
| 89 | } |
---|
| 90 | }); |
---|
| 91 | |
---|
| 92 | println!("Duration (ms) : {}", (duration.as_millis()).to_formatted_string(&Locale::en)); |
---|
| 93 | println!("Number of processors: {}", (nprocs).to_formatted_string(&Locale::en)); |
---|
| 94 | println!("Number of threads : {}", (nthreads).to_formatted_string(&Locale::en)); |
---|
| 95 | println!("Total yields : {:>15}", (global_counter).to_formatted_string(&Locale::en)); |
---|
| 96 | println!("Yields per second : {:>15}", (((global_counter as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 97 | println!("ns per yields : {:>15}", ((duration.as_nanos() as f64 / global_counter as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 98 | println!("Yields per threads : {:>15}", (global_counter / nthreads as u64).to_formatted_string(&Locale::en)); |
---|
| 99 | println!("Yields per procs : {:>15}", (global_counter / nprocs as u64).to_formatted_string(&Locale::en)); |
---|
| 100 | println!("Yields/sec/procs : {:>15}", ((((global_counter as f64) / nprocs as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en)); |
---|
| 101 | println!("ns per yields/procs : {:>15}", ((duration.as_nanos() as f64 / (global_counter as f64 / nprocs as f64)) as u64).to_formatted_string(&Locale::en)); |
---|
| 102 | } |
---|