|
| 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 | +#[macro_use] |
| 19 | +extern crate criterion; |
| 20 | +extern crate arrow; |
| 21 | +extern crate datafusion; |
| 22 | + |
| 23 | +mod data_utils; |
| 24 | +use crate::criterion::Criterion; |
| 25 | +use datafusion::error::Result; |
| 26 | +use datafusion::execution::context::SessionContext; |
| 27 | +use datafusion::prelude::CsvReadOptions; |
| 28 | +use datafusion::test_util::csv::TestCsvFile; |
| 29 | +use parking_lot::Mutex; |
| 30 | +use std::sync::Arc; |
| 31 | +use std::time::Duration; |
| 32 | +use test_utils::AccessLogGenerator; |
| 33 | +use tokio::runtime::Runtime; |
| 34 | + |
| 35 | +fn load_csv(ctx: Arc<Mutex<SessionContext>>, path: &str, options: CsvReadOptions) { |
| 36 | + let rt = Runtime::new().unwrap(); |
| 37 | + let df = rt.block_on(ctx.lock().read_csv(path, options)).unwrap(); |
| 38 | + criterion::black_box(rt.block_on(df.collect()).unwrap()); |
| 39 | +} |
| 40 | + |
| 41 | +fn create_context() -> Result<Arc<Mutex<SessionContext>>> { |
| 42 | + let ctx = SessionContext::new(); |
| 43 | + Ok(Arc::new(Mutex::new(ctx))) |
| 44 | +} |
| 45 | + |
| 46 | +fn generate_test_file() -> TestCsvFile { |
| 47 | + let write_location = std::env::current_dir() |
| 48 | + .unwrap() |
| 49 | + .join("benches") |
| 50 | + .join("data"); |
| 51 | + |
| 52 | + // Make sure the write directory exists. |
| 53 | + std::fs::create_dir_all(&write_location).unwrap(); |
| 54 | + let file_path = write_location.join("logs.csv"); |
| 55 | + |
| 56 | + let generator = AccessLogGenerator::new().with_include_nulls(true); |
| 57 | + let num_batches = 2; |
| 58 | + TestCsvFile::try_new(file_path.clone(), generator.take(num_batches as usize)) |
| 59 | + .expect("Failed to create test file.") |
| 60 | +} |
| 61 | + |
| 62 | +fn criterion_benchmark(c: &mut Criterion) { |
| 63 | + let ctx = create_context().unwrap(); |
| 64 | + let test_file = generate_test_file(); |
| 65 | + |
| 66 | + let mut group = c.benchmark_group("load csv testing"); |
| 67 | + group.measurement_time(Duration::from_secs(20)); |
| 68 | + |
| 69 | + group.bench_function("default csv read options", |b| { |
| 70 | + b.iter(|| { |
| 71 | + load_csv( |
| 72 | + ctx.clone(), |
| 73 | + test_file.path().to_str().unwrap(), |
| 74 | + CsvReadOptions::default(), |
| 75 | + ) |
| 76 | + }) |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +criterion_group!(benches, criterion_benchmark); |
| 81 | +criterion_main!(benches); |
0 commit comments