1- use super :: append_random_data;
1+ use super :: { append_random_data, get_journal } ;
22use commonware_runtime:: {
33 benchmarks:: { context, tokio} ,
4- tokio:: Context ,
4+ tokio:: { Config , Context , Runner } ,
5+ Runner as _,
56} ;
67use commonware_storage:: journal:: fixed:: Journal ;
78use commonware_utils:: array:: FixedBytes ;
@@ -47,7 +48,21 @@ async fn bench_run_concurrent(
4748}
4849
4950fn bench_fixed_read_random ( c : & mut Criterion ) {
50- let executor = tokio:: Executor :: default ( ) ;
51+ // Create a config we can use across all benchmarks (with a fixed `storage_directory`), allowing the
52+ // same test file to be re-used.
53+ let cfg = Config :: default ( ) ;
54+
55+ // Generate a large temp journal with random data.
56+ let runner = Runner :: new ( cfg. clone ( ) ) ;
57+ runner. start ( |ctx| async move {
58+ // Create a large temp journal with random data.
59+ let mut j = get_journal ( ctx, PARTITION , ITEMS_PER_BLOB ) . await ;
60+ append_random_data :: < ITEM_SIZE > ( & mut j, ITEMS_TO_WRITE ) . await ;
61+ j. close ( ) . await . unwrap ( ) ;
62+ } ) ;
63+
64+ // Run the benchmarks
65+ let runner = tokio:: Runner :: new ( cfg. clone ( ) ) ;
5166 for mode in [ "serial" , "concurrent" ] {
5267 for items_to_read in [ 100 , 1_000 , 10_000 , 100_000 ] {
5368 c. bench_function (
@@ -59,18 +74,9 @@ fn bench_fixed_read_random(c: &mut Criterion) {
5974 ITEM_SIZE
6075 ) ,
6176 |b| {
62- b. to_async ( & executor) . iter_custom ( |iters| async move {
63- // Append random data to the journal
77+ b. to_async ( & runner) . iter_custom ( |iters| async move {
6478 let ctx = context:: get :: < commonware_runtime:: tokio:: Context > ( ) ;
65- let j = append_random_data (
66- ctx. clone ( ) ,
67- PARTITION ,
68- ITEMS_PER_BLOB ,
69- ITEMS_TO_WRITE ,
70- )
71- . await ;
72-
73- // Run the benchmark
79+ let j = get_journal ( ctx. clone ( ) , PARTITION , ITEMS_PER_BLOB ) . await ;
7480 let mut duration = Duration :: ZERO ;
7581 for _ in 0 ..iters {
7682 let start = Instant :: now ( ) ;
@@ -81,16 +87,19 @@ fn bench_fixed_read_random(c: &mut Criterion) {
8187 }
8288 duration += start. elapsed ( ) ;
8389 }
84-
85- // Destroy the journal after reading to avoid polluting the next iteration
86- j. destroy ( ) . await . unwrap ( ) ;
87-
8890 duration
8991 } ) ;
9092 } ,
9193 ) ;
9294 }
9395 }
96+
97+ // Clean up the temp journal
98+ let runner = Runner :: new ( cfg) ;
99+ runner. start ( |context| async move {
100+ let j = get_journal :: < ITEM_SIZE > ( context, PARTITION , ITEMS_PER_BLOB ) . await ;
101+ j. destroy ( ) . await . unwrap ( ) ;
102+ } ) ;
94103}
95104
96105criterion_group ! {
0 commit comments