-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add benchmark_runner for sql_benchmarks with help and list commands #22001
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = "tpch" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. Why is this needed? It seems somewhat redundant with datafusion/benchmarks/src/sql_benchmark.rs Lines 45 to 70 in dcd364a
Contributor
Author
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. The name doesn't really need to be there. The suite file though does for later PR's. The SqlBenchmark is per file, the suite is ... per suite. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description = "TPC-H SQL benchmarks" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [[options]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = "format" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| short = "f" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default = "parquet" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| values = ["parquet", "csv", "mem"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help = "Selects the TPC-H data format." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [[options]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name = "scale-factor" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| short = "sf" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default = "1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| values = ["1", "10"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help = "Selects the TPC-H scale factor." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. | ||
|
|
||
| //! CLI construction and argument conversion for `benchmark_runner`. | ||
| //! | ||
| //! This module owns the clap command tree for the initial runner surface: | ||
| //! top-level help and suite listing. | ||
|
|
||
| use clap::builder::styling::{AnsiColor, Styles}; | ||
| use clap::{ArgMatches, Command}; | ||
| use datafusion_common::{Result, exec_datafusion_err}; | ||
|
|
||
| const HELP_STYLES: Styles = Styles::styled() | ||
| .header(AnsiColor::Green.on_default().bold()) | ||
| .usage(AnsiColor::Green.on_default().bold()) | ||
| .literal(AnsiColor::Cyan.on_default().bold()) | ||
| .placeholder(AnsiColor::Cyan.on_default()); | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum RunnerCommand { | ||
| Help, | ||
| List, | ||
| } | ||
|
|
||
| /// Builds the command tree for help and suite listing. | ||
| pub fn build_cli() -> Command { | ||
| Command::new("benchmark_runner") | ||
| .about("Inspect DataFusion SQL benchmark suites.") | ||
| .styles(HELP_STYLES) | ||
| .subcommand_required(false) | ||
| .arg_required_else_help(false) | ||
| .disable_help_subcommand(true) | ||
| .subcommand(Command::new("help").about("Print help")) | ||
| .subcommand(Command::new("list").about("List SQL benchmark suites")) | ||
| } | ||
|
|
||
| /// Converts clap matches into a typed command. | ||
| pub(crate) fn command_from_matches(matches: &ArgMatches) -> Result<RunnerCommand> { | ||
| match matches.subcommand() { | ||
| None | Some(("help", _)) => Ok(RunnerCommand::Help), | ||
| Some(("list", _)) => Ok(RunnerCommand::List), | ||
| Some((name, _)) => Err(exec_datafusion_err!("Unknown command '{name}'")), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn list_rejects_unrecognized_options() { | ||
| let matches = | ||
| build_cli().try_get_matches_from(["benchmark_runner", "list", "--format"]); | ||
|
|
||
| assert!(matches.is_err(), "{matches:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn help_mentions_list_command() { | ||
| let err = build_cli() | ||
| .try_get_matches_from(["benchmark_runner", "--help"]) | ||
| .unwrap_err(); | ||
| let help = err.to_string(); | ||
|
|
||
| assert!(help.contains("list")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // 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. | ||
|
|
||
| //! Command-line inspection for SQL benchmark suites. | ||
| //! | ||
| //! This module backs the `benchmark_runner` binary. The initial command | ||
| //! surface lists discovered SQL benchmark suites from `.suite` files and | ||
| //! prints the top-level help. | ||
| //! | ||
| //! Common invocations: | ||
| //! | ||
| //! ```text | ||
| //! cargo run --bin benchmark_runner -- --help | ||
| //! cargo run --release --bin benchmark_runner -- list | ||
| //! ``` | ||
| //! | ||
| //! The public entry point is [`run_cli`]. The submodules are kept private so | ||
| //! the command-line flow remains the single supported API: | ||
| //! | ||
| //! - `cli` builds the clap command tree and parses the selected command. | ||
| //! - `suite` loads `.suite` metadata and discovers benchmark query files. | ||
| //! - `output` formats colored `list` command output. | ||
|
|
||
| mod cli; | ||
| mod output; | ||
| mod suite; | ||
|
|
||
| use crate::benchmark_runner::cli::{RunnerCommand, build_cli, command_from_matches}; | ||
| use crate::benchmark_runner::output::format_suite_list_styled; | ||
| use crate::benchmark_runner::suite::SuiteRegistry; | ||
| use datafusion::error::Result; | ||
| use datafusion_common::DataFusionError; | ||
| use std::io::Write; | ||
| use std::path::PathBuf; | ||
|
|
||
| /// Runs the benchmark runner command-line flow for the provided argument list. | ||
| /// | ||
| /// This discovers suite metadata, parses the help/list command, and dispatches | ||
| /// to the selected implementation. | ||
| pub fn run_cli<I, T>(args: I) -> Result<()> | ||
| where | ||
| I: IntoIterator<Item = T>, | ||
| T: Clone + Into<std::ffi::OsString>, | ||
| { | ||
| let benchmark_dir = default_benchmark_dir(); | ||
| let registry = SuiteRegistry::discover(&benchmark_dir)?; | ||
| let mut cli = build_cli(); | ||
| let matches = match cli.try_get_matches_from_mut(args) { | ||
| Ok(matches) => matches, | ||
| Err(e) if e.kind() == clap::error::ErrorKind::DisplayHelp => { | ||
| e.print()?; | ||
| return Ok(()); | ||
| } | ||
| Err(e) => return Err(DataFusionError::External(Box::new(e))), | ||
| }; | ||
| let command = command_from_matches(&matches)?; | ||
|
|
||
| match command { | ||
| RunnerCommand::Help => { | ||
| cli.print_long_help()?; | ||
| println!(); | ||
| } | ||
| RunnerCommand::List => { | ||
| print_styled(&format_suite_list_styled(®istry)?)?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Writes already styled output through `anstream` so ANSI color handling | ||
| /// matches clap help output on supported terminals. | ||
| fn print_styled(output: &str) -> Result<()> { | ||
| let mut stdout = anstream::stdout(); | ||
|
|
||
| write!(&mut stdout, "{output}") | ||
| .map_err(|e| DataFusionError::External(Box::new(e)))?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Resolves the SQL benchmark root from either the repository root or the | ||
| /// benchmarks crate manifest directory. | ||
| fn default_benchmark_dir() -> PathBuf { | ||
| let repo_root_path = PathBuf::from("benchmarks/sql_benchmarks"); | ||
| if repo_root_path.exists() { | ||
| repo_root_path | ||
| } else { | ||
| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("sql_benchmarks") | ||
| } | ||
| } |
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.
I double checked and it seems like
anstreamis already a dependency of clap_builder, so this is not not a (net) new dependencyThere 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.
Coloured output ftw :) At least locally, depends on your terminal I think.