11use criterion:: async_executor:: AsyncExecutor ;
22use criterion:: { BenchmarkId , Criterion , criterion_group, criterion_main} ;
3+ use sharded_ringbuf:: { ExpShardedRingBuf , spawn_dequeuer_unbounded, spawn_enqueuer_with_iterator} ;
34use sharded_ringbuf:: {
45 MLFShardedRingBuf , ShardPolicy , mlf_spawn_dequeuer_unbounded, mlf_spawn_enqueuer_with_iterator,
56 spawn_dequeuer_full_unbounded, spawn_enqueuer_full_with_iterator,
67} ;
7- use sharded_ringbuf:: { ShardedRingBuf , spawn_dequeuer_unbounded, spawn_enqueuer_with_iterator} ;
88use smol:: future;
99use std:: future:: Future ;
1010use std:: sync:: Arc ;
@@ -93,24 +93,15 @@ fn mul_stress(iter: usize) -> u128 {
9393 acc
9494}
9595
96- /// Minimal executor adapter so Criterion can call smol::block_on
97- #[ derive( Clone , Copy ) ]
98- struct SmolExecutor ;
99-
100- impl AsyncExecutor for SmolExecutor {
101- fn block_on < T > ( & self , future : impl Future < Output = T > ) -> T {
102- future:: block_on ( future)
103- }
104- }
105-
10696async fn lfsrb_pin ( capacity : usize , shards : usize , task_count : usize ) {
10797 let max_items: usize = capacity;
10898
109- let rb = Arc :: new ( ShardedRingBuf :: new ( max_items, shards) ) ;
99+ let rb = Arc :: new ( ExpShardedRingBuf :: new ( max_items, shards) ) ;
110100
111101 let mut deq_tasks = Vec :: with_capacity ( shards) ;
112102 let mut enq_tasks = Vec :: with_capacity ( task_count) ;
113103
104+ // necessary for poison
114105 let notifier_task = spawn ( {
115106 let rb_clone = rb. clone ( ) ;
116107 async move {
@@ -133,18 +124,35 @@ async fn lfsrb_pin(capacity: usize, shards: usize, task_count: usize) {
133124 // );
134125 // enq_tasks.push(handle);
135126 // }
127+ // for i in 0..task_count {
128+ // let handle = spawn_enqueuer_with_iterator(
129+ // rb.clone(),
130+ // ShardPolicy::Pin { initial_index: i },
131+ // 0..250000,
132+ // );
133+ // enq_tasks.push(handle);
134+ // }
135+
136136 for i in 0 ..task_count {
137- let handle = spawn_enqueuer_with_iterator (
137+ let handle = spawn_enqueuer_full_with_iterator (
138138 rb. clone ( ) ,
139139 ShardPolicy :: Pin { initial_index : i } ,
140- 0 ..= 250000 ,
140+ 0 ..250000 ,
141141 ) ;
142142 enq_tasks. push ( handle) ;
143143 }
144144
145- for i in 0 ..shards {
145+ // for i in 0..task_count {
146+ // let handle =
147+ // spawn_dequeuer_unbounded(rb.clone(), ShardPolicy::Pin { initial_index: i }, |x| {
148+ // // test_func(x as u128);
149+ // });
150+ // deq_tasks.push(handle);
151+ // }
152+
153+ for i in 0 ..task_count {
146154 let handle =
147- spawn_dequeuer_unbounded ( rb. clone ( ) , ShardPolicy :: Pin { initial_index : i } , |x| {
155+ spawn_dequeuer_full_unbounded ( rb. clone ( ) , ShardPolicy :: Pin { initial_index : i } , |x| {
148156 // test_func(x as u128);
149157 } ) ;
150158 deq_tasks. push ( handle) ;
@@ -212,13 +220,15 @@ async fn mlfsrb_pin(capacity: usize, shards: usize, task_count: usize) {
212220 let handle = tokio:: spawn ( {
213221 let rb_clone = rb. clone ( ) ;
214222 async move {
215- rb_clone. enqueue ( i) . await ;
223+ for j in 0 ..250000 {
224+ rb_clone. enqueue ( j) . await ;
225+ }
216226 }
217227 } ) ;
218228 enq_tasks. push ( handle) ;
219229 }
220230
221- for i in 0 ..shards {
231+ for i in 0 ..task_count {
222232 let handle = tokio:: spawn ( {
223233 let rb_clone = rb. clone ( ) ;
224234 async move {
@@ -227,9 +237,7 @@ async fn mlfsrb_pin(capacity: usize, shards: usize, task_count: usize) {
227237 let item = rb_clone. dequeue_in_shard ( i) . await ;
228238 match item {
229239 None => break ,
230- Some ( val) => {
231-
232- }
240+ Some ( val) => { }
233241 }
234242 }
235243 }
@@ -242,7 +250,7 @@ async fn mlfsrb_pin(capacity: usize, shards: usize, task_count: usize) {
242250 enq. await . unwrap ( ) ;
243251 }
244252
245- for i in 0 ..shards {
253+ for i in 0 ..task_count {
246254 {
247255 let rb = rb. clone ( ) ;
248256 spawn ( async move {
@@ -262,7 +270,7 @@ async fn mlfsrb_pin(capacity: usize, shards: usize, task_count: usize) {
262270async fn lfsrb_pin_deq_full ( capacity : usize , shards : usize , task_count : usize ) {
263271 let max_items: usize = capacity;
264272
265- let rb = Arc :: new ( ShardedRingBuf :: new ( max_items, shards) ) ;
273+ let rb = Arc :: new ( ExpShardedRingBuf :: new ( max_items, shards) ) ;
266274
267275 let mut deq_tasks = Vec :: with_capacity ( shards) ;
268276 let mut enq_tasks = Vec :: with_capacity ( task_count) ;
@@ -380,7 +388,7 @@ async fn lfsrb_pin_with_msg_vec(
380388) {
381389 let max_items: usize = capacity;
382390
383- let rb = Arc :: new ( ShardedRingBuf :: new ( max_items, shards) ) ;
391+ let rb = Arc :: new ( ExpShardedRingBuf :: new ( max_items, shards) ) ;
384392
385393 let mut deq_tasks = Vec :: with_capacity ( shards) ;
386394 let mut enq_tasks = Vec :: with_capacity ( task_count) ;
@@ -568,13 +576,13 @@ async fn mlfsrb_pin_with_msg_vec(
568576
569577fn benchmark_pin ( c : & mut Criterion ) {
570578 // const MAX_THREADS: [usize; 2] = [4, 8];
571- const MAX_THREADS : [ usize ; 1 ] = [ 8 ] ;
572- const CAPACITY : usize = 128 ;
579+ const MAX_THREADS : [ usize ; 1 ] = [ 4 ] ;
580+ const CAPACITY : usize = 1000000 ;
573581 // const CAPACITY: usize = 200000;
574582 // const SHARDS: [usize; 5] = [1, 2, 4, 8, 16];
575583 // const TASKS: [usize; 5] = [1, 2, 4, 8, 16];
576- const SHARDS : [ usize ; 1 ] = [ 1 ] ;
577- const TASKS : [ usize ; 1 ] = [ 100 ] ;
584+ const SHARDS : [ usize ; 1 ] = [ 4 ] ;
585+ const TASKS : [ usize ; 1 ] = [ 4 ] ;
578586
579587 // const MSG_COUNT: usize = 250000;
580588 // // let msg = BigData { buf: Box::new([0; 1 * 1024]) };
@@ -590,34 +598,34 @@ fn benchmark_pin(c: &mut Criterion) {
590598 // }
591599 // }
592600
593- // for thread_num in MAX_THREADS {
594- // let runtime = tokio::runtime::Builder::new_multi_thread()
595- // .enable_all()
596- // .worker_threads(thread_num)
597- // .build()
598- // .unwrap();
601+ for thread_num in MAX_THREADS {
602+ let runtime = tokio:: runtime:: Builder :: new_multi_thread ( )
603+ . enable_all ( )
604+ . worker_threads ( thread_num)
605+ . build ( )
606+ . unwrap ( ) ;
599607
600- // for shard_num in SHARDS {
601- // for task_count in TASKS {
602- // let func_name = format!(
603- // "Pin: {} threads, {} shards, {} enq tasks enqueuing 1 million items, {} looping deq task",
604- // thread_num, shard_num, task_count, shard_num
605- // );
608+ for shard_num in SHARDS {
609+ for task_count in TASKS {
610+ let func_name = format ! (
611+ "Pin: {} threads, {} shards, {} enq tasks enqueuing 1 million items, {} looping deq task" ,
612+ thread_num, shard_num, task_count, shard_num
613+ ) ;
606614
607- // c.bench_with_input(
608- // BenchmarkId::new(func_name, CAPACITY),
609- // &(CAPACITY),
610- // |b, &cap| {
611- // // Insert a call to `to_async` to convert the bencher to async mode.
612- // // The timing loops are the same as with the normal bencher.
613- // b.to_async(&runtime).iter(async || {
614- // lfsrb_pin(cap, shard_num, task_count).await;
615- // });
616- // },
617- // );
618- // }
619- // }
620- // }
615+ c. bench_with_input (
616+ BenchmarkId :: new ( func_name, CAPACITY ) ,
617+ & ( CAPACITY ) ,
618+ |b, & cap| {
619+ // Insert a call to `to_async` to convert the bencher to async mode.
620+ // The timing loops are the same as with the normal bencher.
621+ b. to_async ( & runtime) . iter ( async || {
622+ lfsrb_pin ( cap, shard_num, task_count) . await ;
623+ } ) ;
624+ } ,
625+ ) ;
626+ }
627+ }
628+ }
621629
622630 // for thread_num in MAX_THREADS {
623631 // let runtime = tokio::runtime::Builder::new_multi_thread()
0 commit comments