forked from datafusion-contrib/datafusion-dft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmarking for FlightSQL from CLI (datafusion-contrib#194)
Adds FlightSQL benchmarking functionality to the cli. The breakdown is expected to include duration summary stats for each flightsql grpc request, such as: get_flight_info time to first byte (time to get first batch from do_get) total do_get time total execution As part of this i refactored `AppExecution` to now hold `ExecutionContext` and `FlightSQLContext`. Each of these context's contain the core execution functionality used by both the CLI and TUI (i.e. executing queries, benchmarking queries, loading ddl, etc). I can envision a future where there is also a `RayContext` or `BallistaContext`.
- Loading branch information
1 parent
25f69d7
commit 3e1576a
Showing
16 changed files
with
806 additions
and
424 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
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,130 @@ | ||
// 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_flight::sql::client::FlightSqlServiceClient; | ||
use datafusion::sql::parser::DFParser; | ||
use log::{error, info, warn}; | ||
|
||
use color_eyre::eyre::{self, Result}; | ||
use tokio::sync::Mutex; | ||
use tokio_stream::StreamExt; | ||
use tonic::{transport::Channel, IntoRequest}; | ||
|
||
use crate::config::FlightSQLConfig; | ||
|
||
use crate::execution::flightsql_benchmarks::FlightSQLBenchmarkStats; | ||
|
||
pub type FlightSQLClient = Mutex<Option<FlightSqlServiceClient<Channel>>>; | ||
|
||
#[derive(Default)] | ||
pub struct FlightSQLContext { | ||
config: FlightSQLConfig, | ||
flightsql_client: FlightSQLClient, | ||
} | ||
|
||
impl FlightSQLContext { | ||
pub fn new(config: FlightSQLConfig) -> Self { | ||
Self { | ||
config, | ||
flightsql_client: Mutex::new(None), | ||
} | ||
} | ||
|
||
pub fn client(&self) -> &FlightSQLClient { | ||
&self.flightsql_client | ||
} | ||
|
||
/// Create FlightSQL client from users FlightSQL config | ||
pub async fn create_client(&self) -> Result<()> { | ||
let url = Box::leak(self.config.connection_url.clone().into_boxed_str()); | ||
info!("Connecting to FlightSQL host: {}", url); | ||
let channel = Channel::from_static(url).connect().await; | ||
match channel { | ||
Ok(c) => { | ||
let client = FlightSqlServiceClient::new(c); | ||
let mut guard = self.flightsql_client.lock().await; | ||
*guard = Some(client); | ||
Ok(()) | ||
} | ||
Err(e) => Err(eyre::eyre!( | ||
"Error creating channel for FlightSQL client: {:?}", | ||
e | ||
)), | ||
} | ||
} | ||
|
||
pub async fn benchmark_query(&self, query: &str) -> Result<FlightSQLBenchmarkStats> { | ||
let iterations = self.config.benchmark_iterations; | ||
let mut get_flight_info_durations = Vec::with_capacity(iterations); | ||
let mut ttfb_durations = Vec::with_capacity(iterations); | ||
let mut do_get_durations = Vec::with_capacity(iterations); | ||
let mut total_durations = Vec::with_capacity(iterations); | ||
|
||
let dialect = datafusion::sql::sqlparser::dialect::GenericDialect {}; | ||
let statements = DFParser::parse_sql_with_dialect(query, &dialect)?; | ||
if statements.len() == 1 { | ||
if let Some(ref mut client) = *self.flightsql_client.lock().await { | ||
for _ in 0..iterations { | ||
let start = std::time::Instant::now(); | ||
let flight_info = client.execute(query.to_string(), None).await?; | ||
if flight_info.endpoint.len() > 1 { | ||
warn!("More than one endpoint: Benchmark results will not be reliable"); | ||
} | ||
let get_flight_info_duration = start.elapsed(); | ||
// Current logic wont properly handle having multiple endpoints | ||
for endpoint in flight_info.endpoint { | ||
if let Some(ticket) = &endpoint.ticket { | ||
match client.do_get(ticket.clone().into_request()).await { | ||
Ok(ref mut s) => { | ||
while let Some((i, _)) = | ||
futures::stream::StreamExt::enumerate(&mut *s).next().await | ||
{ | ||
if i == 0 { | ||
let ttfb_duration = | ||
start.elapsed() - get_flight_info_duration; | ||
ttfb_durations.push(ttfb_duration); | ||
} | ||
} | ||
let do_get_duration = | ||
start.elapsed() - get_flight_info_duration; | ||
do_get_durations.push(do_get_duration); | ||
} | ||
Err(e) => { | ||
error!("Error getting Flight stream: {:?}", e); | ||
} | ||
} | ||
} | ||
} | ||
get_flight_info_durations.push(get_flight_info_duration); | ||
let total_duration = start.elapsed(); | ||
total_durations.push(total_duration); | ||
} | ||
} else { | ||
return Err(eyre::eyre!("No FlightSQL client configured")); | ||
} | ||
Ok(FlightSQLBenchmarkStats::new( | ||
query.to_string(), | ||
get_flight_info_durations, | ||
ttfb_durations, | ||
do_get_durations, | ||
total_durations, | ||
)) | ||
} else { | ||
Err(eyre::eyre!("Only a single statement can be benchmarked")) | ||
} | ||
} | ||
} |
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,100 @@ | ||
// 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 std::time::Duration; | ||
|
||
use super::local_benchmarks::DurationsSummary; | ||
|
||
pub struct FlightSQLBenchmarkStats { | ||
query: String, | ||
runs: usize, | ||
get_flight_info_durations: Vec<Duration>, | ||
ttfb_durations: Vec<Duration>, | ||
do_get_durations: Vec<Duration>, | ||
total_durations: Vec<Duration>, | ||
} | ||
|
||
impl FlightSQLBenchmarkStats { | ||
pub fn new( | ||
query: String, | ||
get_flight_info_durations: Vec<Duration>, | ||
ttfb_durations: Vec<Duration>, | ||
do_get_durations: Vec<Duration>, | ||
total_durations: Vec<Duration>, | ||
) -> Self { | ||
let runs = get_flight_info_durations.len(); | ||
Self { | ||
query, | ||
runs, | ||
get_flight_info_durations, | ||
ttfb_durations, | ||
do_get_durations, | ||
total_durations, | ||
} | ||
} | ||
|
||
fn summarize(&self, durations: &[Duration]) -> DurationsSummary { | ||
let mut sorted = durations.to_vec(); | ||
sorted.sort(); | ||
let len = sorted.len(); | ||
let min = *sorted.first().unwrap(); | ||
let max = *sorted.last().unwrap(); | ||
let mean = sorted.iter().sum::<Duration>() / len as u32; | ||
let median = sorted[len / 2]; | ||
let this_total = durations.iter().map(|d| d.as_nanos()).sum::<u128>(); | ||
let total_duration = self | ||
.total_durations | ||
.iter() | ||
.map(|d| d.as_nanos()) | ||
.sum::<u128>(); | ||
let percent_of_total = (this_total as f64 / total_duration as f64) * 100.0; | ||
DurationsSummary { | ||
min, | ||
max, | ||
mean, | ||
median, | ||
percent_of_total, | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for FlightSQLBenchmarkStats { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f)?; | ||
writeln!(f, "----------------------------")?; | ||
writeln!(f, "Benchmark Stats ({} runs)", self.runs)?; | ||
writeln!(f, "----------------------------")?; | ||
writeln!(f, "{}", self.query)?; | ||
writeln!(f, "----------------------------")?; | ||
|
||
let logical_planning_summary = self.summarize(&self.get_flight_info_durations); | ||
writeln!(f, "Get Flight Info")?; | ||
writeln!(f, "{}", logical_planning_summary)?; | ||
|
||
let physical_planning_summary = self.summarize(&self.ttfb_durations); | ||
writeln!(f, "Time to First Byte")?; | ||
writeln!(f, "{}", physical_planning_summary)?; | ||
|
||
let execution_summary = self.summarize(&self.do_get_durations); | ||
writeln!(f, "Do Get")?; | ||
writeln!(f, "{}", execution_summary)?; | ||
|
||
let total_summary = self.summarize(&self.total_durations); | ||
writeln!(f, "Total")?; | ||
writeln!(f, "{}", total_summary) | ||
} | ||
} |
Oops, something went wrong.