Skip to content

Commit

Permalink
Added in a SQL execution time calculation (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloutiertyler authored Feb 25, 2025
1 parent 172c060 commit d147dff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/client-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl Host {
// We need a header for query results
let mut header = vec![];

let sql_start = std::time::Instant::now();
let sql_span =
tracing::trace_span!("execute_sql", total_duration = tracing::field::Empty,).entered();

let rows = sql::execute::run(
// Returns an empty result set for mutations
db,
Expand All @@ -101,13 +105,20 @@ impl Host {
}
})?;

let total_duration = sql_start.elapsed();
sql_span.record("total_duration", tracing::field::debug(total_duration));

// Turn the header into a `ProductType`
let schema = header
.into_iter()
.map(|(col_name, col_type)| ProductTypeElement::new(col_type, Some(col_name)))
.collect();

Ok(vec![StmtResultJson { schema, rows }])
Ok(vec![StmtResultJson {
schema,
rows,
total_duration_micros: total_duration.as_micros() as u64,
}])
},
)
.await
Expand Down
9 changes: 8 additions & 1 deletion crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::auth::{
anon_auth_middleware, SpacetimeAuth, SpacetimeEnergyUsed, SpacetimeExecutionDurationMicros, SpacetimeIdentity,
SpacetimeIdentityToken,
Expand Down Expand Up @@ -385,7 +387,12 @@ where
.ok_or(StatusCode::NOT_FOUND)?;
let json = host.exec_sql(auth, database, body).await?;

Ok(axum::Json(json))
let total_duration = json.iter().fold(0, |acc, x| acc + x.total_duration_micros);

Ok((
TypedHeader(SpacetimeExecutionDurationMicros(Duration::from_micros(total_duration))),
axum::Json(json),
))
}

#[derive(Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/json/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ use spacetimedb_lib::{ProductType, ProductValue};
pub struct StmtResultJson {
pub schema: ProductType,
pub rows: Vec<ProductValue>,
pub total_duration_micros: u64,
}

2 comments on commit d147dff

@github-actions
Copy link

@github-actions github-actions bot commented on d147dff Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Criterion benchmark results

Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

@github-actions
Copy link

@github-actions github-actions bot commented on d147dff Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark results Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

Please sign in to comment.