@@ -1065,5 +1065,64 @@ fn with_extra_columns(batches: Vec<RecordBatch>, n: usize) -> Vec<RecordBatch> {
10651065 . collect ( )
10661066}
10671067
1068- criterion_group ! ( benches, criterion_benchmark, sort_axis_benchmark) ;
1068+ fn small_batches_benchmark ( c : & mut Criterion ) {
1069+ let mut group = c. benchmark_group ( "small_batches" ) ;
1070+ group. sample_size ( 10 ) ;
1071+ group. sampling_mode ( criterion:: SamplingMode :: Flat ) ;
1072+ group. bench_function ( "two_keys" , |b| {
1073+ let runtime = Runtime :: new ( ) . unwrap ( ) ;
1074+ let ctx = SessionContext :: new_with_config (
1075+ SessionConfig :: new ( )
1076+ . with_target_partitions ( 1 )
1077+ . with_batch_size ( 32 ) ,
1078+ ) ;
1079+
1080+ // Store small input batches before timing, then restore normal output batches.
1081+ runtime. block_on ( async {
1082+ ctx. sql (
1083+ "CREATE TABLE sort_input AS \
1084+ SELECT value % 997 AS sort_key, value FROM range(2000000)",
1085+ )
1086+ . await
1087+ . unwrap ( )
1088+ . collect ( )
1089+ . await
1090+ . unwrap ( ) ;
1091+ ctx. sql ( "SET datafusion.execution.batch_size = 8192" )
1092+ . await
1093+ . unwrap ( )
1094+ . collect ( )
1095+ . await
1096+ . unwrap ( ) ;
1097+ } ) ;
1098+
1099+ // Two keys exercise row-format merging of the small sorted runs.
1100+ b. iter ( || {
1101+ runtime. block_on ( async {
1102+ let mut stream = ctx
1103+ . sql (
1104+ "SELECT sort_key, value FROM sort_input ORDER BY sort_key, value" ,
1105+ )
1106+ . await
1107+ . unwrap ( )
1108+ . execute_stream ( )
1109+ . await
1110+ . unwrap ( ) ;
1111+ let mut row_count = 0 ;
1112+ while let Some ( batch) = stream. next ( ) . await {
1113+ row_count += batch. unwrap ( ) . num_rows ( ) ;
1114+ }
1115+ row_count
1116+ } )
1117+ } ) ;
1118+ } ) ;
1119+ group. finish ( ) ;
1120+ }
1121+
1122+ criterion_group ! (
1123+ benches,
1124+ criterion_benchmark,
1125+ sort_axis_benchmark,
1126+ small_batches_benchmark
1127+ ) ;
10691128criterion_main ! ( benches) ;
0 commit comments