source: benchmark/readyQ/yield.rs@ e5aba4a

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since e5aba4a was e5aba4a, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added and tweeked rust benchmarks

  • Property mode set to 100644
File size: 3.2 KB
Line 
1use std::sync::Arc;
2use std::sync::atomic::Ordering;
3use std::time::Instant;
4
5use tokio::runtime::Builder;
6use tokio::sync;
7use tokio::task;
8
9use clap::App;
10use num_format::{Locale, ToFormattedString};
11
12#[path = "../bench.rs"]
13mod bench;
14
15// ==================================================
16struct Yielder {
17 sem: sync::Semaphore,
18}
19
20async 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// ==================================================
38fn main() {
39 let options = App::new("Cycle Tokio")
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
46 let exp = Arc::new(bench::BenchData::new(options, nthreads));
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(
52 (0..nthreads).map(|i| {
53 let pi = (i + nthreads) % nthreads;
54 Arc::new(Yielder{
55 sem: sync::Semaphore::new(0),
56 })
57 }).collect()
58 );
59
60 let mut global_counter :u64 = 0;
61 let mut duration : std::time::Duration = std::time::Duration::from_secs(0);
62 let runtime = Builder::new_multi_thread()
63 .worker_threads(nprocs)
64 .enable_all()
65 .build()
66 .unwrap();
67
68 runtime.block_on(async {
69 let threads: Vec<_> = (0..nthreads).map(|i| {
70 tokio::spawn(yield_main(i, thddata.clone(), exp.clone()))
71 }).collect();
72 println!("Starting");
73
74 let start = Instant::now();
75
76 for i in 0..nthreads {
77 thddata[i].sem.add_permits(1);
78 }
79
80 duration = exp.wait(&start).await;
81
82 println!("\nDone");
83
84 for i in 0..nthreads {
85 thddata[i].sem.add_permits(1);
86 }
87
88 for t in threads {
89 global_counter += t.await.unwrap();
90 }
91 });
92
93 println!("Duration (ms) : {}", (duration.as_millis()).to_formatted_string(&Locale::en));
94 println!("Number of processors: {}", (nprocs).to_formatted_string(&Locale::en));
95 println!("Number of threads : {}", (nthreads).to_formatted_string(&Locale::en));
96 println!("Total yields : {:>15}", (global_counter).to_formatted_string(&Locale::en));
97 println!("Yields per second : {:>15}", (((global_counter as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en));
98 println!("ns per yields : {:>15}", ((duration.as_nanos() as f64 / global_counter as f64) as u64).to_formatted_string(&Locale::en));
99 println!("Yields per threads : {:>15}", (global_counter / nthreads as u64).to_formatted_string(&Locale::en));
100 println!("Yields per procs : {:>15}", (global_counter / nprocs as u64).to_formatted_string(&Locale::en));
101 println!("Yields/sec/procs : {:>15}", ((((global_counter as f64) / nprocs as f64) / duration.as_secs() as f64) as u64).to_formatted_string(&Locale::en));
102 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));
103}
Note: See TracBrowser for help on using the repository browser.