hawdb_sql/timing.rs
1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
16pub struct RelationalSqlStageTimings {
17 /// Time spent parsing SQL on a template-cache miss. Cache hits report zero.
18 pub parse_nanos: u64,
19 /// Time spent resolving the bound-neutral template against current schema state.
20 pub bind_nanos: u64,
21 /// Time spent selecting access paths, enumerating joins, and lowering execution state.
22 pub plan_nanos: u64,
23 /// Time spent executing the prepared relational operators.
24 pub execute_nanos: u64,
25}
26
27impl RelationalSqlStageTimings {
28 pub const fn total_nanos(self) -> u64 {
29 self.parse_nanos
30 .saturating_add(self.bind_nanos)
31 .saturating_add(self.plan_nanos)
32 .saturating_add(self.execute_nanos)
33 }
34}
35
36#[doc(hidden)]
37pub fn elapsed_nanos(started: hawdb_core::time::Instant) -> u64 {
38 u64::try_from(started.elapsed().as_nanos()).unwrap_or(u64::MAX)
39}
40
41#[doc(hidden)]
42pub fn measure_nanos<T>(nanos: &mut u64, operation: impl FnOnce() -> T) -> T {
43 let started = hawdb_core::time::Instant::now();
44 let output = operation();
45 *nanos = nanos.saturating_add(elapsed_nanos(started));
46 output
47}