linkrs_metrics/
executor_stats.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::time::Duration;
4
5#[derive(Debug, Clone, Default, Serialize, Deserialize)]
6pub struct ExecutorStats {
7 pub num_rows: usize,
8 pub exec_time_us: u64,
9 pub total_time_us: u64,
10 pub memory_peak: usize,
11 pub memory_current: usize,
12 pub batch_count: usize,
13 pub other_stats: HashMap<String, String>,
14}
15
16impl ExecutorStats {
17 pub fn new() -> Self {
18 Self::default()
19 }
20
21 pub fn add_row(&mut self, count: usize) {
22 self.num_rows += count;
23 }
24
25 pub fn add_exec_time(&mut self, duration: Duration) {
26 self.exec_time_us += duration.as_micros() as u64;
27 }
28
29 pub fn add_total_time(&mut self, duration: Duration) {
30 self.total_time_us += duration.as_micros() as u64;
31 }
32
33 pub fn set_memory_peak(&mut self, peak: usize) {
34 if peak > self.memory_peak {
35 self.memory_peak = peak;
36 }
37 }
38
39 pub fn update_memory_current(&mut self, current: usize) {
40 self.memory_current = current;
41 }
42
43 pub fn add_batch(&mut self, count: usize) {
44 self.batch_count += count;
45 }
46
47 pub fn add_stat(&mut self, key: String, value: String) {
48 self.other_stats.insert(key, value);
49 }
50
51 pub fn get_stat(&self, key: &str) -> Option<&String> {
52 self.other_stats.get(key)
53 }
54
55 pub fn throughput_rows_per_sec(&self) -> f64 {
56 if self.exec_time_us == 0 {
57 return 0.0;
58 }
59 self.num_rows as f64 / (self.exec_time_us as f64 / 1_000_000.0)
60 }
61
62 pub fn efficiency_rows_per_us(&self) -> f64 {
63 if self.exec_time_us == 0 {
64 return 0.0;
65 }
66 self.num_rows as f64 / self.exec_time_us as f64
67 }
68
69 pub fn to_json(&self) -> Result<String, serde_json::Error> {
70 serde_json::to_string(self)
71 }
72
73 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
74 serde_json::from_str(json)
75 }
76
77 pub fn to_formatted_string(&self) -> String {
78 format!(
79 "rows={}, exec_time={}ms, total_time={}ms, memory_peak={}B, batches={}",
80 self.num_rows,
81 self.exec_time_us / 1000,
82 self.total_time_us / 1000,
83 self.memory_peak,
84 self.batch_count,
85 )
86 }
87}