coven_database/work_counts.rs
1//! Test-only accounting shared by a database's writer and readers. The
2//! connections count the work SQLite does for them, and coven counts its own
3//! work beside it; a test reads the two together.
4
5use coven_foundation::locks::lock_unpoisoned;
6use std::sync::{Arc, Mutex};
7
8/// Local database work observed at its execution boundaries.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub struct WorkCounts {
11 /// Successful acquisitions of the writer connection, including read-only work.
12 pub writer_holds: usize,
13 /// Successful durable transaction commits, including unchanged transactions.
14 pub commits: usize,
15 /// Snapshot bodies the loader parsed, for loading, verification or
16 /// file-reference checks.
17 pub snapshot_reads: usize,
18 /// Rows whose published state an applied slot kept or put back beside
19 /// this device's unsent writes (§6.2).
20 pub published_rows: usize,
21 /// Rows a removal view evaluated: its present rows staged, with their
22 /// CHECKs, unique values and references read (§9.3).
23 pub evaluated_rows: usize,
24 /// Reads of what a transaction can't change: the schema version, SQLite's
25 /// schema cookie, and synced columns' and foreign keys' registered ids.
26 pub constant_reads: usize,
27 /// SQLite virtual machine steps the app's statements ran in `query` and
28 /// `query_row`, in reads, live queries' runs, writes and migrations.
29 pub app_read_steps: usize,
30 /// The app's statements that return rows, run in `query` and `query_row`,
31 /// one per run.
32 pub app_read_statements: usize,
33 /// SQLite virtual machine steps the app's statements ran in `execute` and
34 /// `execute_batch`, triggers' included, in writes and migrations.
35 pub app_write_steps: usize,
36 /// Steps of coven's lookups judging whether a write changed rows a live
37 /// query joins: each asks whether a partner row in its scope holds a
38 /// changed row's value (§4).
39 pub lookup_steps: usize,
40}
41
42/// The work coven counts beside its connections'.
43#[derive(Clone, Copy, Debug, Default)]
44pub(crate) struct StoreWork {
45 pub(crate) writer_holds: usize,
46 pub(crate) snapshot_reads: usize,
47 pub(crate) published_rows: usize,
48 pub(crate) evaluated_rows: usize,
49 /// Reads of the schema version and of synced columns' and foreign
50 /// keys' registered ids.
51 pub(crate) constant_reads: usize,
52}
53
54/// Injected counter; clones observe the same database, never other test databases.
55#[derive(Clone)]
56pub struct WorkCounter {
57 connections: sqlive::ConnectionWorkCounter,
58 store: Arc<Mutex<StoreWork>>,
59}
60
61impl WorkCounter {
62 /// A counter no database has counted on yet, for a test to give one.
63 pub fn new() -> Self {
64 Self {
65 connections: sqlive::ConnectionWorkCounter::new(),
66 store: Arc::new(Mutex::new(StoreWork::default())),
67 }
68 }
69
70 /// Start another measurement after the preceding work has completed.
71 pub fn take(&self) -> WorkCounts {
72 let connections = self.connections.take();
73 let store = std::mem::take(&mut *lock_unpoisoned(&self.store));
74 WorkCounts {
75 writer_holds: store.writer_holds,
76 commits: connections.commits,
77 snapshot_reads: store.snapshot_reads,
78 published_rows: store.published_rows,
79 evaluated_rows: store.evaluated_rows,
80 constant_reads: store.constant_reads + connections.cookie_reads,
81 app_read_steps: connections.app_read_steps,
82 app_read_statements: connections.app_read_statements,
83 app_write_steps: connections.app_write_steps,
84 lookup_steps: connections.lookup_steps,
85 }
86 }
87
88 /// Have `connection` count its work here from now on.
89 pub(crate) fn track(&self, connection: &crate::sqlite::StoreSql) {
90 connection.test_track_work(self.connections.clone());
91 }
92
93 /// Count work coven did beside its connections.
94 pub(crate) fn record(&self, update: impl FnOnce(&mut StoreWork)) {
95 update(&mut lock_unpoisoned(&self.store));
96 }
97}