|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +use arrow::record_batch::RecordBatch; |
| 21 | +use arrow_array::{ArrayRef, Int32Array, Int64Array, StringArray}; |
| 22 | +use datafusion_execution::TaskContext; |
| 23 | +use datafusion_physical_expr::expressions::col; |
| 24 | +use datafusion_physical_expr::PhysicalSortExpr; |
| 25 | +use datafusion_physical_plan::memory::MemoryExec; |
| 26 | +use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec; |
| 27 | +use datafusion_physical_plan::{collect, ExecutionPlan}; |
| 28 | + |
| 29 | +use criterion::async_executor::FuturesExecutor; |
| 30 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 31 | + |
| 32 | +fn generate_spm_for_round_robin_tie_breaker( |
| 33 | + has_same_value: bool, |
| 34 | + enable_round_robin_repartition: bool, |
| 35 | + batch_count: usize, |
| 36 | + partition_count: usize, |
| 37 | +) -> SortPreservingMergeExec { |
| 38 | + let row_size = 256; |
| 39 | + let rb = if has_same_value { |
| 40 | + let a: ArrayRef = Arc::new(Int32Array::from(vec![1; row_size])); |
| 41 | + let b: ArrayRef = Arc::new(StringArray::from_iter(vec![Some("a"); row_size])); |
| 42 | + let c: ArrayRef = Arc::new(Int64Array::from_iter(vec![0; row_size])); |
| 43 | + RecordBatch::try_from_iter(vec![("a", a), ("b", b), ("c", c)]).unwrap() |
| 44 | + } else { |
| 45 | + let v = (0i32..row_size as i32).collect::<Vec<_>>(); |
| 46 | + let a: ArrayRef = Arc::new(Int32Array::from(v)); |
| 47 | + |
| 48 | + // Use alphanumeric characters |
| 49 | + let charset: Vec<char> = |
| 50 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 51 | + .chars() |
| 52 | + .collect(); |
| 53 | + |
| 54 | + let mut strings = Vec::new(); |
| 55 | + for i in 0..256 { |
| 56 | + let mut s = String::new(); |
| 57 | + s.push(charset[i % charset.len()]); |
| 58 | + s.push(charset[(i / charset.len()) % charset.len()]); |
| 59 | + strings.push(Some(s)); |
| 60 | + } |
| 61 | + |
| 62 | + let b: ArrayRef = Arc::new(StringArray::from_iter(strings)); |
| 63 | + |
| 64 | + let v = (0i64..row_size as i64).collect::<Vec<_>>(); |
| 65 | + let c: ArrayRef = Arc::new(Int64Array::from_iter(v)); |
| 66 | + RecordBatch::try_from_iter(vec![("a", a), ("b", b), ("c", c)]).unwrap() |
| 67 | + }; |
| 68 | + |
| 69 | + let rbs = (0..batch_count).map(|_| rb.clone()).collect::<Vec<_>>(); |
| 70 | + let partitiones = vec![rbs.clone(); partition_count]; |
| 71 | + |
| 72 | + let schema = rb.schema(); |
| 73 | + let sort = vec![ |
| 74 | + PhysicalSortExpr { |
| 75 | + expr: col("b", &schema).unwrap(), |
| 76 | + options: Default::default(), |
| 77 | + }, |
| 78 | + PhysicalSortExpr { |
| 79 | + expr: col("c", &schema).unwrap(), |
| 80 | + options: Default::default(), |
| 81 | + }, |
| 82 | + ]; |
| 83 | + |
| 84 | + let exec = MemoryExec::try_new(&partitiones, schema, None).unwrap(); |
| 85 | + SortPreservingMergeExec::new(sort, Arc::new(exec)) |
| 86 | + .with_round_robin_repartition(enable_round_robin_repartition) |
| 87 | +} |
| 88 | + |
| 89 | +fn run_bench( |
| 90 | + c: &mut Criterion, |
| 91 | + has_same_value: bool, |
| 92 | + enable_round_robin_repartition: bool, |
| 93 | + batch_count: usize, |
| 94 | + partition_count: usize, |
| 95 | + description: &str, |
| 96 | +) { |
| 97 | + let task_ctx = TaskContext::default(); |
| 98 | + let task_ctx = Arc::new(task_ctx); |
| 99 | + |
| 100 | + let spm = Arc::new(generate_spm_for_round_robin_tie_breaker( |
| 101 | + has_same_value, |
| 102 | + enable_round_robin_repartition, |
| 103 | + batch_count, |
| 104 | + partition_count, |
| 105 | + )) as Arc<dyn ExecutionPlan>; |
| 106 | + |
| 107 | + c.bench_function(description, |b| { |
| 108 | + b.to_async(FuturesExecutor) |
| 109 | + .iter(|| black_box(collect(Arc::clone(&spm), Arc::clone(&task_ctx)))) |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +fn criterion_benchmark(c: &mut Criterion) { |
| 114 | + let params = [ |
| 115 | + (true, false, "low_card_without_tiebreaker"), // low cardinality, no tie breaker |
| 116 | + (true, true, "low_card_with_tiebreaker"), // low cardinality, with tie breaker |
| 117 | + (false, false, "high_card_without_tiebreaker"), // high cardinality, no tie breaker |
| 118 | + (false, true, "high_card_with_tiebreaker"), // high cardinality, with tie breaker |
| 119 | + ]; |
| 120 | + |
| 121 | + let batch_counts = [1, 25, 625]; |
| 122 | + let partition_counts = [2, 8, 32]; |
| 123 | + |
| 124 | + for &(has_same_value, enable_round_robin_repartition, cardinality_label) in ¶ms { |
| 125 | + for &batch_count in &batch_counts { |
| 126 | + for &partition_count in &partition_counts { |
| 127 | + let description = format!( |
| 128 | + "{}_batch_count_{}_partition_count_{}", |
| 129 | + cardinality_label, batch_count, partition_count |
| 130 | + ); |
| 131 | + run_bench( |
| 132 | + c, |
| 133 | + has_same_value, |
| 134 | + enable_round_robin_repartition, |
| 135 | + batch_count, |
| 136 | + partition_count, |
| 137 | + &description, |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +criterion_group!(benches, criterion_benchmark); |
| 145 | +criterion_main!(benches); |
0 commit comments