mito2/sst/index/
statistics.rs1use std::time::{Duration, Instant};
16
17use crate::metrics::{INDEX_CREATE_BYTES_TOTAL, INDEX_CREATE_ELAPSED, INDEX_CREATE_ROWS_TOTAL};
18
19pub type ByteCount = u64;
20pub type RowCount = usize;
21
22enum Stage {
24 Update,
25 #[cfg(feature = "enterprise")]
26 Merge,
27 Finish,
28 Cleanup,
29}
30
31pub struct Statistics {
33 index_type: &'static str,
35 update_elapsed: Duration,
37 update_timing_available: bool,
39 merge_elapsed: Duration,
41 finish_elapsed: Duration,
43 cleanup_eplased: Duration,
45 row_count: RowCount,
47 byte_count: ByteCount,
49}
50
51impl Statistics {
52 pub fn new(index_type: &'static str) -> Self {
54 Self {
55 index_type,
56 update_elapsed: Duration::default(),
57 update_timing_available: true,
58 merge_elapsed: Duration::default(),
59 finish_elapsed: Duration::default(),
60 cleanup_eplased: Duration::default(),
61 row_count: 0,
62 byte_count: 0,
63 }
64 }
65
66 #[cfg(feature = "enterprise")]
69 pub fn new_without_update_timing(index_type: &'static str) -> Self {
70 let mut stats = Self::new(index_type);
71 stats.update_timing_available = false;
72 stats
73 }
74
75 #[cfg(feature = "enterprise")]
77 pub fn inc_row_count(&mut self, rows: RowCount) {
78 self.row_count += rows;
79 }
80
81 #[must_use]
83 pub fn record_update(&mut self) -> TimerGuard<'_> {
84 self.update_timing_available = true;
85 TimerGuard::new(self, Stage::Update)
86 }
87
88 #[must_use]
93 #[cfg(feature = "enterprise")]
94 pub fn record_merge(&mut self) -> TimerGuard<'_> {
95 TimerGuard::new(self, Stage::Merge)
96 }
97
98 #[must_use]
100 pub fn record_finish(&mut self) -> TimerGuard<'_> {
101 TimerGuard::new(self, Stage::Finish)
102 }
103
104 #[must_use]
106 pub fn record_cleanup(&mut self) -> TimerGuard<'_> {
107 TimerGuard::new(self, Stage::Cleanup)
108 }
109
110 pub fn row_count(&self) -> RowCount {
112 self.row_count
113 }
114
115 pub fn byte_count(&self) -> ByteCount {
117 self.byte_count
118 }
119}
120
121impl Drop for Statistics {
122 fn drop(&mut self) {
123 if self.update_timing_available {
124 INDEX_CREATE_ELAPSED
125 .with_label_values(&["update", self.index_type])
126 .observe(self.update_elapsed.as_secs_f64());
127 }
128 if !self.merge_elapsed.is_zero() {
129 INDEX_CREATE_ELAPSED
130 .with_label_values(&["merge", self.index_type])
131 .observe(self.merge_elapsed.as_secs_f64());
132 }
133 INDEX_CREATE_ELAPSED
134 .with_label_values(&["finish", self.index_type])
135 .observe(self.finish_elapsed.as_secs_f64());
136 INDEX_CREATE_ELAPSED
137 .with_label_values(&["cleanup", self.index_type])
138 .observe(self.cleanup_eplased.as_secs_f64());
139 if self.update_timing_available {
140 INDEX_CREATE_ELAPSED
141 .with_label_values(&["total", self.index_type])
142 .observe(
143 (self.update_elapsed
144 + self.merge_elapsed
145 + self.finish_elapsed
146 + self.cleanup_eplased)
147 .as_secs_f64(),
148 );
149 }
150
151 INDEX_CREATE_ROWS_TOTAL
152 .with_label_values(&[self.index_type])
153 .inc_by(self.row_count as _);
154 INDEX_CREATE_BYTES_TOTAL
155 .with_label_values(&[self.index_type])
156 .inc_by(self.byte_count as _);
157 }
158}
159
160pub struct TimerGuard<'a> {
163 stats: &'a mut Statistics,
164 stage: Stage,
165 timer: Instant,
166}
167
168impl<'a> TimerGuard<'a> {
169 fn new(stats: &'a mut Statistics, stage: Stage) -> Self {
171 Self {
172 stats,
173 stage,
174 timer: Instant::now(),
175 }
176 }
177
178 pub fn inc_row_count(&mut self, n: usize) {
180 self.stats.row_count += n;
181 }
182
183 pub fn inc_byte_count(&mut self, n: u64) {
185 self.stats.byte_count += n;
186 }
187}
188
189impl Drop for TimerGuard<'_> {
190 fn drop(&mut self) {
191 match self.stage {
192 Stage::Update => {
193 self.stats.update_elapsed += self.timer.elapsed();
194 }
195 #[cfg(feature = "enterprise")]
196 Stage::Merge => {
197 self.stats.merge_elapsed += self.timer.elapsed();
198 }
199 Stage::Finish => {
200 self.stats.finish_elapsed += self.timer.elapsed();
201 }
202 Stage::Cleanup => {
203 self.stats.cleanup_eplased += self.timer.elapsed();
204 }
205 }
206 }
207}
208
209#[cfg(test)]
210mod tests {
211 use super::*;
212
213 #[cfg(feature = "enterprise")]
214 #[test]
215 fn test_unmeasured_updates_do_not_publish_zero_durations() {
216 let name = "test_sketch_fused_counter_only";
217 let update = INDEX_CREATE_ELAPSED.with_label_values(&["update", name]);
218 let total = INDEX_CREATE_ELAPSED.with_label_values(&["total", name]);
219 let update_before = update.get_sample_count();
220 let total_before = total.get_sample_count();
221 let mut stats = Statistics::new_without_update_timing(name);
222 stats.inc_row_count(7);
223 assert_eq!(stats.row_count(), 7);
224 drop(stats);
225 assert_eq!(update.get_sample_count(), update_before);
226 assert_eq!(total.get_sample_count(), total_before);
227 let mut measured = Statistics::new_without_update_timing(name);
228 {
229 let _timer = measured.record_update();
230 }
231 drop(measured);
232 assert_eq!(update.get_sample_count(), update_before + 1);
233 assert_eq!(total.get_sample_count(), total_before + 1);
234 }
235
236 #[cfg(feature = "enterprise")]
237 #[test]
238 fn test_merge_does_not_count_input_rows_as_output_rows() {
239 let mut stats = Statistics::new("test_merge");
240 {
241 let _guard = stats.record_merge();
242 }
243 assert_eq!(stats.row_count(), 0);
244 {
245 let mut guard = stats.record_finish();
246 guard.inc_row_count(7);
247 guard.inc_byte_count(32);
248 }
249 assert_eq!(stats.row_count(), 7);
250 assert_eq!(stats.byte_count(), 32);
251 }
252
253 #[test]
254 fn test_statistics_basic() {
255 let mut stats = Statistics::new("test");
256 {
257 let mut guard = stats.record_update();
258 guard.inc_byte_count(100);
259 guard.inc_row_count(10);
260
261 let now = Instant::now();
262 while now.elapsed().is_zero() {
263 }
265 }
266 {
267 let _guard = stats.record_finish();
268 let now = Instant::now();
269 while now.elapsed().is_zero() {
270 }
272 }
273 {
274 let _guard = stats.record_cleanup();
275 let now = Instant::now();
276 while now.elapsed().is_zero() {
277 }
279 }
280 assert_eq!(stats.row_count(), 10);
281 assert_eq!(stats.byte_count(), 100);
282 assert!(stats.update_elapsed > Duration::default());
283 assert!(stats.finish_elapsed > Duration::default());
284 assert!(stats.cleanup_eplased > Duration::default());
285 }
286}