oxigdal-streaming 0.1.4

Real-time data processing and streaming pipelines for OxiGDAL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Performance tracking for streaming operations.

use crate::error::Result;
use chrono::{DateTime, Duration, Utc};
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;

/// Type alias for throughput interval samples (timestamp, element_count, byte_count).
type IntervalSamples = VecDeque<(DateTime<Utc>, u64, u64)>;

/// Performance tracker for streaming operations.
pub struct PerformanceTracker {
    start_time: Instant,
    checkpoints: Arc<RwLock<Vec<(String, Instant)>>>,
    enabled: Arc<RwLock<bool>>,
}

impl PerformanceTracker {
    /// Create a new performance tracker.
    pub fn new() -> Self {
        Self {
            start_time: Instant::now(),
            checkpoints: Arc::new(RwLock::new(Vec::new())),
            enabled: Arc::new(RwLock::new(true)),
        }
    }

    /// Enable tracking.
    pub async fn enable(&self) {
        *self.enabled.write().await = true;
    }

    /// Disable tracking.
    pub async fn disable(&self) {
        *self.enabled.write().await = false;
    }

    /// Record a checkpoint.
    pub async fn checkpoint(&self, name: String) -> Result<()> {
        if !*self.enabled.read().await {
            return Ok(());
        }

        let mut checkpoints = self.checkpoints.write().await;
        checkpoints.push((name, Instant::now()));

        Ok(())
    }

    /// Get elapsed time since start.
    pub fn elapsed(&self) -> std::time::Duration {
        self.start_time.elapsed()
    }

    /// Get all checkpoints.
    pub async fn get_checkpoints(&self) -> Vec<(String, std::time::Duration)> {
        let checkpoints = self.checkpoints.read().await;
        let start = self.start_time;

        checkpoints
            .iter()
            .map(|(name, instant)| {
                let duration = instant.duration_since(start);
                (name.clone(), duration)
            })
            .collect()
    }

    /// Clear all checkpoints.
    pub async fn clear(&self) {
        self.checkpoints.write().await.clear();
    }

    /// Reset the tracker.
    pub async fn reset(&self) {
        self.clear().await;
    }
}

impl Default for PerformanceTracker {
    fn default() -> Self {
        Self::new()
    }
}

/// Latency tracker with histogram support.
pub struct LatencyTracker {
    samples: Arc<RwLock<VecDeque<std::time::Duration>>>,
    max_samples: usize,
    buckets: Vec<std::time::Duration>,
    histogram: Arc<RwLock<Vec<u64>>>,
}

impl LatencyTracker {
    /// Create a new latency tracker.
    pub fn new(max_samples: usize) -> Self {
        let buckets = vec![
            std::time::Duration::from_millis(1),
            std::time::Duration::from_millis(5),
            std::time::Duration::from_millis(10),
            std::time::Duration::from_millis(50),
            std::time::Duration::from_millis(100),
            std::time::Duration::from_millis(500),
            std::time::Duration::from_secs(1),
            std::time::Duration::from_secs(5),
        ];

        let histogram = vec![0; buckets.len()];

        Self {
            samples: Arc::new(RwLock::new(VecDeque::with_capacity(max_samples))),
            max_samples,
            buckets,
            histogram: Arc::new(RwLock::new(histogram)),
        }
    }

    /// Record a latency sample.
    pub async fn record(&self, latency: std::time::Duration) {
        let mut samples = self.samples.write().await;

        if samples.len() >= self.max_samples {
            samples.pop_front();
        }

        samples.push_back(latency);

        let mut histogram = self.histogram.write().await;
        for (i, &bucket) in self.buckets.iter().enumerate() {
            if latency <= bucket {
                histogram[i] += 1;
            }
        }
    }

    /// Get the average latency.
    pub async fn average(&self) -> Option<std::time::Duration> {
        let samples = self.samples.read().await;

        if samples.is_empty() {
            return None;
        }

        let sum: std::time::Duration = samples.iter().sum();
        Some(sum / samples.len() as u32)
    }

    /// Get the minimum latency.
    pub async fn min(&self) -> Option<std::time::Duration> {
        let samples = self.samples.read().await;
        samples.iter().min().copied()
    }

    /// Get the maximum latency.
    pub async fn max(&self) -> Option<std::time::Duration> {
        let samples = self.samples.read().await;
        samples.iter().max().copied()
    }

    /// Get the median latency.
    pub async fn median(&self) -> Option<std::time::Duration> {
        let samples = self.samples.read().await;

        if samples.is_empty() {
            return None;
        }

        let mut sorted: Vec<_> = samples.iter().copied().collect();
        sorted.sort();

        Some(sorted[sorted.len() / 2])
    }

    /// Get the 95th percentile latency.
    pub async fn p95(&self) -> Option<std::time::Duration> {
        self.percentile(0.95).await
    }

    /// Get the 99th percentile latency.
    pub async fn p99(&self) -> Option<std::time::Duration> {
        self.percentile(0.99).await
    }

    /// Get a specific percentile.
    pub async fn percentile(&self, p: f64) -> Option<std::time::Duration> {
        let samples = self.samples.read().await;

        if samples.is_empty() {
            return None;
        }

        let mut sorted: Vec<_> = samples.iter().copied().collect();
        sorted.sort();

        let index = ((sorted.len() as f64 * p) as usize).min(sorted.len() - 1);
        Some(sorted[index])
    }

    /// Get the histogram.
    pub async fn histogram(&self) -> Vec<(std::time::Duration, u64)> {
        let histogram = self.histogram.read().await;

        self.buckets
            .iter()
            .zip(histogram.iter())
            .map(|(&bucket, &count)| (bucket, count))
            .collect()
    }

    /// Clear all samples.
    pub async fn clear(&self) {
        self.samples.write().await.clear();
        *self.histogram.write().await = vec![0; self.buckets.len()];
    }
}

/// Throughput tracker.
pub struct ThroughputTracker {
    start_time: DateTime<Utc>,
    element_count: Arc<RwLock<u64>>,
    byte_count: Arc<RwLock<u64>>,
    interval_samples: Arc<RwLock<IntervalSamples>>,
    interval_duration: Duration,
    max_intervals: usize,
}

impl ThroughputTracker {
    /// Create a new throughput tracker.
    pub fn new(interval_duration: Duration, max_intervals: usize) -> Self {
        Self {
            start_time: Utc::now(),
            element_count: Arc::new(RwLock::new(0)),
            byte_count: Arc::new(RwLock::new(0)),
            interval_samples: Arc::new(RwLock::new(VecDeque::with_capacity(max_intervals))),
            interval_duration,
            max_intervals,
        }
    }

    /// Record elements processed.
    pub async fn record_elements(&self, count: u64) {
        *self.element_count.write().await += count;
    }

    /// Record bytes processed.
    pub async fn record_bytes(&self, bytes: u64) {
        *self.byte_count.write().await += bytes;
    }

    /// Record both elements and bytes.
    pub async fn record(&self, elements: u64, bytes: u64) {
        self.record_elements(elements).await;
        self.record_bytes(bytes).await;
    }

    /// Take an interval snapshot.
    pub async fn snapshot(&self) {
        let now = Utc::now();
        let elements = *self.element_count.read().await;
        let bytes = *self.byte_count.read().await;

        let mut samples = self.interval_samples.write().await;

        if samples.len() >= self.max_intervals {
            samples.pop_front();
        }

        samples.push_back((now, elements, bytes));
    }

    /// Get the overall throughput (elements per second).
    pub async fn elements_per_second(&self) -> f64 {
        let elapsed = (Utc::now() - self.start_time).num_milliseconds() as f64 / 1000.0;
        let count = *self.element_count.read().await as f64;

        if elapsed > 0.0 { count / elapsed } else { 0.0 }
    }

    /// Get the overall throughput (bytes per second).
    pub async fn bytes_per_second(&self) -> f64 {
        let elapsed = (Utc::now() - self.start_time).num_milliseconds() as f64 / 1000.0;
        let bytes = *self.byte_count.read().await as f64;

        if elapsed > 0.0 { bytes / elapsed } else { 0.0 }
    }

    /// Get the average throughput over recent intervals.
    pub async fn average_elements_per_second(&self) -> f64 {
        let samples = self.interval_samples.read().await;

        if samples.len() < 2 {
            return 0.0;
        }

        let first = &samples[0];
        let last = &samples[samples.len() - 1];

        let elapsed = (last.0 - first.0).num_milliseconds() as f64 / 1000.0;
        let elements = (last.1 - first.1) as f64;

        if elapsed > 0.0 {
            elements / elapsed
        } else {
            0.0
        }
    }

    /// Get the configured interval duration.
    pub fn interval_duration(&self) -> Duration {
        self.interval_duration
    }

    /// Get the peak throughput.
    pub async fn peak_elements_per_second(&self) -> f64 {
        let samples = self.interval_samples.read().await;

        if samples.len() < 2 {
            return 0.0;
        }

        let mut max_rate: f64 = 0.0;

        // Convert VecDeque to Vec to use windows()
        let samples_vec: Vec<_> = samples.iter().copied().collect();

        for window in samples_vec.windows(2) {
            let (t1, e1, _) = &window[0];
            let (t2, e2, _) = &window[1];

            let elapsed = (*t2 - *t1).num_milliseconds() as f64 / 1000.0;
            let elements = (e2 - e1) as f64;

            if elapsed > 0.0 {
                let rate = elements / elapsed;
                max_rate = max_rate.max(rate);
            }
        }

        max_rate
    }

    /// Clear all counters.
    pub async fn clear(&self) {
        *self.element_count.write().await = 0;
        *self.byte_count.write().await = 0;
        self.interval_samples.write().await.clear();
    }

    /// Reset the tracker.
    pub async fn reset(&self) {
        self.clear().await;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_performance_tracker() {
        let tracker = PerformanceTracker::new();

        tracker
            .checkpoint("start".to_string())
            .await
            .expect("Failed to record start checkpoint in test");
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        tracker
            .checkpoint("middle".to_string())
            .await
            .expect("Failed to record middle checkpoint in test");
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        tracker
            .checkpoint("end".to_string())
            .await
            .expect("Failed to record end checkpoint in test");

        let checkpoints = tracker.get_checkpoints().await;
        assert_eq!(checkpoints.len(), 3);
    }

    #[tokio::test]
    async fn test_latency_tracker() {
        let tracker = LatencyTracker::new(100);

        tracker.record(std::time::Duration::from_millis(10)).await;
        tracker.record(std::time::Duration::from_millis(20)).await;
        tracker.record(std::time::Duration::from_millis(30)).await;

        let avg = tracker
            .average()
            .await
            .expect("Failed to get average latency in test");
        assert!(avg >= std::time::Duration::from_millis(19));
        assert!(avg <= std::time::Duration::from_millis(21));

        let min = tracker
            .min()
            .await
            .expect("Failed to get minimum latency in test");
        assert_eq!(min, std::time::Duration::from_millis(10));

        let max = tracker
            .max()
            .await
            .expect("Failed to get maximum latency in test");
        assert_eq!(max, std::time::Duration::from_millis(30));
    }

    #[tokio::test]
    async fn test_throughput_tracker() {
        let tracker = ThroughputTracker::new(Duration::seconds(1), 10);

        tracker.record(100, 1000).await;

        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

        let eps = tracker.elements_per_second().await;
        assert!(eps > 0.0);

        let bps = tracker.bytes_per_second().await;
        assert!(bps > 0.0);
    }

    #[tokio::test]
    async fn test_latency_percentiles() {
        let tracker = LatencyTracker::new(100);

        for i in 1..=100 {
            tracker.record(std::time::Duration::from_millis(i)).await;
        }

        let p95 = tracker
            .p95()
            .await
            .expect("Failed to get 95th percentile latency in test");
        assert!(p95 >= std::time::Duration::from_millis(94));
        assert!(p95 <= std::time::Duration::from_millis(96));

        let p99 = tracker
            .p99()
            .await
            .expect("Failed to get 99th percentile latency in test");
        assert!(p99 >= std::time::Duration::from_millis(98));
    }
}