|
| 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::hint::black_box; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +use arrow::array::{ArrayRef, Float64Array}; |
| 22 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 23 | +use criterion::{BatchSize, Criterion, criterion_group, criterion_main}; |
| 24 | +use datafusion_expr::function::AccumulatorArgs; |
| 25 | +use datafusion_expr::{Accumulator, AggregateUDFImpl}; |
| 26 | +use datafusion_functions_aggregate::percentile_cont::PercentileCont; |
| 27 | +use datafusion_physical_expr::expressions::{col, lit}; |
| 28 | + |
| 29 | +const STEP_SIZE: usize = 128; |
| 30 | +const SLIDES_PER_ITER: usize = 32; |
| 31 | +const WINDOW_SIZES: [usize; 3] = [256, 4096, 16384]; |
| 32 | + |
| 33 | +fn prepare_accumulator() -> Box<dyn Accumulator> { |
| 34 | + let schema = Arc::new(Schema::new(vec![Field::new("f", DataType::Float64, true)])); |
| 35 | + let value_expr = col("f", &schema).unwrap(); |
| 36 | + let percentile_expr = lit(0.5_f64); |
| 37 | + let value_field = value_expr.return_field(&schema).unwrap(); |
| 38 | + let percentile_field = percentile_expr.return_field(&schema).unwrap(); |
| 39 | + let accumulator_args = AccumulatorArgs { |
| 40 | + return_field: Field::new("f", DataType::Float64, true).into(), |
| 41 | + schema: &schema, |
| 42 | + expr_fields: &[value_field, percentile_field], |
| 43 | + ignore_nulls: false, |
| 44 | + order_bys: &[], |
| 45 | + is_reversed: false, |
| 46 | + name: "percentile_cont(f, 0.5)", |
| 47 | + is_distinct: false, |
| 48 | + exprs: &[value_expr, percentile_expr], |
| 49 | + }; |
| 50 | + PercentileCont::new().accumulator(accumulator_args).unwrap() |
| 51 | +} |
| 52 | + |
| 53 | +fn stream_array(len: usize, null_stride: Option<usize>) -> ArrayRef { |
| 54 | + let values = (0..len) |
| 55 | + .map(|idx| { |
| 56 | + if null_stride.is_some_and(|stride| idx % stride == 0) { |
| 57 | + None |
| 58 | + } else { |
| 59 | + Some(idx as f64) |
| 60 | + } |
| 61 | + }) |
| 62 | + .collect::<Vec<_>>(); |
| 63 | + Arc::new(Float64Array::from(values)) as ArrayRef |
| 64 | +} |
| 65 | + |
| 66 | +/// Benchmark the sliding window cycle: retract + update + evaluate |
| 67 | +fn sliding_window_bench( |
| 68 | + c: &mut Criterion, |
| 69 | + name: &str, |
| 70 | + window_size: usize, |
| 71 | + stream: &ArrayRef, |
| 72 | +) { |
| 73 | + c.bench_function(name, |b| { |
| 74 | + b.iter_batched( |
| 75 | + || { |
| 76 | + let mut accumulator = prepare_accumulator(); |
| 77 | + let initial = stream.slice(0, window_size); |
| 78 | + accumulator |
| 79 | + .update_batch(std::slice::from_ref(&initial)) |
| 80 | + .unwrap(); |
| 81 | + accumulator |
| 82 | + }, |
| 83 | + |mut accumulator| { |
| 84 | + for slide in 0..SLIDES_PER_ITER { |
| 85 | + let offset = slide * STEP_SIZE; |
| 86 | + let retract = stream.slice(offset, STEP_SIZE); |
| 87 | + let update = stream.slice(offset + window_size, STEP_SIZE); |
| 88 | + accumulator |
| 89 | + .retract_batch(std::slice::from_ref(&retract)) |
| 90 | + .unwrap(); |
| 91 | + accumulator |
| 92 | + .update_batch(std::slice::from_ref(&update)) |
| 93 | + .unwrap(); |
| 94 | + black_box(accumulator.evaluate().unwrap()); |
| 95 | + } |
| 96 | + }, |
| 97 | + BatchSize::SmallInput, |
| 98 | + ) |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +fn percentile_cont_benchmark(c: &mut Criterion) { |
| 103 | + for window_size in WINDOW_SIZES { |
| 104 | + let stream_len = window_size + STEP_SIZE * SLIDES_PER_ITER; |
| 105 | + let stream_no_nulls = stream_array(stream_len, None); |
| 106 | + let stream_with_nulls = stream_array(stream_len, Some(10)); |
| 107 | + |
| 108 | + sliding_window_bench( |
| 109 | + c, |
| 110 | + &format!( |
| 111 | + "percentile_cont sliding_window f64 no_nulls window_size={window_size}" |
| 112 | + ), |
| 113 | + window_size, |
| 114 | + &stream_no_nulls, |
| 115 | + ); |
| 116 | + |
| 117 | + sliding_window_bench( |
| 118 | + c, |
| 119 | + &format!( |
| 120 | + "percentile_cont sliding_window f64 with_nulls window_size={window_size}" |
| 121 | + ), |
| 122 | + window_size, |
| 123 | + &stream_with_nulls, |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +criterion_group!(benches, percentile_cont_benchmark); |
| 129 | +criterion_main!(benches); |
0 commit comments