|
| 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 arrow::array::StringArray; |
| 19 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 20 | +use datafusion::physical_plan::ColumnarValue; |
| 21 | +use datafusion_comet_spark_expr::spark_to_time; |
| 22 | +use std::sync::Arc; |
| 23 | + |
| 24 | +fn criterion_benchmark(c: &mut Criterion) { |
| 25 | + let mut group = c.benchmark_group("to_time"); |
| 26 | + |
| 27 | + let hh_mm = create_string_array(10000, |i| format!("{}:{:02}", i % 24, i % 60)); |
| 28 | + group.bench_function("hh_mm", |b| { |
| 29 | + b.iter(|| spark_to_time(std::slice::from_ref(&hh_mm), true).unwrap()); |
| 30 | + }); |
| 31 | + |
| 32 | + let hh_mm_ss = create_string_array(10000, |i| { |
| 33 | + format!("{}:{:02}:{:02}", i % 24, i % 60, i % 60) |
| 34 | + }); |
| 35 | + group.bench_function("hh_mm_ss", |b| { |
| 36 | + b.iter(|| spark_to_time(std::slice::from_ref(&hh_mm_ss), true).unwrap()); |
| 37 | + }); |
| 38 | + |
| 39 | + let fractional = create_string_array(10000, |i| { |
| 40 | + format!("{}:{:02}:{:02}.{:06}", i % 24, i % 60, i % 60, i * 7 % 1000000) |
| 41 | + }); |
| 42 | + group.bench_function("fractional", |b| { |
| 43 | + b.iter(|| spark_to_time(std::slice::from_ref(&fractional), true).unwrap()); |
| 44 | + }); |
| 45 | + |
| 46 | + let am_pm = create_string_array(10000, |i| { |
| 47 | + let hour = (i % 12) + 1; |
| 48 | + let suffix = if i % 2 == 0 { "AM" } else { "PM" }; |
| 49 | + format!("{}:{:02}:{:02} {}", hour, i % 60, i % 60, suffix) |
| 50 | + }); |
| 51 | + group.bench_function("am_pm", |b| { |
| 52 | + b.iter(|| spark_to_time(std::slice::from_ref(&am_pm), true).unwrap()); |
| 53 | + }); |
| 54 | + |
| 55 | + group.finish(); |
| 56 | +} |
| 57 | + |
| 58 | +fn create_string_array(size: usize, f: impl Fn(usize) -> String) -> ColumnarValue { |
| 59 | + let values: Vec<String> = (0..size).map(&f).collect(); |
| 60 | + let array = StringArray::from(values); |
| 61 | + ColumnarValue::Array(Arc::new(array)) |
| 62 | +} |
| 63 | + |
| 64 | +fn config() -> Criterion { |
| 65 | + Criterion::default() |
| 66 | +} |
| 67 | + |
| 68 | +criterion_group! { |
| 69 | + name = benches; |
| 70 | + config = config(); |
| 71 | + targets = criterion_benchmark |
| 72 | +} |
| 73 | +criterion_main!(benches); |
0 commit comments