Apache DataFusion SQL Query Engine
ARROW-9711: [Rust] Add new benchmark derived from TPC-H
Andy Groveab9073915c73
diff --git a/rust/benchmarks/README.md b/rust/benchmarks/README.mdindex d38f58672c..468341dccc 100644--- a/rust/benchmarks/README.md+++ b/rust/benchmarks/README.md@@ -19,12 +19,28 @@# Apache Arrow Rust Benchmarks-This crate contains benchmarks based on the [New York Taxi and Limousine Commission](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page) data set.+This crate contains benchmarks based on popular public data sets and open source benchmark suites, making it easy to+run real-world benchmarks to help with performance and scalability testing and for comparing performance with other Arrow+implementations as well as other query engines.-Currently, only DataFusion benchmarks exist, but the plan is to add benchmarks for the arrow, flight, and parquet crates as well.+Currently, only DataFusion benchmarks exist, but the plan is to add benchmarks for the arrow, flight, and parquet+crates as well.++## Benchmark derived from TPC-H++These benchmarks are derived from the [TPC-H](http://www.tpc.org/tpch/) benchmark. Data for this benchmark can be+generated using [tpch-dbgen](https://github.com/databricks/tpch-dbgen).++```bash+cargo run --release --bin tpch -- --iterations 3 --path /mnt/tpch/csv --format csv --query 1 --batch-size 4096+```++## NYC Taxi Benchmark++These benchmarks are based on the [New York Taxi and Limousine Commission](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page) data set.```bash-cargo run --release -- --iterations 3 --path /mnt/nyctaxi/csv --format csv --batch-size 4096+cargo run --release --bin nyctaxi -- --iterations 3 --path /mnt/nyctaxi/csv --format csv --batch-size 4096```Example output:diff --git a/rust/benchmarks/src/main.rs b/rust/benchmarks/src/bin/nyctaxi.rssimilarity index 100%rename from rust/benchmarks/src/main.rsrename to rust/benchmarks/src/bin/nyctaxi.rsdiff --git a/rust/benchmarks/src/bin/tpch.rs b/rust/benchmarks/src/bin/tpch.rsnew file mode 100644index 0000000000..a8a3b96350--- /dev/null+++ b/rust/benchmarks/src/bin/tpch.rs@@ -0,0 +1,171 @@+// 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.++//! Benchmark derived from TPC-H. This is not an official TPC-H benchmark.++use std::path::PathBuf;+use std::process;+use std::time::Instant;++use arrow::datatypes::{DataType, Field, Schema};+use arrow::util::pretty;+use datafusion::error::Result;+use datafusion::execution::context::{ExecutionConfig, ExecutionContext};++use datafusion::execution::physical_plan::csv::CsvReadOptions;+use structopt::StructOpt;++#[derive(Debug, StructOpt)]+#[structopt(name = "Benchmarks", about = "Apache Arrow Rust Benchmarks.")]+struct TpchOpt {+ /// Query number+ #[structopt(short, long)]+ query: usize,++ /// Activate debug mode to see query results+ #[structopt(short, long)]+ debug: bool,++ /// Number of iterations of each test run+ #[structopt(short = "i", long = "iterations", default_value = "3")]+ iterations: usize,++ /// Number of threads to use for parallel execution+ #[structopt(short = "c", long = "concurrency", default_value = "2")]+ concurrency: usize,++ /// Batch size when reading CSV or Parquet files+ #[structopt(short = "s", long = "batch-size", default_value = "4096")]+ batch_size: usize,++ /// Path to data files+ #[structopt(parse(from_os_str), required = true, short = "p", long = "path")]+ path: PathBuf,++ /// File format: `csv` or `parquet`+ #[structopt(short = "f", long = "format", default_value = "csv")]+ file_format: String,+}++fn main() -> Result<()> {+ let opt = TpchOpt::from_args();+ println!("Running benchmarks with the following options: {:?}", opt);++ let mut ctx = ExecutionContext::with_config(+ ExecutionConfig::new().with_concurrency(opt.concurrency),+ );++ let path = opt.path.to_str().unwrap();++ match opt.file_format.as_str() {+ "csv" => {+ let path = format!("{}/lineitem", path);+ let schema = lineitem_schema();+ let options = CsvReadOptions::new().schema(&schema).has_header(true);+ ctx.register_csv("lineitem", &path, options)?+ }+ "parquet" => {+ let path = format!("{}/lineitem", path);+ ctx.register_parquet("lineitem", &path)?+ }+ other => {+ println!("Invalid file format '{}'", other);+ process::exit(-1);+ }+ }++ let sql = match opt.query {+ 1 => {+ "select+ l_returnflag,+ l_linestatus,+ sum(l_quantity),+ sum(l_extendedprice),+ sum(l_extendedprice * (1 - l_discount)),+ sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)),+ avg(l_quantity),+ avg(l_extendedprice),+ avg(l_discount),+ count(*)+ from+ lineitem+ where+ l_shipdate <= '1998-12-01'+ group by+ l_returnflag,+ l_linestatus+ order by+ l_returnflag,+ l_linestatus"+ }++ _ => unimplemented!("unsupported query"),+ };++ for i in 0..opt.iterations {+ let start = Instant::now();+ execute_sql(&mut ctx, sql, opt.batch_size, opt.debug)?;+ println!(+ "Query {} iteration {} took {} ms",+ opt.query,+ i,+ start.elapsed().as_millis()+ );+ }++ Ok(())+}++fn execute_sql(+ ctx: &mut ExecutionContext,+ sql: &str,+ batch_size: usize,+ debug: bool,+) -> Result<()> {+ let plan = ctx.create_logical_plan(sql)?;+ let plan = ctx.optimize(&plan)?;+ if debug {+ println!("Optimized logical plan:\n{:?}", plan);+ }+ let physical_plan = ctx.create_physical_plan(&plan, batch_size)?;+ let result = ctx.collect(physical_plan.as_ref())?;+ if debug {+ pretty::print_batches(&result)?;+ }+ Ok(())+}++fn lineitem_schema() -> Schema {+ Schema::new(vec![+ Field::new("l_orderkey", DataType::UInt32, true),+ Field::new("l_partkey", DataType::UInt32, true),+ Field::new("l_suppkey", DataType::UInt32, true),+ Field::new("l_linenumber", DataType::UInt32, true),+ Field::new("l_quantity", DataType::Float64, true),+ Field::new("l_extendedprice", DataType::Float64, true),+ Field::new("l_discount", DataType::Float64, true),+ Field::new("l_tax", DataType::Float64, true),+ Field::new("l_returnflag", DataType::Utf8, true),+ Field::new("l_linestatus", DataType::Utf8, true),+ Field::new("l_shipdate", DataType::Utf8, true),+ Field::new("l_commitdate", DataType::Utf8, true),+ Field::new("l_receiptdate", DataType::Utf8, true),+ Field::new("l_shipinstruct", DataType::Utf8, true),+ Field::new("l_shipmode", DataType::Utf8, true),+ Field::new("l_comment", DataType::Utf8, true),+ ])+}