|
| 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 | +//! Benchmark for measuring the impact of gc_view_arrays on spill performance. |
| 19 | +//! This test creates a GROUP BY workload with StringView columns and a tight |
| 20 | +//! memory limit to force spilling, then measures spill file sizes, peak RSS, |
| 21 | +//! and query latency. |
| 22 | +
|
| 23 | +use arrow::array::{ArrayRef, Int64Array, RecordBatch, StringViewArray}; |
| 24 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 25 | +use datafusion::datasource::MemTable; |
| 26 | +use datafusion::execution::runtime_env::RuntimeEnvBuilder; |
| 27 | +use datafusion::prelude::*; |
| 28 | +use datafusion_execution::memory_pool::FairSpillPool; |
| 29 | +use std::sync::Arc; |
| 30 | +use std::time::Instant; |
| 31 | + |
| 32 | +/// Create deterministic test data with StringView columns. |
| 33 | +/// Uses deterministic strings (no randomness) for reproducibility. |
| 34 | +fn create_stringview_batches( |
| 35 | + num_batches: usize, |
| 36 | + rows_per_batch: usize, |
| 37 | + num_groups: usize, |
| 38 | +) -> Vec<RecordBatch> { |
| 39 | + let schema = Arc::new(Schema::new(vec![ |
| 40 | + Field::new("group_key", DataType::Utf8View, false), |
| 41 | + Field::new("value", DataType::Int64, false), |
| 42 | + ])); |
| 43 | + |
| 44 | + let mut batches = Vec::with_capacity(num_batches); |
| 45 | + |
| 46 | + for batch_idx in 0..num_batches { |
| 47 | + // 40+ byte strings ensure they are NOT inlined in StringView |
| 48 | + let strings: Vec<String> = (0..rows_per_batch) |
| 49 | + .map(|row_idx| { |
| 50 | + let group = (batch_idx * rows_per_batch + row_idx) % num_groups; |
| 51 | + format!( |
| 52 | + "group_{:010}_payload_data_for_testing_{:08}", |
| 53 | + group, batch_idx |
| 54 | + ) |
| 55 | + }) |
| 56 | + .collect(); |
| 57 | + |
| 58 | + let string_array = |
| 59 | + StringViewArray::from(strings.iter().map(|s| s.as_str()).collect::<Vec<_>>()); |
| 60 | + |
| 61 | + let values: Vec<i64> = (0..rows_per_batch) |
| 62 | + .map(|i| (batch_idx * rows_per_batch + i) as i64) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + let batch = RecordBatch::try_new( |
| 66 | + Arc::clone(&schema), |
| 67 | + vec![ |
| 68 | + Arc::new(string_array) as ArrayRef, |
| 69 | + Arc::new(Int64Array::from(values)) as ArrayRef, |
| 70 | + ], |
| 71 | + ) |
| 72 | + .unwrap(); |
| 73 | + batches.push(batch); |
| 74 | + } |
| 75 | + |
| 76 | + batches |
| 77 | +} |
| 78 | + |
| 79 | +/// Run the GROUP BY query with EXPLAIN ANALYZE and extract spill metrics from the output. |
| 80 | +async fn run_stringview_aggregate_spill_benchmark( |
| 81 | + pool_size_mb: usize, |
| 82 | + num_batches: usize, |
| 83 | + rows_per_batch: usize, |
| 84 | + num_groups: usize, |
| 85 | +) -> (f64, String) { |
| 86 | + let pool_size = pool_size_mb * 1024 * 1024; |
| 87 | + |
| 88 | + let batches = create_stringview_batches(num_batches, rows_per_batch, num_groups); |
| 89 | + |
| 90 | + let schema = batches[0].schema(); |
| 91 | + let table = MemTable::try_new(schema, vec![batches]).unwrap(); |
| 92 | + |
| 93 | + let runtime = RuntimeEnvBuilder::new() |
| 94 | + .with_memory_pool(Arc::new(FairSpillPool::new(pool_size))) |
| 95 | + .build_arc() |
| 96 | + .unwrap(); |
| 97 | + |
| 98 | + let config = SessionConfig::new() |
| 99 | + .with_target_partitions(1) // Single partition for deterministic spill behavior |
| 100 | + .with_batch_size(8192); |
| 101 | + |
| 102 | + let ctx = SessionContext::new_with_config_rt(config, runtime); |
| 103 | + ctx.register_table("t", Arc::new(table)).unwrap(); |
| 104 | + |
| 105 | + let start = Instant::now(); |
| 106 | + |
| 107 | + // Use EXPLAIN ANALYZE to get spill metrics in the execution plan output |
| 108 | + let df = ctx |
| 109 | + .sql("EXPLAIN ANALYZE SELECT group_key, COUNT(*) as cnt, SUM(value) as total FROM t GROUP BY group_key") |
| 110 | + .await |
| 111 | + .unwrap(); |
| 112 | + |
| 113 | + let results = df.collect().await.expect("Query should succeed with spilling"); |
| 114 | + let query_time_ms = start.elapsed().as_secs_f64() * 1000.0; |
| 115 | + |
| 116 | + // Extract the EXPLAIN ANALYZE text |
| 117 | + let explain_text = results |
| 118 | + .iter() |
| 119 | + .flat_map(|batch| { |
| 120 | + let plan_col = batch |
| 121 | + .column_by_name("plan") |
| 122 | + .unwrap() |
| 123 | + .as_any() |
| 124 | + .downcast_ref::<arrow::array::StringArray>() |
| 125 | + .unwrap(); |
| 126 | + (0..batch.num_rows()) |
| 127 | + .map(|i| plan_col.value(i).to_string()) |
| 128 | + .collect::<Vec<_>>() |
| 129 | + }) |
| 130 | + .collect::<Vec<_>>() |
| 131 | + .join("\n"); |
| 132 | + |
| 133 | + (query_time_ms, explain_text) |
| 134 | +} |
| 135 | + |
| 136 | +/// Parse a human-readable size like "20.9 MB" or "512.0 K" to bytes. |
| 137 | +fn parse_human_size(s: &str) -> Option<usize> { |
| 138 | + let s = s.trim(); |
| 139 | + // Try to find a number (possibly with decimal) followed by optional unit |
| 140 | + let num_end = s |
| 141 | + .find(|c: char| !c.is_ascii_digit() && c != '.') |
| 142 | + .unwrap_or(s.len()); |
| 143 | + let num_str = &s[..num_end].trim(); |
| 144 | + let unit = s[num_end..].trim(); |
| 145 | + |
| 146 | + let num: f64 = num_str.parse().ok()?; |
| 147 | + let multiplier = match unit { |
| 148 | + "B" | "" => 1.0, |
| 149 | + "K" => 1024.0, |
| 150 | + "M" | "MB" => 1024.0 * 1024.0, |
| 151 | + "G" | "GB" => 1024.0 * 1024.0 * 1024.0, |
| 152 | + _ => return None, |
| 153 | + }; |
| 154 | + Some((num * multiplier) as usize) |
| 155 | +} |
| 156 | + |
| 157 | +/// Extract spill_count and spilled_bytes from EXPLAIN ANALYZE output. |
| 158 | +/// Metrics are formatted like: spill_count=5, spilled_bytes=20.9 MB |
| 159 | +fn extract_spill_metrics(explain_text: &str) -> (usize, usize) { |
| 160 | + let mut spill_count = 0; |
| 161 | + let mut spill_bytes = 0; |
| 162 | + |
| 163 | + for line in explain_text.lines() { |
| 164 | + if let Some(pos) = line.find("spill_count=") { |
| 165 | + let val_str = &line[pos + "spill_count=".len()..]; |
| 166 | + // Take until comma or bracket |
| 167 | + let end = val_str |
| 168 | + .find(|c: char| c == ',' || c == ']') |
| 169 | + .unwrap_or(val_str.len()); |
| 170 | + if let Some(v) = parse_human_size(&val_str[..end]) { |
| 171 | + spill_count += v; |
| 172 | + } |
| 173 | + } |
| 174 | + if let Some(pos) = line.find("spilled_bytes=") { |
| 175 | + let val_str = &line[pos + "spilled_bytes=".len()..]; |
| 176 | + let end = val_str |
| 177 | + .find(|c: char| c == ',' || c == ']') |
| 178 | + .unwrap_or(val_str.len()); |
| 179 | + if let Some(v) = parse_human_size(&val_str[..end]) { |
| 180 | + spill_bytes += v; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + (spill_count, spill_bytes) |
| 186 | +} |
| 187 | + |
| 188 | +/// Benchmark: high-cardinality GROUP BY with StringView columns and forced spilling. |
| 189 | +/// |
| 190 | +/// This exercises the hash aggregation spill path where IncrementalSortIterator |
| 191 | +/// produces chunks via take_record_batch. Without gc_view_arrays, each chunk |
| 192 | +/// retains references to all StringView data buffers from the parent batch, |
| 193 | +/// causing N× write amplification in the IPC spill writer. |
| 194 | +/// |
| 195 | +/// Run with: cargo test -p datafusion --test core_integration gc_view_benchmark -- --nocapture |
| 196 | +#[tokio::test] |
| 197 | +async fn bench_stringview_aggregate_spill() { |
| 198 | + let num_batches = 50; |
| 199 | + let rows_per_batch = 2000; |
| 200 | + let num_groups = 50_000; // High cardinality — many groups force spilling |
| 201 | + let pool_size_mb = 20; // Must be large enough for baseline (no gc) to succeed |
| 202 | + let n_runs = 3; |
| 203 | + |
| 204 | + eprintln!("\n=== StringView Aggregate Spill Benchmark ==="); |
| 205 | + eprintln!( |
| 206 | + "Config: {} batches × {} rows = {} total rows, {} groups, {} MB pool", |
| 207 | + num_batches, |
| 208 | + rows_per_batch, |
| 209 | + num_batches * rows_per_batch, |
| 210 | + num_groups, |
| 211 | + pool_size_mb |
| 212 | + ); |
| 213 | + |
| 214 | + let mut times = Vec::new(); |
| 215 | + let mut spill_counts = Vec::new(); |
| 216 | + let mut spill_bytes_vec = Vec::new(); |
| 217 | + |
| 218 | + for run in 0..n_runs { |
| 219 | + eprintln!("\nRun {}/{}:", run + 1, n_runs); |
| 220 | + let (time_ms, explain_text) = run_stringview_aggregate_spill_benchmark( |
| 221 | + pool_size_mb, |
| 222 | + num_batches, |
| 223 | + rows_per_batch, |
| 224 | + num_groups, |
| 225 | + ) |
| 226 | + .await; |
| 227 | + |
| 228 | + let (spill_count, spill_bytes) = extract_spill_metrics(&explain_text); |
| 229 | + |
| 230 | + eprintln!(" Query time: {:.1} ms", time_ms); |
| 231 | + eprintln!(" Spill count: {}", spill_count); |
| 232 | + eprintln!( |
| 233 | + " Spill bytes: {} ({:.2} MB)", |
| 234 | + spill_bytes, |
| 235 | + spill_bytes as f64 / 1024.0 / 1024.0 |
| 236 | + ); |
| 237 | + |
| 238 | + // Print aggregate-related lines from explain for verification |
| 239 | + for line in explain_text.lines() { |
| 240 | + if line.contains("Aggregate") || line.contains("spill") { |
| 241 | + eprintln!(" EXPLAIN: {}", line.trim()); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + times.push(time_ms); |
| 246 | + spill_counts.push(spill_count); |
| 247 | + spill_bytes_vec.push(spill_bytes); |
| 248 | + } |
| 249 | + |
| 250 | + // Compute statistics |
| 251 | + let mean_time: f64 = times.iter().sum::<f64>() / n_runs as f64; |
| 252 | + let mean_spill: f64 = |
| 253 | + spill_bytes_vec.iter().map(|&x| x as f64).sum::<f64>() / n_runs as f64; |
| 254 | + let mean_spill_count: f64 = |
| 255 | + spill_counts.iter().map(|&x| x as f64).sum::<f64>() / n_runs as f64; |
| 256 | + |
| 257 | + let stddev_time = if n_runs > 1 { |
| 258 | + (times |
| 259 | + .iter() |
| 260 | + .map(|x| (x - mean_time).powi(2)) |
| 261 | + .sum::<f64>() |
| 262 | + / (n_runs - 1) as f64) |
| 263 | + .sqrt() |
| 264 | + } else { |
| 265 | + 0.0 |
| 266 | + }; |
| 267 | + let stddev_spill = if n_runs > 1 { |
| 268 | + (spill_bytes_vec |
| 269 | + .iter() |
| 270 | + .map(|&x| (x as f64 - mean_spill).powi(2)) |
| 271 | + .sum::<f64>() |
| 272 | + / (n_runs - 1) as f64) |
| 273 | + .sqrt() |
| 274 | + } else { |
| 275 | + 0.0 |
| 276 | + }; |
| 277 | + |
| 278 | + eprintln!("\n=== RESULTS ({} runs) ===", n_runs); |
| 279 | + eprintln!( |
| 280 | + "Query time: {:.1} ± {:.1} ms (range: {:.1} - {:.1})", |
| 281 | + mean_time, |
| 282 | + stddev_time, |
| 283 | + times.iter().cloned().reduce(f64::min).unwrap(), |
| 284 | + times.iter().cloned().reduce(f64::max).unwrap() |
| 285 | + ); |
| 286 | + eprintln!("Spill count: {:.1}", mean_spill_count); |
| 287 | + eprintln!( |
| 288 | + "Spill bytes: {:.0} ± {:.0} ({:.2} ± {:.3} MB)", |
| 289 | + mean_spill, |
| 290 | + stddev_spill, |
| 291 | + mean_spill / 1024.0 / 1024.0, |
| 292 | + stddev_spill / 1024.0 / 1024.0, |
| 293 | + ); |
| 294 | + eprintln!("Individual spill bytes: {:?}", spill_bytes_vec); |
| 295 | +} |
0 commit comments