Skip to main content

sqlive/
statement_profiles.rs

1//! The statements a connection ran while a profile was open, with what
2//! SQLite counted of each, for tests and measurements of the plans SQLite
3//! chose.
4
5use super::{HostSql, Policy};
6use std::sync::Arc;
7
8/// One statement execution the profile saw.
9#[derive(Clone, Debug)]
10pub struct ProfiledStatement {
11    /// The statement's text, as prepared.
12    pub sql: String,
13    /// Full-scan steps of this execution alone.
14    pub fullscan_steps: i32,
15    /// Executions of this prepared statement so far, this one included.
16    pub runs: i32,
17    /// Virtual machine steps of this prepared statement so far, this
18    /// execution's included.
19    pub vm_steps: i32,
20    /// Times SQLite prepared this statement again so far, this execution's
21    /// included.
22    pub reprepares: i32,
23}
24
25impl<P: Policy> HostSql<P> {
26    /// Trace this connection's statements until the matching
27    /// [`Self::end_profile`]. A profile inside another, such as a migration's
28    /// profile inside the one that opening the database starts, leaves the
29    /// outer one tracing.
30    pub(super) fn start_profile(&mut self) {
31        let open = self.profiles.get();
32        self.profiles.set(open + 1);
33        if open > 0 {
34            return;
35        }
36        self.scans.lock().unwrap().clear();
37        let scans = Arc::clone(&self.scans);
38        self.connection
39            .trace_v2(
40                rusqlite::trace::TraceEventCodes::SQLITE_TRACE_PROFILE,
41                Some(move |event: rusqlite::trace::TraceEvent<'_>| {
42                    if let rusqlite::trace::TraceEvent::Profile(statement, _) = event {
43                        scans.lock().unwrap().push(ProfiledStatement {
44                            sql: statement.sql().into_owned(),
45                            fullscan_steps: statement
46                                .get_status(rusqlite::StatementStatus::FullscanStep),
47                            runs: statement.get_status(rusqlite::StatementStatus::Run),
48                            vm_steps: statement.get_status(rusqlite::StatementStatus::VmStep),
49                            reprepares: statement.get_status(rusqlite::StatementStatus::RePrepare),
50                        });
51                    }
52                }),
53            )
54            .unwrap();
55    }
56
57    /// End a profile; only the outermost stops tracing.
58    pub(super) fn end_profile(&mut self) {
59        let open = self.profiles.get() - 1;
60        self.profiles.set(open);
61        if open == 0 {
62            self.connection
63                .trace_v2(
64                    rusqlite::trace::TraceEventCodes::empty(),
65                    None::<fn(rusqlite::trace::TraceEvent<'_>)>,
66                )
67                .unwrap();
68        }
69    }
70
71    /// The statements the last profile saw, in the order they ran.
72    pub fn profiled_statements(&self) -> Vec<ProfiledStatement> {
73        self.scans.lock().unwrap().clone()
74    }
75}