Skip to content

Commit

Permalink
add bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Dec 12, 2023
1 parent a9c0d75 commit 1371be8
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 125 deletions.
171 changes: 83 additions & 88 deletions benches/chan.rs
Original file line number Diff line number Diff line change
@@ -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();
}
});
}
8 changes: 2 additions & 6 deletions benches/lib.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions benches/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![cfg(nightly)]
#![feature(test)]
#[macro_use]
extern crate mco;
extern crate test;

use mco::std::sync::{Mutex, SyncHashMap};
Expand Down
13 changes: 13 additions & 0 deletions benches/stack.rs
Original file line number Diff line number Diff line change
@@ -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);
});
}
45 changes: 20 additions & 25 deletions benches/time.rs
Original file line number Diff line number Diff line change
@@ -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();
});
}
3 changes: 0 additions & 3 deletions benches/vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![cfg(nightly)]
#![feature(test)]
#[macro_use]
extern crate mco;
extern crate test;

use mco::std::sync::{Mutex, SyncVec};
Expand Down
1 change: 1 addition & 0 deletions examples/src/co.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions mco-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 1371be8

Please sign in to comment.