Skip to content

Commit

Permalink
add bench for decimal: builder and vec to array
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 committed Aug 15, 2022
1 parent 5e27d93 commit 94c9cca
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
55 changes: 54 additions & 1 deletion arrow/benches/array_from_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

#[macro_use]
extern crate criterion;

use criterion::Criterion;

extern crate arrow;

use arrow::array::*;
use arrow::util::decimal::Decimal256;
use num::BigInt;
use rand::Rng;
use std::{convert::TryFrom, sync::Arc};

fn array_from_vec(n: usize) {
Expand Down Expand Up @@ -72,6 +76,55 @@ fn struct_array_from_vec(
);
}

fn decimal128_array_from_vec(array: &[Option<i128>]) {
criterion::black_box(
array
.iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(34, 2)
.unwrap(),
);
}

fn decimal256_array_from_vec() {
// bench decimal256array
// create option<into<decimal256>> array
let size = 1 << 10;
let mut array = vec![];
let mut rng = rand::thread_rng();
for _ in 0..size {
let decimal =
Decimal256::from(BigInt::from(rng.gen_range::<i128, _>(0..9999999999999)));
array.push(Some(decimal));
}
criterion::black_box(
array
.into_iter()
.collect::<Decimal256Array>()
.with_precision_and_scale(70, 2)
.unwrap(),
);
}

fn decimal_benchmark(c: &mut Criterion) {
// bench decimal128 array
// create option<i128> array
let size: usize = 1 << 15;
let mut rng = rand::thread_rng();
let mut array = vec![];
for _ in 0..size {
array.push(Some(rng.gen_range::<i128, _>(0..9999999999)));
}
c.bench_function("decimal128_array_from_vec 32768", |b| {
b.iter(|| decimal128_array_from_vec(array.as_slice()))
});

// bench decimal256 array
c.bench_function("decimal256_array_from_vec 32768", |b| {
b.iter(|| decimal256_array_from_vec())
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("array_from_vec 128", |b| b.iter(|| array_from_vec(128)));
c.bench_function("array_from_vec 256", |b| b.iter(|| array_from_vec(256)));
Expand Down Expand Up @@ -108,5 +161,5 @@ fn criterion_benchmark(c: &mut Criterion) {
});
}

criterion_group!(benches, criterion_benchmark);
criterion_group!(benches, criterion_benchmark, decimal_benchmark);
criterion_main!(benches);
38 changes: 37 additions & 1 deletion arrow/benches/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ extern crate rand;
use std::mem::size_of;

use criterion::*;
use num::BigInt;
use rand::distributions::Standard;

use arrow::array::*;
use arrow::util::decimal::Decimal256;
use arrow::util::test_util::seedable_rng;
use rand::Rng;

Expand Down Expand Up @@ -106,11 +108,45 @@ fn bench_string(c: &mut Criterion) {
group.finish();
}

fn bench_decimal128(c: &mut Criterion) {
c.bench_function("bench_decimal128_builder", |b| {
b.iter(|| {
let mut rng = rand::thread_rng();
let mut decimal_builder = Decimal128Builder::new(BATCH_SIZE, 38, 0);
for _ in 0..BATCH_SIZE {
decimal_builder
.append_value(rng.gen_range::<i128, _>(0..9999999999))
.unwrap();
}
black_box(decimal_builder.finish());
})
});
}

fn bench_decimal256(c: &mut Criterion) {
c.bench_function("bench_decimal128_builder", |b| {
b.iter(|| {
let mut rng = rand::thread_rng();
let mut decimal_builder = Decimal256Builder::new(BATCH_SIZE, 76, 10);
for _ in 0..BATCH_SIZE {
decimal_builder
.append_value(&Decimal256::from(BigInt::from(
rng.gen_range::<i128, _>(0..99999999999),
)))
.unwrap()
}
black_box(decimal_builder.finish());
})
});
}

criterion_group!(
benches,
bench_primitive,
bench_primitive_nulls,
bench_bool,
bench_string
bench_string,
bench_decimal128,
bench_decimal256,
);
criterion_main!(benches);

0 comments on commit 94c9cca

Please sign in to comment.