|
| 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::{Array, ArrayRef, StringArray}; |
| 22 | +use arrow::datatypes::Field; |
| 23 | +use criterion::{Criterion, criterion_group, criterion_main}; |
| 24 | +use datafusion_common::config::ConfigOptions; |
| 25 | +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs}; |
| 26 | +use datafusion_functions::datetime::to_time; |
| 27 | +use rand::Rng; |
| 28 | +use rand::rngs::ThreadRng; |
| 29 | + |
| 30 | +fn random_time_string(rng: &mut ThreadRng) -> String { |
| 31 | + format!( |
| 32 | + "{:02}:{:02}:{:02}.{:06}", |
| 33 | + rng.random_range(0..24u32), |
| 34 | + rng.random_range(0..60u32), |
| 35 | + rng.random_range(0..60u32), |
| 36 | + rng.random_range(0..1_000_000u32), |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +fn time_strings(rng: &mut ThreadRng) -> StringArray { |
| 41 | + let strings: Vec<String> = (0..100_000).map(|_| random_time_string(rng)).collect(); |
| 42 | + StringArray::from(strings) |
| 43 | +} |
| 44 | + |
| 45 | +fn time_strings_with_nulls(rng: &mut ThreadRng) -> StringArray { |
| 46 | + let values: Vec<Option<String>> = (0..100_000) |
| 47 | + .map(|_| { |
| 48 | + if rng.random_range(0..10u32) == 0 { |
| 49 | + None |
| 50 | + } else { |
| 51 | + Some(random_time_string(rng)) |
| 52 | + } |
| 53 | + }) |
| 54 | + .collect(); |
| 55 | + StringArray::from(values) |
| 56 | +} |
| 57 | + |
| 58 | +fn bench_to_time(c: &mut Criterion, name: &str, array: ArrayRef) { |
| 59 | + let batch_len = array.len(); |
| 60 | + let input = ColumnarValue::Array(array); |
| 61 | + let udf = to_time(); |
| 62 | + let return_type = udf.return_type(&[input.data_type()]).unwrap(); |
| 63 | + let return_field = Arc::new(Field::new("f", return_type, true)); |
| 64 | + let arg_fields = vec![Field::new("a", input.data_type(), true).into()]; |
| 65 | + let config_options = Arc::new(ConfigOptions::default()); |
| 66 | + |
| 67 | + c.bench_function(name, |b| { |
| 68 | + b.iter(|| { |
| 69 | + black_box( |
| 70 | + udf.invoke_with_args(ScalarFunctionArgs { |
| 71 | + args: vec![input.clone()], |
| 72 | + arg_fields: arg_fields.clone(), |
| 73 | + number_rows: batch_len, |
| 74 | + return_field: Arc::clone(&return_field), |
| 75 | + config_options: Arc::clone(&config_options), |
| 76 | + }) |
| 77 | + .expect("to_time should work on valid values"), |
| 78 | + ) |
| 79 | + }) |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +fn criterion_benchmark(c: &mut Criterion) { |
| 84 | + let mut rng = rand::rng(); |
| 85 | + bench_to_time(c, "to_time_no_nulls_100k", Arc::new(time_strings(&mut rng))); |
| 86 | + bench_to_time( |
| 87 | + c, |
| 88 | + "to_time_10pct_nulls_100k", |
| 89 | + Arc::new(time_strings_with_nulls(&mut rng)), |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +criterion_group!(benches, criterion_benchmark); |
| 94 | +criterion_main!(benches); |
0 commit comments