1- use criterion:: { criterion_group, criterion_main , BenchmarkId , Criterion } ;
1+ use criterion:: { criterion_group, BenchmarkId , Criterion } ;
22use cubestore:: config:: Config ;
33use cubestore:: metastore:: { BaseRocksStoreFs , Column , ColumnType , MetaStore , RocksMetaStore } ;
44use cubestore:: remotefs:: LocalDirRemoteFs ;
@@ -8,6 +8,13 @@ use std::fs;
88use std:: sync:: Arc ;
99use tokio:: runtime:: { Builder , Runtime } ;
1010
11+ mod tracking_allocator;
12+
13+ use tracking_allocator:: TrackingAllocator ;
14+
15+ #[ global_allocator]
16+ static ALLOCATOR : TrackingAllocator = TrackingAllocator :: new ( ) ;
17+
1118fn prepare_metastore ( name : & str ) -> Result < Arc < RocksMetaStore > , CubeError > {
1219 let config = Config :: test ( name) ;
1320
@@ -97,118 +104,94 @@ async fn bench_get_tables_with_path(
97104 }
98105}
99106
100- fn benchmark_get_tables_with_path_small ( c : & mut Criterion , runtime : & Runtime ) {
107+ fn do_get_tables_with_path_bench (
108+ c : & mut Criterion ,
109+ runtime : & Runtime ,
110+ num_schemas : usize ,
111+ tables_per_schema : usize ,
112+ iterations : usize ,
113+ ) {
114+ let total_tables = num_schemas * tables_per_schema;
101115 let metastore = runtime. block_on ( async {
102- let metastore = prepare_metastore ( "get_tables_with_path_small" ) . unwrap ( ) ;
103- populate_metastore ( & metastore, 10 , 10 ) . await . unwrap ( ) ; // 100 tables
116+ let metastore =
117+ prepare_metastore ( & format ! ( "get_tables_with_path_{}" , total_tables) ) . unwrap ( ) ;
118+ populate_metastore ( & metastore, num_schemas, tables_per_schema)
119+ . await
120+ . unwrap ( ) ;
104121 metastore
105122 } ) ;
106123
107124 c. bench_with_input (
108- BenchmarkId :: new ( "get_tables_with_path_small_include_non_ready_true " , 100 ) ,
109- & 100 ,
125+ BenchmarkId :: new ( "get_tables_with_path_include_non_ready_true " , total_tables ) ,
126+ & iterations ,
110127 |b, & iterations| {
111128 b. to_async ( runtime)
112129 . iter ( || bench_get_tables_with_path ( & metastore, true , iterations) ) ;
113130 } ,
114131 ) ;
115132
116133 c. bench_with_input (
117- BenchmarkId :: new ( "get_tables_with_path_small_include_non_ready_false " , 100 ) ,
118- & 100 ,
134+ BenchmarkId :: new ( "get_tables_with_path_include_non_ready_false " , total_tables ) ,
135+ & iterations ,
119136 |b, & iterations| {
120137 b. to_async ( runtime)
121138 . iter ( || bench_get_tables_with_path ( & metastore, false , iterations) ) ;
122139 } ,
123140 ) ;
124141}
125142
126- fn benchmark_get_tables_with_path_medium ( c : & mut Criterion , runtime : & Runtime ) {
127- let metastore = runtime. block_on ( async {
128- let metastore = prepare_metastore ( "get_tables_with_path_medium" ) . unwrap ( ) ;
129- populate_metastore ( & metastore, 50 , 20 ) . await . unwrap ( ) ; // 1,000 tables
130- metastore
131- } ) ;
132-
133- c. bench_with_input (
134- BenchmarkId :: new ( "get_tables_with_path_medium_include_non_ready_true" , 50 ) ,
135- & 50 ,
136- |b, & iterations| {
137- b. to_async ( runtime)
138- . iter ( || bench_get_tables_with_path ( & metastore, true , iterations) ) ;
139- } ,
140- ) ;
141-
142- c. bench_with_input (
143- BenchmarkId :: new ( "get_tables_with_path_medium_include_non_ready_false" , 50 ) ,
144- & 50 ,
145- |b, & iterations| {
146- b. to_async ( runtime)
147- . iter ( || bench_get_tables_with_path ( & metastore, false , iterations) ) ;
148- } ,
149- ) ;
143+ async fn do_cold_cache_test ( num_schemas : usize , tables_per_schema : usize ) {
144+ let fresh_metastore = prepare_metastore ( "cold_cache_fresh" ) . unwrap ( ) ;
145+ populate_metastore ( & fresh_metastore, num_schemas, tables_per_schema)
146+ . await
147+ . unwrap ( ) ;
148+ let result = fresh_metastore. get_tables_with_path ( false ) . await ;
149+ assert ! ( result. is_ok( ) ) ;
150150}
151151
152- fn benchmark_get_tables_with_path_large ( c : & mut Criterion , runtime : & Runtime ) {
153- let metastore = runtime. block_on ( async {
154- let metastore = prepare_metastore ( "get_tables_with_path_large" ) . unwrap ( ) ;
155- populate_metastore ( & metastore, 25 , 1000 ) . await . unwrap ( ) ; // 25,000 tables
156- metastore
157- } ) ;
158-
159- c. bench_with_input (
160- BenchmarkId :: new ( "get_tables_with_path_large_include_non_ready_true" , 10 ) ,
161- & 10 ,
162- |b, & iterations| {
163- b. to_async ( runtime)
164- . iter ( || bench_get_tables_with_path ( & metastore, true , iterations) ) ;
165- } ,
166- ) ;
167-
168- c. bench_with_input (
169- BenchmarkId :: new ( "get_tables_with_path_large_include_non_ready_false" , 10 ) ,
170- & 10 ,
171- |b, & iterations| {
172- b. to_async ( runtime)
173- . iter ( || bench_get_tables_with_path ( & metastore, false , iterations) ) ;
174- } ,
175- ) ;
152+ async fn do_warm_cache_test ( metastore : & Arc < RocksMetaStore > ) {
153+ let result = metastore. get_tables_with_path ( false ) . await ;
154+ assert ! ( result. is_ok( ) ) ;
176155}
177156
178- fn cold_vs_warm_cache_benchmark ( c : & mut Criterion , runtime : & Runtime ) {
157+ fn do_cold_vs_warm_cache_bench (
158+ c : & mut Criterion ,
159+ runtime : & Runtime ,
160+ num_schemas : usize ,
161+ tables_per_schema : usize ,
162+ ) {
179163 let metastore = runtime. block_on ( async {
180164 let metastore = prepare_metastore ( "cold_warm_cache" ) . unwrap ( ) ;
181- populate_metastore ( & metastore, 20 , 50 ) . await . unwrap ( ) ; // 1,000 tables
165+ populate_metastore ( & metastore, num_schemas, tables_per_schema)
166+ . await
167+ . unwrap ( ) ;
182168 metastore
183169 } ) ;
184170
185- // Cold cache benchmark (first call)
186171 c. bench_function ( "get_tables_with_path_cold_cache" , |b| {
187- b. to_async ( runtime) . iter ( || async {
188- let fresh_metastore = prepare_metastore ( "cold_cache_fresh" ) . unwrap ( ) ;
189- populate_metastore ( & fresh_metastore, 20 , 50 ) . await . unwrap ( ) ;
190- let result = fresh_metastore. get_tables_with_path ( false ) . await ;
191- assert ! ( result. is_ok( ) ) ;
192- } ) ;
172+ b. to_async ( runtime)
173+ . iter ( || do_cold_cache_test ( num_schemas, tables_per_schema) ) ;
193174 } ) ;
194175
195- // Warm cache benchmark (subsequent calls)
196176 c. bench_function ( "get_tables_with_path_warm_cache" , |b| {
197- b. to_async ( runtime) . iter ( || async {
198- let result = metastore. get_tables_with_path ( false ) . await ;
199- assert ! ( result. is_ok( ) ) ;
200- } ) ;
177+ b. to_async ( runtime) . iter ( || do_warm_cache_test ( & metastore) ) ;
201178 } ) ;
202179}
203180
204181fn do_benches ( c : & mut Criterion ) {
182+ ALLOCATOR . reset_stats ( ) ;
205183 let runtime = Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ;
206184
207- benchmark_get_tables_with_path_small ( c, & runtime) ;
208- benchmark_get_tables_with_path_medium ( c, & runtime) ;
209- benchmark_get_tables_with_path_large ( c, & runtime) ;
210- cold_vs_warm_cache_benchmark ( c, & runtime) ;
185+ do_get_tables_with_path_bench ( c, & runtime, 10 , 10 , 100 ) ;
186+ do_get_tables_with_path_bench ( c, & runtime, 50 , 20 , 50 ) ;
187+ do_get_tables_with_path_bench ( c, & runtime, 25 , 1000 , 10 ) ;
188+
189+ do_cold_vs_warm_cache_bench ( c, & runtime, 20 , 50 ) ;
211190}
212191
213192criterion_group ! ( benches, do_benches) ;
214- criterion_main ! ( benches) ;
193+
194+ fn main ( ) {
195+ benches ( ) ;
196+ ALLOCATOR . print_stats ( ) ;
197+ }
0 commit comments