scirs2_core/profiling/
entries.rs

1//! Data structures for profiling entries (timing and memory)
2
3use std::time::Duration;
4
5/// Timing entry for the profiler
6#[derive(Debug, Clone)]
7pub struct TimingEntry {
8    /// Number of calls
9    calls: usize,
10    /// Total duration
11    total_duration: Duration,
12    /// Minimum duration
13    min_duration: Duration,
14    /// Maximum duration
15    max_duration: Duration,
16    /// Parent operation (used for hierarchical profiling structure)
17    #[allow(dead_code)]
18    parent: Option<String>,
19    /// Child operations
20    children: Vec<String>,
21}
22
23impl TimingEntry {
24    /// Create a new timing entry
25    pub fn new(duration: Duration, parent: Option<&str>) -> Self {
26        Self {
27            calls: 1,
28            total_duration: duration,
29            min_duration: duration,
30            max_duration: duration,
31            parent: parent.map(String::from),
32            children: Vec::new(),
33        }
34    }
35
36    /// Add a new timing measurement
37    pub fn add_measurement(&mut self, duration: Duration) {
38        self.calls += 1;
39        self.total_duration += duration;
40        self.min_duration = std::cmp::min(self.min_duration, duration);
41        self.max_duration = std::cmp::max(self.max_duration, duration);
42    }
43
44    /// Add a child operation
45    pub fn add_child(&mut self, child: &str) {
46        if !self.children.contains(&child.to_string()) {
47            self.children.push(child.to_string());
48        }
49    }
50
51    /// Get the average duration
52    pub fn average_duration(&self) -> Duration {
53        if self.calls == 0 {
54            Duration::from_secs(0)
55        } else {
56            self.total_duration / self.calls as u32
57        }
58    }
59
60    /// Get the number of calls
61    pub fn calls(&self) -> usize {
62        self.calls
63    }
64
65    /// Get the total duration
66    pub fn total_duration(&self) -> Duration {
67        self.total_duration
68    }
69
70    /// Get the minimum duration
71    pub fn min_duration(&self) -> Duration {
72        self.min_duration
73    }
74
75    /// Get the maximum duration
76    pub fn max_duration(&self) -> Duration {
77        self.max_duration
78    }
79
80    /// Get the parent operation name
81    pub fn parent(&self) -> Option<&str> {
82        self.parent.as_deref()
83    }
84
85    /// Get the child operations
86    pub fn children(&self) -> &[String] {
87        &self.children
88    }
89}
90
91/// Memory tracking entry for the profiler
92#[derive(Debug, Clone)]
93pub struct MemoryEntry {
94    /// Number of allocations
95    allocations: usize,
96    /// Total memory delta (can be negative for memory releases)
97    total_delta: isize,
98    /// Maximum memory delta in a single allocation
99    max_delta: usize,
100}
101
102impl MemoryEntry {
103    /// Create a new memory entry
104    pub fn new(delta: usize) -> Self {
105        Self {
106            allocations: 1,
107            total_delta: delta as isize,
108            max_delta: delta,
109        }
110    }
111
112    /// Add a new memory measurement
113    pub fn add_measurement(&mut self, delta: usize) {
114        self.allocations += 1;
115        self.total_delta += delta as isize;
116        self.max_delta = std::cmp::max(self.max_delta, delta);
117    }
118
119    /// Get the average memory delta
120    pub fn average_delta(&self) -> f64 {
121        if self.allocations == 0 {
122            0.0
123        } else {
124            self.total_delta as f64 / self.allocations as f64
125        }
126    }
127
128    /// Get the number of allocations
129    pub fn allocations(&self) -> usize {
130        self.allocations
131    }
132
133    /// Get the total memory delta
134    pub fn total_delta(&self) -> isize {
135        self.total_delta
136    }
137
138    /// Get the maximum memory delta
139    pub fn max_delta(&self) -> usize {
140        self.max_delta
141    }
142}