11//! Benchmark read performance at random offsets.
22
33use super :: { create_append, destroy_append, CACHE_SIZE , PAGE_SIZE , PAGE_SIZE_USIZE } ;
4- use commonware_runtime:: { buffer:: paged:: CacheRef , deterministic, Runner as _} ;
4+ use commonware_runtime:: {
5+ buffer:: paged:: CacheRef , deterministic, tokio, BufferPooler , Runner , Storage ,
6+ } ;
57use commonware_utils:: NZUsize ;
68use criterion:: Criterion ;
79use rand:: { rngs:: StdRng , Rng , SeedableRng } ;
@@ -11,17 +13,21 @@ use std::time::Instant;
1113const TOTAL_PAGES : usize = CACHE_SIZE ;
1214const TOTAL_SIZE : usize = PAGE_SIZE_USIZE * TOTAL_PAGES ;
1315
14- pub fn bench ( c : & mut Criterion ) {
15- for read_size in [ 64 , 256 , 1024 , 4096 ] {
16- let name = format ! ( "read_{read_size}" ) . into_bytes ( ) ;
17-
18- c. bench_function ( & format ! ( "{}/size={}" , module_path!( ) , read_size) , |b| {
16+ fn bench_backend < R > ( c : & mut Criterion , backend : & str , read_size : usize )
17+ where
18+ R : Runner + Default ,
19+ R :: Context : Storage + BufferPooler ,
20+ {
21+ c. bench_function (
22+ & format ! ( "{}/backend={backend} size={read_size}" , module_path!( ) ) ,
23+ |b| {
1924 b. iter_custom ( |iters| {
20- let name = name . clone ( ) ;
25+ let name = format ! ( "read_{backend}_{read_size}" ) . into_bytes ( ) ;
2126
22- let executor = deterministic :: Runner :: default ( ) ;
27+ let executor = R :: default ( ) ;
2328 executor. start ( |ctx| async move {
2429 let cache_ref = CacheRef :: from_pooler ( & ctx, PAGE_SIZE , NZUsize ! ( CACHE_SIZE ) ) ;
30+
2531 // Setup: populate the blob
2632 let append = create_append ( & ctx, & name, cache_ref. clone ( ) ) . await ;
2733 let data = vec ! [ 0xABu8 ; TOTAL_SIZE ] ;
@@ -30,7 +36,7 @@ pub fn bench(c: &mut Criterion) {
3036 drop ( append) ;
3137
3238 // Benchmark: random reads
33- let append = create_append ( & ctx, & name, cache_ref. clone ( ) ) . await ;
39+ let append = create_append ( & ctx, & name, cache_ref) . await ;
3440 let mut buf = vec ! [ 0u8 ; read_size] ;
3541 let max_offset = TOTAL_SIZE - read_size;
3642 let mut rng = StdRng :: seed_from_u64 ( 42 ) ;
@@ -51,6 +57,13 @@ pub fn bench(c: &mut Criterion) {
5157 elapsed
5258 } )
5359 } ) ;
54- } ) ;
60+ } ,
61+ ) ;
62+ }
63+
64+ pub fn bench ( c : & mut Criterion ) {
65+ for read_size in [ 64 , 256 , 1024 , 4096 ] {
66+ bench_backend :: < deterministic:: Runner > ( c, "deterministic" , read_size) ;
67+ bench_backend :: < tokio:: Runner > ( c, "tokio" , read_size) ;
5568 }
5669}
0 commit comments