Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Added bench to read json
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jun 4, 2022
1 parent 7014e28 commit 696f915
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ harness = false
name = "write_json"
harness = false

[[bench]]
name = "read_json"
harness = false

[[bench]]
name = "slices_iterator"
harness = false
65 changes: 65 additions & 0 deletions benches/read_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use arrow2::array::Array;
use arrow2::datatypes::DataType;
use criterion::{criterion_group, criterion_main, Criterion};

use arrow2::io::json::{read, write};
use arrow2::util::bench_util::*;

fn prep(array: impl Array + 'static) -> (Vec<u8>, DataType) {
let mut data = vec![];
let blocks = write::Serializer::new(
vec![Ok(Box::new(array) as Box<dyn Array>)].into_iter(),
vec![],
);
// the operation of writing is IO-bounded.
write::write(&mut data, blocks).unwrap();

let dt = read::infer(&serde_json::from_slice(&data).unwrap()).unwrap();
(data, dt)
}

fn bench_read(data: &[u8], dt: &DataType) {
let json = serde_json::from_slice(data).unwrap();
read::deserialize(&json, dt.clone()).unwrap();
}

fn add_benchmark(c: &mut Criterion) {
(10..=20).step_by(2).for_each(|log2_size| {
let size = 2usize.pow(log2_size);

let array = create_primitive_array::<i32>(size, 0.1);

let (data, dt) = prep(array);

c.bench_function(&format!("read i32 2^{}", log2_size), |b| {
b.iter(|| bench_read(&data, &dt))
});

let array = create_primitive_array::<f64>(size, 0.1);

let (data, dt) = prep(array);

c.bench_function(&format!("read f64 2^{}", log2_size), |b| {
b.iter(|| bench_read(&data, &dt))
});

let array = create_string_array::<i32>(size, 10, 0.1, 42);

let (data, dt) = prep(array);

c.bench_function(&format!("read utf8 2^{}", log2_size), |b| {
b.iter(|| bench_read(&data, &dt))
});

let array = create_boolean_array(size, 0.1, 0.1);

let (data, dt) = prep(array);

c.bench_function(&format!("read bool 2^{}", log2_size), |b| {
b.iter(|| bench_read(&data, &dt))
});
})
}

criterion_group!(benches, add_benchmark);
criterion_main!(benches);

0 comments on commit 696f915

Please sign in to comment.