-
Notifications
You must be signed in to change notification settings - Fork 860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Benchmarks for Arrow IPC reader #7091
Draft
alamb
wants to merge
10
commits into
apache:main
Choose a base branch
from
alamb:alamb/ipc_read_benchmark
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7025df2
Add benchmarks for Arrow IPC writer
andygrove e377abe
Add benchmarks for Arrow IPC writer
andygrove 1c999a6
reuse target buffer
alamb 57e4838
rename, etc
alamb 92bdea3
Add compression type
alamb 3d7d396
update
alamb 96b454f
Add ipc reader benchmark
alamb 7527f8e
Add mmap example
alamb 8bc23a1
fmt
alamb 6cd7e5d
Update arrow-ipc/benches/ipc_reader.rs
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow_array::builder::{Date32Builder, Decimal128Builder, Int32Builder}; | ||
use arrow_array::{builder::StringBuilder, RecordBatch}; | ||
use arrow_buffer::Buffer; | ||
use arrow_ipc::convert::fb_to_schema; | ||
use arrow_ipc::reader::{read_footer_length, FileDecoder, FileReader, StreamReader}; | ||
use arrow_ipc::writer::{FileWriter, IpcWriteOptions, StreamWriter}; | ||
use arrow_ipc::{root_as_footer, Block, CompressionType}; | ||
use arrow_schema::{DataType, Field, Schema}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use std::io::Cursor; | ||
use std::sync::Arc; | ||
use tempfile::tempdir; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("arrow_ipc_reader"); | ||
|
||
group.bench_function("StreamReader/read_10", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
let mut writer = StreamWriter::try_new(&mut buffer, batch.schema().as_ref()).unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
|
||
b.iter(move || { | ||
let projection = None; | ||
let mut reader = StreamReader::try_new(buffer.as_slice(), projection).unwrap(); | ||
for _ in 0..10 { | ||
reader.next().unwrap().unwrap(); | ||
} | ||
assert!(reader.next().is_none()); | ||
}) | ||
}); | ||
|
||
group.bench_function("StreamReader/read_10/zstd", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
let options = IpcWriteOptions::default() | ||
.try_with_compression(Some(CompressionType::ZSTD)) | ||
.unwrap(); | ||
let mut writer = | ||
StreamWriter::try_new_with_options(&mut buffer, batch.schema().as_ref(), options) | ||
.unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
|
||
b.iter(move || { | ||
let projection = None; | ||
let mut reader = StreamReader::try_new(buffer.as_slice(), projection).unwrap(); | ||
for _ in 0..10 { | ||
reader.next().unwrap().unwrap(); | ||
} | ||
assert!(reader.next().is_none()); | ||
}) | ||
}); | ||
|
||
group.bench_function("FileReader/read_10", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
let mut writer = FileWriter::try_new(&mut buffer, batch.schema().as_ref()).unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
|
||
b.iter(move || { | ||
let projection = None; | ||
let cursor = Cursor::new(buffer.as_slice()); | ||
let mut reader = FileReader::try_new(cursor, projection).unwrap(); | ||
for _ in 0..10 { | ||
reader.next().unwrap().unwrap(); | ||
} | ||
assert!(reader.next().is_none()); | ||
}) | ||
}); | ||
|
||
group.bench_function("FileReader/read_10/mmap", |b| { | ||
let batch = create_batch(8192, true); | ||
// write to an actual file | ||
let dir = tempdir().unwrap(); | ||
let path = dir.path().join("test.arrow"); | ||
let file = std::fs::File::create(&path).unwrap(); | ||
let mut writer = FileWriter::try_new(file, batch.schema().as_ref()).unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
|
||
b.iter(move || { | ||
let ipc_file = std::fs::File::open(&path).expect("failed to open file"); | ||
let mmap = unsafe { memmap2::Mmap::map(&ipc_file).expect("failed to mmap file") }; | ||
|
||
// Convert the mmap region to an Arrow `Buffer` to back the arrow arrays. | ||
let bytes = bytes::Bytes::from_owner(mmap); | ||
let buffer = Buffer::from(bytes); | ||
let decoder = IPCBufferDecoder::new(buffer); | ||
assert_eq!(decoder.num_batches(), 10); | ||
|
||
for i in 0..decoder.num_batches() { | ||
decoder.get_batch(i); | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
// copied from the zero_copy_ipc example. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did want to benchmark using mmap as I think it is an important usecase Maybe we should contemplate making this |
||
// should we move this to an actual API? | ||
/// Wrapper around the example in the `FileDecoder` which handles the | ||
/// low level interaction with the Arrow IPC format. | ||
struct IPCBufferDecoder { | ||
/// Memory (or memory mapped) Buffer with the data | ||
buffer: Buffer, | ||
/// Decoder that reads Arrays that refers to the underlying buffers | ||
decoder: FileDecoder, | ||
/// Location of the batches within the buffer | ||
batches: Vec<Block>, | ||
} | ||
|
||
impl IPCBufferDecoder { | ||
fn new(buffer: Buffer) -> Self { | ||
let trailer_start = buffer.len() - 10; | ||
let footer_len = read_footer_length(buffer[trailer_start..].try_into().unwrap()).unwrap(); | ||
let footer = root_as_footer(&buffer[trailer_start - footer_len..trailer_start]).unwrap(); | ||
|
||
let schema = fb_to_schema(footer.schema().unwrap()); | ||
|
||
let mut decoder = FileDecoder::new(Arc::new(schema), footer.version()); | ||
|
||
// Read dictionaries | ||
for block in footer.dictionaries().iter().flatten() { | ||
let block_len = block.bodyLength() as usize + block.metaDataLength() as usize; | ||
let data = buffer.slice_with_length(block.offset() as _, block_len); | ||
decoder.read_dictionary(block, &data).unwrap(); | ||
} | ||
|
||
// convert to Vec from the flatbuffers Vector to avoid having a direct dependency on flatbuffers | ||
let batches = footer | ||
.recordBatches() | ||
.map(|b| b.iter().copied().collect()) | ||
.unwrap_or_default(); | ||
|
||
Self { | ||
buffer, | ||
decoder, | ||
batches, | ||
} | ||
} | ||
|
||
fn num_batches(&self) -> usize { | ||
self.batches.len() | ||
} | ||
|
||
fn get_batch(&self, i: usize) -> RecordBatch { | ||
let block = &self.batches[i]; | ||
let block_len = block.bodyLength() as usize + block.metaDataLength() as usize; | ||
let data = self | ||
.buffer | ||
.slice_with_length(block.offset() as _, block_len); | ||
self.decoder | ||
.read_record_batch(block, &data) | ||
.unwrap() | ||
.unwrap() | ||
} | ||
} | ||
|
||
fn create_batch(num_rows: usize, allow_nulls: bool) -> RecordBatch { | ||
let schema = Arc::new(Schema::new(vec![ | ||
Field::new("c0", DataType::Int32, true), | ||
Field::new("c1", DataType::Utf8, true), | ||
Field::new("c2", DataType::Date32, true), | ||
Field::new("c3", DataType::Decimal128(11, 2), true), | ||
])); | ||
let mut a = Int32Builder::new(); | ||
let mut b = StringBuilder::new(); | ||
let mut c = Date32Builder::new(); | ||
let mut d = Decimal128Builder::new() | ||
.with_precision_and_scale(11, 2) | ||
.unwrap(); | ||
for i in 0..num_rows { | ||
a.append_value(i as i32); | ||
c.append_value(i as i32); | ||
d.append_value((i * 1000000) as i128); | ||
if allow_nulls && i % 10 == 0 { | ||
b.append_null(); | ||
} else { | ||
b.append_value(format!("this is string number {i}")); | ||
} | ||
} | ||
let a = a.finish(); | ||
let b = b.finish(); | ||
let c = c.finish(); | ||
let d = d.finish(); | ||
RecordBatch::try_new( | ||
schema.clone(), | ||
vec![Arc::new(a), Arc::new(b), Arc::new(c), Arc::new(d)], | ||
) | ||
.unwrap() | ||
} | ||
|
||
fn config() -> Criterion { | ||
Criterion::default() | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = config(); | ||
targets = criterion_benchmark | ||
} | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow_array::builder::{Date32Builder, Decimal128Builder, Int32Builder}; | ||
use arrow_array::{builder::StringBuilder, RecordBatch}; | ||
use arrow_ipc::writer::{FileWriter, IpcWriteOptions, StreamWriter}; | ||
use arrow_ipc::CompressionType; | ||
use arrow_schema::{DataType, Field, Schema}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use std::sync::Arc; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("arrow_ipc_stream_writer"); | ||
|
||
group.bench_function("StreamWriter/write_10", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
b.iter(move || { | ||
buffer.clear(); | ||
let mut writer = StreamWriter::try_new(&mut buffer, batch.schema().as_ref()).unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
}) | ||
}); | ||
|
||
group.bench_function("StreamWriter/write_10/zstd", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
b.iter(move || { | ||
buffer.clear(); | ||
let options = IpcWriteOptions::default() | ||
.try_with_compression(Some(CompressionType::ZSTD)) | ||
.unwrap(); | ||
let mut writer = | ||
StreamWriter::try_new_with_options(&mut buffer, batch.schema().as_ref(), options) | ||
.unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
}) | ||
}); | ||
|
||
group.bench_function("FileWriter/write_10", |b| { | ||
let batch = create_batch(8192, true); | ||
let mut buffer = Vec::with_capacity(2 * 1024 * 1024); | ||
b.iter(move || { | ||
buffer.clear(); | ||
let mut writer = FileWriter::try_new(&mut buffer, batch.schema().as_ref()).unwrap(); | ||
for _ in 0..10 { | ||
writer.write(&batch).unwrap(); | ||
} | ||
writer.finish().unwrap(); | ||
}) | ||
}); | ||
} | ||
|
||
fn create_batch(num_rows: usize, allow_nulls: bool) -> RecordBatch { | ||
let schema = Arc::new(Schema::new(vec![ | ||
Field::new("c0", DataType::Int32, true), | ||
Field::new("c1", DataType::Utf8, true), | ||
Field::new("c2", DataType::Date32, true), | ||
Field::new("c3", DataType::Decimal128(11, 2), true), | ||
])); | ||
let mut a = Int32Builder::new(); | ||
let mut b = StringBuilder::new(); | ||
let mut c = Date32Builder::new(); | ||
let mut d = Decimal128Builder::new() | ||
.with_precision_and_scale(11, 2) | ||
.unwrap(); | ||
for i in 0..num_rows { | ||
a.append_value(i as i32); | ||
c.append_value(i as i32); | ||
d.append_value((i * 1000000) as i128); | ||
if allow_nulls && i % 10 == 0 { | ||
b.append_null(); | ||
} else { | ||
b.append_value(format!("this is string number {i}")); | ||
} | ||
} | ||
let a = a.finish(); | ||
let b = b.finish(); | ||
let c = c.finish(); | ||
let d = d.finish(); | ||
RecordBatch::try_new( | ||
schema.clone(), | ||
vec![Arc::new(a), Arc::new(b), Arc::new(c), Arc::new(d)], | ||
) | ||
.unwrap() | ||
} | ||
|
||
fn config() -> Criterion { | ||
Criterion::default() | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = config(); | ||
targets = criterion_benchmark | ||
} | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The benchmark for mmap shows even more time spent in validating
![Screenshot 2025-02-06 at 3 50 14 PM](https://private-user-images.githubusercontent.com/490673/410641538-16946fac-c306-4326-8d78-d5df8c3db51f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5NjU3MzIsIm5iZiI6MTczODk2NTQzMiwicGF0aCI6Ii80OTA2NzMvNDEwNjQxNTM4LTE2OTQ2ZmFjLWMzMDYtNDMyNi04ZDc4LWQ1ZGY4YzNkYjUxZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwN1QyMTU3MTJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zMGI4OTcyOTM0OGJmNDcwMjViOWI0MmJlMGFmOWE3NDM5Y2U3NTVjNGEzNWU5M2RjZGMyZDEzN2M0N2I1YWMwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.KYmKlHG5W0L9tbtwOvKRpEMslxLdIaqZ3u9Ygh8V93E)
flamegraph