@@ -39,29 +39,76 @@ fn random_resource(atom: &Atom) -> Resource {
3939fn criterion_benchmark ( c : & mut Criterion ) {
4040 let store = Db :: init_temp ( "bench" ) . unwrap ( ) ;
4141
42- c. bench_function ( "add_resource" , |b| {
43- b. iter ( || {
44- let resource = random_resource ( & random_atom_string ( ) ) ;
45- store
46- . add_resource_opts ( & resource, true , true , false )
47- . unwrap ( ) ;
48- } )
42+ let mut flushing = c. benchmark_group ( "IO bound benchmarks" ) ;
43+ flushing. significance_level ( 0.1 ) . sample_size ( 10 ) ;
44+
45+ flushing. bench_function ( "flush 100 resources" , |b| {
46+ b. iter_batched (
47+ || {
48+ // SETUP: Create 100 dirty resources
49+ for _ in 0 ..100 {
50+ let resource = random_resource ( & random_atom_string ( ) ) ;
51+ store
52+ . add_resource_opts ( & resource, true , true , false )
53+ . unwrap ( ) ;
54+ }
55+ } ,
56+ |( ) | {
57+ // MEASURE: Only the flush
58+ store. flush ( ) . unwrap ( ) ;
59+ } ,
60+ criterion:: BatchSize :: SmallInput ,
61+ )
4962 } ) ;
5063
51- c. bench_function ( "resource.save() string" , |b| {
52- b. iter ( || {
53- let mut resource = random_resource ( & random_atom_string ( ) ) ;
54- resource. save ( & store) . unwrap ( ) ;
64+ flushing. bench_function ( "resource.save() string" , |b| {
65+ b. iter_custom ( |iters| {
66+ let mut total_duration = std:: time:: Duration :: new ( 0 , 0 ) ;
67+ let mut i = 0 ;
68+ let flush_interval = 100 ;
69+
70+ while i < iters {
71+ let batch_size = std:: cmp:: min ( flush_interval, iters - i) ;
72+
73+ let start = std:: time:: Instant :: now ( ) ;
74+ for _ in 0 ..batch_size {
75+ let mut resource = random_resource ( & random_atom_string ( ) ) ;
76+ resource. save_locally ( & store) . unwrap ( ) ;
77+ }
78+ total_duration += start. elapsed ( ) ;
79+
80+ store. flush ( ) . unwrap ( ) ;
81+ i += batch_size;
82+ }
83+ total_duration
5584 } )
5685 } ) ;
5786
58- c. bench_function ( "resource.save() array" , |b| {
59- b. iter ( || {
60- let mut resource = random_resource ( & random_atom_array ( ) ) ;
61- resource. save ( & store) . unwrap ( ) ;
87+ flushing. bench_function ( "resource.save() array" , |b| {
88+ b. iter_custom ( |iters| {
89+ let mut total_duration = std:: time:: Duration :: new ( 0 , 0 ) ;
90+ let mut i = 0 ;
91+ let flush_interval = 100 ;
92+
93+ while i < iters {
94+ let batch_size = std:: cmp:: min ( flush_interval, iters - i) ;
95+
96+ let start = std:: time:: Instant :: now ( ) ;
97+ for _ in 0 ..batch_size {
98+ let mut resource = random_resource ( & random_atom_array ( ) ) ;
99+ resource. save_locally ( & store) . unwrap ( ) ;
100+ }
101+ total_duration += start. elapsed ( ) ;
102+
103+ store. flush ( ) . unwrap ( ) ;
104+ i += batch_size;
105+ }
106+ total_duration
62107 } )
63108 } ) ;
64109
110+ flushing. finish ( ) ;
111+
65112 let big_resource = store
66113 . get_resource_extended (
67114 "https://localhost/collections" ,
@@ -94,13 +141,20 @@ fn criterion_benchmark(c: &mut Criterion) {
94141 } )
95142 } ) ;
96143
97- c. bench_function ( "all_resources()" , |b| {
144+ let mut all_resources_group = c. benchmark_group ( "all_resources" ) ;
145+ all_resources_group. sample_size ( 10 ) ;
146+
147+ all_resources_group. bench_function ( "all_resources()" , |b| {
98148 b. iter ( || {
99149 let _all = store. all_resources ( false ) . collect :: < Vec < Resource > > ( ) ;
100150 } )
101151 } ) ;
102152
153+ all_resources_group. finish ( ) ;
154+ println ! ( "Clearing store" ) ;
155+ // If this takes a long time, it probably means there is still a lot of data that needs to be flushed.
103156 store. clear_all_danger ( ) . unwrap ( ) ;
157+ println ! ( "Store cleared" ) ;
104158}
105159
106160criterion_group ! ( benches, criterion_benchmark) ;
0 commit comments