From 1371be8cbfa64d1f567a052d4a37bb6daba478de Mon Sep 17 00:00:00 2001 From: "zhuxiujia@qq.com" Date: Tue, 12 Dec 2023 22:28:11 +0800 Subject: [PATCH] add bench test --- benches/chan.rs | 171 ++++++++++++++++++++++----------------------- benches/lib.rs | 8 +-- benches/map.rs | 3 - benches/stack.rs | 13 ++++ benches/time.rs | 45 ++++++------ benches/vec.rs | 3 - examples/src/co.rs | 1 + mco-gen/src/lib.rs | 2 + 8 files changed, 121 insertions(+), 125 deletions(-) create mode 100644 benches/stack.rs diff --git a/benches/chan.rs b/benches/chan.rs index 03cd883..076a8b6 100644 --- a/benches/chan.rs +++ b/benches/chan.rs @@ -1,106 +1,101 @@ #![feature(test)] +extern crate test; -#[cfg(all(nightly, test))] -mod bench { - #![cfg(nightly)] - extern crate test; +use self::test::Bencher; +use std::sync::mpsc::channel; - use self::test::Bencher; - use std::sync::mpsc::channel; - use std::sync::Arc; +use mco::chan; - use mco::chan; - use mco::std::queue::mpmc_bounded::Queue; - use std::thread; +use std::thread; +use mco::std::queue::mpsc_list::Queue; - #[bench] - fn bounded_mpmc(b: &mut Bencher) { - b.iter(|| { - let total_work = 1000_000; - let nthreads = 1; - let nmsgs = total_work / nthreads; - let q = Queue::with_capacity(nthreads * nmsgs); - assert_eq!(None, q.pop()); - let (tx, rx) = chan!(); +#[bench] +fn bounded_mpmc(b: &mut Bencher) { + b.iter(|| { + let total_work = 1000_000; + let nthreads = 1; + let nmsgs = total_work / nthreads; + let q = Queue::with_capacity(nthreads * nmsgs); + assert_eq!(None, q.pop()); + let (tx, rx) = chan!(); - for _ in 0..nthreads { - let q = q.clone(); - let tx = tx.clone(); - thread::spawn(move || { - let q = q; - for i in 0..nmsgs { - assert!(q.push(i).is_ok()); - } - tx.send(()).unwrap(); - }); - } + for _ in 0..nthreads { + let q = q.clone(); + let tx = tx.clone(); + thread::spawn(move || { + let q = q; + for i in 0..nmsgs { + assert!(q.push(i).is_ok()); + } + tx.send(()).unwrap(); + }); + } - let mut completion_rxs = vec![]; - for _ in 0..nthreads { - let (tx, rx) = channel(); - completion_rxs.push(rx); - let q = q.clone(); - thread::spawn(move || { - let q = q; - let mut i = 0; - loop { - match q.pop() { - None => {} - Some(_) => { - i += 1; - if i == nmsgs { - break; - } + let mut completion_rxs = vec![]; + for _ in 0..nthreads { + let (tx, rx) = channel(); + completion_rxs.push(rx); + let q = q.clone(); + thread::spawn(move || { + let q = q; + let mut i = 0; + loop { + match q.pop() { + None => {} + Some(_) => { + i += 1; + if i == nmsgs { + break; } } } - tx.send(i).unwrap(); - }); - } - - for rx in completion_rxs.iter_mut() { - assert_eq!(nmsgs, rx.recv().unwrap()); - } - for _ in 0..nthreads { - rx.recv().unwrap(); - } - }); - } - - // #[bench] - // the channel bench result show that it's 10 fold slow than our queue - // not to mention the multi core contention - #[allow(dead_code)] - fn sys_stream_test(b: &mut Bencher) { - b.iter(|| { - let (tx, rx) = channel(); - let total_work: usize = 1000_000; - // create worker threads that generate mono increasing index - // in other thread the value should be still 100 - thread::spawn(move || { - for i in 0..total_work { - tx.send(i).unwrap(); } + tx.send(i).unwrap(); }); + } + + for rx in completion_rxs.iter_mut() { + assert_eq!(nmsgs, rx.recv().unwrap()); + } + for _ in 0..nthreads { + rx.recv().unwrap(); + } + }); +} +// #[bench] +// the channel bench result show that it's 10 fold slow than our queue +// not to mention the multi core contention +#[allow(dead_code)] +fn sys_stream_test(b: &mut Bencher) { + b.iter(|| { + let (tx, rx) = channel(); + let total_work: usize = 1000_000; + // create worker threads that generate mono increasing index + // in other thread the value should be still 100 + thread::spawn(move || { for i in 0..total_work { - assert_eq!(i, rx.recv().unwrap()); + tx.send(i).unwrap(); } }); - } - // improve performance from 39,294 ns/iter to 12,207 ns/iter (my computer) - //test bench_channel ... bench: 12,207 ns/iter (+/- 118) - #[bench] - fn bench_channel(b: &mut Bencher) { - b.iter(|| { - let (s, r) = chan!(); - for _ in 0..1000 { - s.send(1); - } - for _ in 0..1000 { - let r = r.recv().unwrap(); - } - }); - } + for i in 0..total_work { + assert_eq!(i, rx.recv().unwrap()); + } + }); +} + +// improve performance from 39,294 ns/iter to 12,207 ns/iter (my computer) +//test bench_channel ... bench: 12,207 ns/iter (+/- 118) +#[bench] +fn bench_channel(b: &mut Bencher) { + b.iter(|| { + let (s, r) = chan!(); + for _ in 0..1000 { + s.send(1); + } + for _ in 0..1000 { + let r = r.recv().unwrap(); + } + }); } diff --git a/benches/lib.rs b/benches/lib.rs index ecddd87..14c3979 100644 --- a/benches/lib.rs +++ b/benches/lib.rs @@ -1,13 +1,9 @@ -#![cfg(nightly)] #![feature(test)] - -#[macro_use] -extern crate mco; extern crate test; -use crate::coroutine::*; -use mco::{config, coroutine}; +use mco::{co}; use test::Bencher; +use mco::coroutine::scope; #[bench] fn yield_bench(b: &mut Bencher) { diff --git a/benches/map.rs b/benches/map.rs index c8b8f4c..c0781ba 100644 --- a/benches/map.rs +++ b/benches/map.rs @@ -1,7 +1,4 @@ -#![cfg(nightly)] #![feature(test)] -#[macro_use] -extern crate mco; extern crate test; use mco::std::sync::{Mutex, SyncHashMap}; diff --git a/benches/stack.rs b/benches/stack.rs new file mode 100644 index 0000000..8339db8 --- /dev/null +++ b/benches/stack.rs @@ -0,0 +1,13 @@ +#![feature(test)] +extern crate test; + +use test::Bencher; +use mco_gen::Stack; + +//windows 2404 ns/iter (+/- 306) +#[bench] +fn bench_stack_new(b: &mut Bencher) { + b.iter(|| { + let s = Stack::new(4096); + }); +} \ No newline at end of file diff --git a/benches/time.rs b/benches/time.rs index fcbeb9a..56f0fc2 100644 --- a/benches/time.rs +++ b/benches/time.rs @@ -1,31 +1,26 @@ #![feature(test)] +extern crate test; -#[cfg(all(nightly, test))] -mod bench { - #![feature(test)] - extern crate test; +use mco::std::sync::WaitGroup; +use mco::std::time::time::Time; +use test::Bencher; - use mco::std::sync::WaitGroup; - use mco::std::time::time::Time; - use test::Bencher; - - //test bench::single_thread_test ... bench: 44 ns/iter (+/- 1) - #[bench] - fn single_thread_test(b: &mut Bencher) { - b.iter(|| { - let now = Time::now(); - }); - } +//test bench::single_thread_test ... bench: 44 ns/iter (+/- 1) +#[bench] +fn single_thread_test(b: &mut Bencher) { + b.iter(|| { + let now = Time::now(); + }); +} - #[bench] - fn mult_thread_test(b: &mut Bencher) { - std::thread::spawn(move || { - for _ in 0..100000 { - let now = Time::now(); - } - }); - b.iter(|| { +#[bench] +fn mult_thread_test(b: &mut Bencher) { + std::thread::spawn(move || { + for _ in 0..100000 { let now = Time::now(); - }); - } + } + }); + b.iter(|| { + let now = Time::now(); + }); } diff --git a/benches/vec.rs b/benches/vec.rs index be7b85c..01ac2a6 100644 --- a/benches/vec.rs +++ b/benches/vec.rs @@ -1,7 +1,4 @@ -#![cfg(nightly)] #![feature(test)] -#[macro_use] -extern crate mco; extern crate test; use mco::std::sync::{Mutex, SyncVec}; diff --git a/examples/src/co.rs b/examples/src/co.rs index a242877..e91c935 100644 --- a/examples/src/co.rs +++ b/examples/src/co.rs @@ -2,6 +2,7 @@ use mco::co; use std::time::Duration; fn main() { + mco::config().set_workers(1); println!("thread {:?}", std::thread::current().id()); co!(|| { println!("coroutine run on thread {:?}", std::thread::current().id()); diff --git a/mco-gen/src/lib.rs b/mco-gen/src/lib.rs index 7f0839c..1a67bc7 100644 --- a/mco-gen/src/lib.rs +++ b/mco-gen/src/lib.rs @@ -25,3 +25,5 @@ pub use crate::scope::Scope; pub use crate::yield_::{ co_get_yield, co_set_para, co_yield_with, done, get_yield, yield_, yield_from, yield_with, }; + +pub use stack::Stack; \ No newline at end of file