|
| 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 | +extern crate criterion; |
| 19 | +mod helper; |
| 20 | + |
| 21 | +use arrow::datatypes::{DataType, Field}; |
| 22 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 23 | +use datafusion_expr::ScalarFunctionArgs; |
| 24 | +use helper::gen_string_array; |
| 25 | + |
| 26 | +fn criterion_benchmark(c: &mut Criterion) { |
| 27 | + let ascii = datafusion_functions::string::ascii(); |
| 28 | + |
| 29 | + // All benches are single batch run with 8192 rows |
| 30 | + const N_ROWS: usize = 8192; |
| 31 | + const STR_LEN: usize = 16; |
| 32 | + const NULL_DENSITY: f32 = 0.1; |
| 33 | + const UTF8_DENSITY_OF_ALL_ASCII: f32 = 0.0; |
| 34 | + const NORMAL_UTF8_DENSITY: f32 = 0.8; |
| 35 | + |
| 36 | + // StringArray ASCII only |
| 37 | + let args_string_ascii = gen_string_array( |
| 38 | + N_ROWS, |
| 39 | + STR_LEN, |
| 40 | + NULL_DENSITY, |
| 41 | + UTF8_DENSITY_OF_ALL_ASCII, |
| 42 | + false, |
| 43 | + ); |
| 44 | + c.bench_function("ascii/string_ascii_only", |b| { |
| 45 | + b.iter(|| { |
| 46 | + black_box(ascii.invoke_with_args(ScalarFunctionArgs { |
| 47 | + args: args_string_ascii.clone(), |
| 48 | + arg_fields: vec![&Field::new( |
| 49 | + "a", |
| 50 | + args_string_ascii[0].data_type(), |
| 51 | + true, |
| 52 | + )], |
| 53 | + number_rows: N_ROWS, |
| 54 | + return_field: &Field::new("f", DataType::Utf8, true), |
| 55 | + })) |
| 56 | + }) |
| 57 | + }); |
| 58 | + |
| 59 | + // StringArray UTF8 |
| 60 | + let args_string_utf8 = |
| 61 | + gen_string_array(N_ROWS, STR_LEN, NULL_DENSITY, NORMAL_UTF8_DENSITY, false); |
| 62 | + c.bench_function("ascii/string_utf8", |b| { |
| 63 | + b.iter(|| { |
| 64 | + black_box(ascii.invoke_with_args(ScalarFunctionArgs { |
| 65 | + args: args_string_utf8.clone(), |
| 66 | + arg_fields: vec![&Field::new("a", args_string_utf8[0].data_type(), true)], |
| 67 | + number_rows: N_ROWS, |
| 68 | + return_field: &Field::new("f", DataType::Utf8, true), |
| 69 | + })) |
| 70 | + }) |
| 71 | + }); |
| 72 | + |
| 73 | + // StringViewArray ASCII only |
| 74 | + let args_string_view_ascii = gen_string_array( |
| 75 | + N_ROWS, |
| 76 | + STR_LEN, |
| 77 | + NULL_DENSITY, |
| 78 | + UTF8_DENSITY_OF_ALL_ASCII, |
| 79 | + true, |
| 80 | + ); |
| 81 | + c.bench_function("ascii/string_view_ascii_only", |b| { |
| 82 | + b.iter(|| { |
| 83 | + black_box(ascii.invoke_with_args(ScalarFunctionArgs { |
| 84 | + args: args_string_view_ascii.clone(), |
| 85 | + arg_fields: vec![&Field::new( |
| 86 | + "a", |
| 87 | + args_string_view_ascii[0].data_type(), |
| 88 | + true, |
| 89 | + )], |
| 90 | + number_rows: N_ROWS, |
| 91 | + return_field: &Field::new("f", DataType::Utf8, true), |
| 92 | + })) |
| 93 | + }) |
| 94 | + }); |
| 95 | + |
| 96 | + // StringViewArray UTF8 |
| 97 | + let args_string_view_utf8 = |
| 98 | + gen_string_array(N_ROWS, STR_LEN, NULL_DENSITY, NORMAL_UTF8_DENSITY, true); |
| 99 | + c.bench_function("ascii/string_view_utf8", |b| { |
| 100 | + b.iter(|| { |
| 101 | + black_box(ascii.invoke_with_args(ScalarFunctionArgs { |
| 102 | + args: args_string_view_utf8.clone(), |
| 103 | + arg_fields: vec![&Field::new( |
| 104 | + "a", |
| 105 | + args_string_view_utf8[0].data_type(), |
| 106 | + true, |
| 107 | + )], |
| 108 | + number_rows: N_ROWS, |
| 109 | + return_field: &Field::new("f", DataType::Utf8, true), |
| 110 | + })) |
| 111 | + }) |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +criterion_group!(benches, criterion_benchmark); |
| 116 | +criterion_main!(benches); |
0 commit comments