Skip to content

Commit a8f3bdc

Browse files
committed
add crypto function benchmark
1 parent 058bcb0 commit a8f3bdc

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

datafusion/functions/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,8 @@ required-features = ["unicode_expressions"]
254254
harness = false
255255
name = "find_in_set"
256256
required-features = ["unicode_expressions"]
257+
258+
[[bench]]
259+
harness = false
260+
name = "crypto"
261+
required-features = ["crypto_expressions"]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
20+
use arrow::datatypes::{DataType, Field};
21+
use arrow::util::bench_util::create_string_array_with_len;
22+
use criterion::{Criterion, criterion_group, criterion_main};
23+
use datafusion_common::config::ConfigOptions;
24+
use datafusion_expr::ScalarFunctionArgs;
25+
use datafusion_expr_common::columnar_value::ColumnarValue;
26+
use datafusion_functions::crypto;
27+
use std::hint::black_box;
28+
use std::sync::Arc;
29+
30+
fn criterion_benchmark(c: &mut Criterion) {
31+
let crypto = vec![
32+
crypto::md5(),
33+
crypto::sha224(),
34+
crypto::sha256(),
35+
crypto::sha384(),
36+
crypto::sha512(),
37+
];
38+
let config_options = Arc::new(ConfigOptions::default());
39+
40+
for func in crypto {
41+
let size = 1024;
42+
let arr_args = vec![ColumnarValue::Array(Arc::new(
43+
create_string_array_with_len::<i32>(size, 0.2, 32),
44+
))];
45+
c.bench_function(&format!("{}_array", func.name()), |b| {
46+
b.iter(|| {
47+
let args_cloned = arr_args.clone();
48+
black_box(func.invoke_with_args(ScalarFunctionArgs {
49+
args: args_cloned,
50+
arg_fields: vec![Field::new("a", DataType::Utf8, true).into()],
51+
number_rows: size,
52+
return_field: Field::new("f", DataType::Utf8, true).into(),
53+
config_options: Arc::clone(&config_options),
54+
}))
55+
})
56+
});
57+
58+
let scalar_args = vec![ColumnarValue::Scalar("test_string".into())];
59+
c.bench_function(&format!("{}_scalar", func.name()), |b| {
60+
b.iter(|| {
61+
let args_cloned = scalar_args.clone();
62+
black_box(func.invoke_with_args(ScalarFunctionArgs {
63+
args: args_cloned,
64+
arg_fields: vec![Field::new("a", DataType::Utf8, true).into()],
65+
number_rows: 1,
66+
return_field: Field::new("f", DataType::Utf8, true).into(),
67+
config_options: Arc::clone(&config_options),
68+
}))
69+
})
70+
});
71+
}
72+
}
73+
74+
criterion_group!(benches, criterion_benchmark);
75+
criterion_main!(benches);

datafusion/functions/src/crypto/basic.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,14 @@ macro_rules! digest_to_array {
173173
($METHOD:ident, $INPUT:expr) => {{
174174
let binary_array: BinaryArray = $INPUT
175175
.iter()
176-
.map(|x| {
177-
x.map(|x| {
178-
let mut digest = $METHOD::default();
179-
digest.update(x);
180-
digest.finalize()
181-
})
182-
})
176+
.map(|x| x.map(|x| $METHOD::digest(x)))
183177
.collect();
184178
Arc::new(binary_array)
185179
}};
186180
}
187181

188182
macro_rules! digest_to_scalar {
189-
($METHOD: ident, $INPUT:expr) => {{
190-
ScalarValue::Binary($INPUT.as_ref().map(|v| {
191-
let mut digest = $METHOD::default();
192-
digest.update(v);
193-
digest.finalize().as_slice().to_vec()
194-
}))
195-
}};
183+
($METHOD: ident, $INPUT:expr) => {{ ScalarValue::Binary($INPUT.map(|v| $METHOD::digest(v).as_slice().to_vec())) }};
196184
}
197185

198186
impl DigestAlgorithm {

0 commit comments

Comments
 (0)