-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add csv loading benchmarks. (#13544)
* Add csv loading benchmarks. * Fix fmt. * Fix clippy.
- Loading branch information
Showing
6 changed files
with
184 additions
and
7 deletions.
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
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,81 @@ | ||
// 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. | ||
|
||
#[macro_use] | ||
extern crate criterion; | ||
extern crate arrow; | ||
extern crate datafusion; | ||
|
||
mod data_utils; | ||
use crate::criterion::Criterion; | ||
use datafusion::error::Result; | ||
use datafusion::execution::context::SessionContext; | ||
use datafusion::prelude::CsvReadOptions; | ||
use datafusion::test_util::csv::TestCsvFile; | ||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use test_utils::AccessLogGenerator; | ||
use tokio::runtime::Runtime; | ||
|
||
fn load_csv(ctx: Arc<Mutex<SessionContext>>, path: &str, options: CsvReadOptions) { | ||
let rt = Runtime::new().unwrap(); | ||
let df = rt.block_on(ctx.lock().read_csv(path, options)).unwrap(); | ||
criterion::black_box(rt.block_on(df.collect()).unwrap()); | ||
} | ||
|
||
fn create_context() -> Result<Arc<Mutex<SessionContext>>> { | ||
let ctx = SessionContext::new(); | ||
Ok(Arc::new(Mutex::new(ctx))) | ||
} | ||
|
||
fn generate_test_file() -> TestCsvFile { | ||
let write_location = std::env::current_dir() | ||
.unwrap() | ||
.join("benches") | ||
.join("data"); | ||
|
||
// Make sure the write directory exists. | ||
std::fs::create_dir_all(&write_location).unwrap(); | ||
let file_path = write_location.join("logs.csv"); | ||
|
||
let generator = AccessLogGenerator::new().with_include_nulls(true); | ||
let num_batches = 2; | ||
TestCsvFile::try_new(file_path.clone(), generator.take(num_batches as usize)) | ||
.expect("Failed to create test file.") | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let ctx = create_context().unwrap(); | ||
let test_file = generate_test_file(); | ||
|
||
let mut group = c.benchmark_group("load csv testing"); | ||
group.measurement_time(Duration::from_secs(20)); | ||
|
||
group.bench_function("default csv read options", |b| { | ||
b.iter(|| { | ||
load_csv( | ||
ctx.clone(), | ||
test_file.path().to_str().unwrap(), | ||
CsvReadOptions::default(), | ||
) | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, 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,69 @@ | ||
// 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. | ||
|
||
//! Helpers for writing csv files and reading them back | ||
use std::fs::File; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
use crate::arrow::{datatypes::SchemaRef, record_batch::RecordBatch}; | ||
use crate::error::Result; | ||
|
||
use arrow::csv::WriterBuilder; | ||
|
||
/// a CSV file that has been created for testing. | ||
pub struct TestCsvFile { | ||
path: PathBuf, | ||
schema: SchemaRef, | ||
} | ||
|
||
impl TestCsvFile { | ||
/// Creates a new csv file at the specified location | ||
pub fn try_new( | ||
path: PathBuf, | ||
batches: impl IntoIterator<Item = RecordBatch>, | ||
) -> Result<Self> { | ||
let file = File::create(&path).unwrap(); | ||
let builder = WriterBuilder::new().with_header(true); | ||
let mut writer = builder.build(file); | ||
|
||
let mut batches = batches.into_iter(); | ||
let first_batch = batches.next().expect("need at least one record batch"); | ||
let schema = first_batch.schema(); | ||
|
||
let mut num_rows = 0; | ||
for batch in batches { | ||
writer.write(&batch)?; | ||
num_rows += batch.num_rows(); | ||
} | ||
|
||
println!("Generated test dataset with {num_rows} rows"); | ||
|
||
Ok(Self { path, schema }) | ||
} | ||
|
||
/// The schema of this csv file | ||
pub fn schema(&self) -> SchemaRef { | ||
Arc::clone(&self.schema) | ||
} | ||
|
||
/// The path to the csv file | ||
pub fn path(&self) -> &std::path::Path { | ||
self.path.as_path() | ||
} | ||
} |
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