async-inspect 0.2.0

X-ray vision for async Rust - inspect and debug async state machines
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Performance comparison between runs
//!
//! This module provides functionality to compare performance metrics across multiple runs,
//! detect regressions, and identify performance improvements.

use super::{DurationStats, LockContentionMetrics, TaskMetrics};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// A snapshot of performance metrics from a single run
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceSnapshot {
    /// Timestamp when the snapshot was taken
    pub timestamp: u64,

    /// Run identifier
    pub run_id: String,

    /// Overall task statistics
    pub task_stats: DurationStats,

    /// All task metrics
    pub task_metrics: Vec<TaskMetrics>,

    /// Lock contention metrics
    pub lock_metrics: Vec<LockContentionMetrics>,

    /// Total tasks executed
    pub total_tasks: usize,

    /// Average task duration
    pub avg_task_duration: Duration,

    /// Total execution time
    pub total_execution_time: Duration,
}

impl PerformanceSnapshot {
    /// Create a new performance snapshot
    #[must_use]
    pub fn new(run_id: String) -> Self {
        Self {
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            run_id,
            task_stats: DurationStats {
                min: Duration::ZERO,
                max: Duration::ZERO,
                mean: Duration::ZERO,
                median: Duration::ZERO,
                p95: Duration::ZERO,
                p99: Duration::ZERO,
                std_dev: 0.0,
                count: 0,
            },
            task_metrics: Vec::new(),
            lock_metrics: Vec::new(),
            total_tasks: 0,
            avg_task_duration: Duration::ZERO,
            total_execution_time: Duration::ZERO,
        }
    }

    /// Save snapshot to file
    pub fn save_to_file(&self, path: &str) -> Result<(), std::io::Error> {
        let json = serde_json::to_string_pretty(self)?;
        std::fs::write(path, json)?;
        Ok(())
    }

    /// Load snapshot from file
    pub fn load_from_file(path: &str) -> Result<Self, std::io::Error> {
        let json = std::fs::read_to_string(path)?;
        let snapshot: Self = serde_json::from_str(&json)?;
        Ok(snapshot)
    }
}

/// Comparison result between two runs
#[derive(Debug, Clone)]
pub struct PerformanceComparison {
    /// Baseline snapshot
    pub baseline: PerformanceSnapshot,

    /// Current snapshot
    pub current: PerformanceSnapshot,

    /// Task duration changes
    pub task_duration_change: DurationChange,

    /// Lock contention changes
    pub lock_contention_changes: Vec<LockContentionChange>,

    /// Overall regression/improvement status
    pub status: ComparisonStatus,

    /// Detailed findings
    pub findings: Vec<Finding>,
}

/// Status of the comparison
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComparisonStatus {
    /// Performance improved
    Improved,
    /// Performance degraded (regression)
    Regressed,
    /// Performance is similar
    Similar,
    /// Mixed results
    Mixed,
}

/// Change in duration metrics
#[derive(Debug, Clone)]
pub struct DurationChange {
    /// Mean duration change
    pub mean_change: f64,

    /// Median duration change
    pub median_change: f64,

    /// P95 duration change
    pub p95_change: f64,

    /// P99 duration change
    pub p99_change: f64,

    /// Whether this represents an improvement
    pub is_improvement: bool,
}

/// Change in lock contention
#[derive(Debug, Clone)]
pub struct LockContentionChange {
    /// Resource name
    pub resource_name: String,

    /// Baseline contention rate
    pub baseline_rate: f64,

    /// Current contention rate
    pub current_rate: f64,

    /// Change in contention rate
    pub rate_change: f64,

    /// Change in wait time
    pub wait_time_change: f64,

    /// Whether this represents an improvement
    pub is_improvement: bool,
}

/// A specific finding from the comparison
#[derive(Debug, Clone)]
pub struct Finding {
    /// Severity of the finding
    pub severity: FindingSeverity,

    /// Category of the finding
    pub category: FindingCategory,

    /// Description
    pub description: String,

    /// Impact percentage (positive = improvement, negative = regression)
    pub impact_percent: f64,
}

/// Severity of a finding
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FindingSeverity {
    /// Critical regression (>50% degradation)
    Critical,
    /// Major issue (20-50% degradation)
    Major,
    /// Minor issue (5-20% degradation)
    Minor,
    /// Informational
    Info,
}

/// Category of a finding
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FindingCategory {
    /// Task execution time
    TaskDuration,
    /// Lock contention
    LockContention,
    /// Overall throughput
    Throughput,
    /// Other
    Other,
}

impl PerformanceComparison {
    /// Compare two performance snapshots
    #[must_use]
    pub fn compare(baseline: PerformanceSnapshot, current: PerformanceSnapshot) -> Self {
        let task_duration_change =
            Self::compare_durations(&baseline.task_stats, &current.task_stats);
        let lock_contention_changes =
            Self::compare_locks(&baseline.lock_metrics, &current.lock_metrics);
        let findings = Self::generate_findings(
            &baseline,
            &current,
            &task_duration_change,
            &lock_contention_changes,
        );
        let status = Self::determine_status(&findings);

        Self {
            baseline,
            current,
            task_duration_change,
            lock_contention_changes,
            status,
            findings,
        }
    }

    fn compare_durations(baseline: &DurationStats, current: &DurationStats) -> DurationChange {
        let mean_change =
            Self::calculate_change_percent(baseline.mean.as_secs_f64(), current.mean.as_secs_f64());

        let median_change = Self::calculate_change_percent(
            baseline.median.as_secs_f64(),
            current.median.as_secs_f64(),
        );

        let p95_change =
            Self::calculate_change_percent(baseline.p95.as_secs_f64(), current.p95.as_secs_f64());

        let p99_change =
            Self::calculate_change_percent(baseline.p99.as_secs_f64(), current.p99.as_secs_f64());

        let is_improvement = mean_change < 0.0 && median_change < 0.0;

        DurationChange {
            mean_change,
            median_change,
            p95_change,
            p99_change,
            is_improvement,
        }
    }

    fn compare_locks(
        baseline: &[LockContentionMetrics],
        current: &[LockContentionMetrics],
    ) -> Vec<LockContentionChange> {
        let mut changes = Vec::new();
        let baseline_map: HashMap<_, _> = baseline.iter().map(|m| (&m.name, m)).collect();

        for current_metric in current {
            if let Some(baseline_metric) = baseline_map.get(&current_metric.name) {
                let rate_change = Self::calculate_change_percent(
                    baseline_metric.contention_rate,
                    current_metric.contention_rate,
                );

                let wait_time_change = Self::calculate_change_percent(
                    baseline_metric.avg_wait_time.as_secs_f64(),
                    current_metric.avg_wait_time.as_secs_f64(),
                );

                changes.push(LockContentionChange {
                    resource_name: current_metric.name.clone(),
                    baseline_rate: baseline_metric.contention_rate,
                    current_rate: current_metric.contention_rate,
                    rate_change,
                    wait_time_change,
                    is_improvement: rate_change < 0.0 && wait_time_change < 0.0,
                });
            }
        }

        changes
    }

    fn generate_findings(
        baseline: &PerformanceSnapshot,
        current: &PerformanceSnapshot,
        duration_change: &DurationChange,
        lock_changes: &[LockContentionChange],
    ) -> Vec<Finding> {
        let mut findings = Vec::new();

        // Check task duration changes
        if duration_change.mean_change.abs() > 5.0 {
            let severity = if duration_change.mean_change.abs() > 50.0 {
                FindingSeverity::Critical
            } else if duration_change.mean_change.abs() > 20.0 {
                FindingSeverity::Major
            } else {
                FindingSeverity::Minor
            };

            findings.push(Finding {
                severity,
                category: FindingCategory::TaskDuration,
                description: format!(
                    "Mean task duration changed by {:.1}%",
                    duration_change.mean_change
                ),
                impact_percent: -duration_change.mean_change, // Negative change is improvement
            });
        }

        // Check lock contention changes
        for lock_change in lock_changes {
            if lock_change.rate_change.abs() > 10.0 {
                let severity = if lock_change.rate_change.abs() > 50.0 {
                    FindingSeverity::Major
                } else {
                    FindingSeverity::Minor
                };

                findings.push(Finding {
                    severity,
                    category: FindingCategory::LockContention,
                    description: format!(
                        "Lock '{}' contention changed by {:.1}%",
                        lock_change.resource_name, lock_change.rate_change
                    ),
                    impact_percent: -lock_change.rate_change,
                });
            }
        }

        // Check throughput
        if baseline.total_tasks > 0 && current.total_tasks > 0 {
            let throughput_change = Self::calculate_change_percent(
                baseline.total_tasks as f64,
                current.total_tasks as f64,
            );

            if throughput_change.abs() > 5.0 {
                findings.push(Finding {
                    severity: FindingSeverity::Info,
                    category: FindingCategory::Throughput,
                    description: format!("Throughput changed by {throughput_change:.1}%"),
                    impact_percent: throughput_change,
                });
            }
        }

        findings
    }

    fn determine_status(findings: &[Finding]) -> ComparisonStatus {
        let regressions = findings.iter().filter(|f| f.impact_percent < 0.0).count();
        let improvements = findings.iter().filter(|f| f.impact_percent > 0.0).count();

        if regressions == 0 && improvements > 0 {
            ComparisonStatus::Improved
        } else if regressions > 0 && improvements == 0 {
            ComparisonStatus::Regressed
        } else if regressions == 0 && improvements == 0 {
            ComparisonStatus::Similar
        } else {
            ComparisonStatus::Mixed
        }
    }

    fn calculate_change_percent(baseline: f64, current: f64) -> f64 {
        if baseline == 0.0 {
            return 0.0;
        }
        ((current - baseline) / baseline) * 100.0
    }

    /// Check if there are any regressions
    #[must_use]
    pub fn has_regressions(&self) -> bool {
        self.findings.iter().any(|f| {
            matches!(
                f.severity,
                FindingSeverity::Critical | FindingSeverity::Major
            ) && f.impact_percent < 0.0
        })
    }

    /// Get all regressions
    #[must_use]
    pub fn get_regressions(&self) -> Vec<&Finding> {
        self.findings
            .iter()
            .filter(|f| f.impact_percent < 0.0)
            .collect()
    }

    /// Get all improvements
    #[must_use]
    pub fn get_improvements(&self) -> Vec<&Finding> {
        self.findings
            .iter()
            .filter(|f| f.impact_percent > 0.0)
            .collect()
    }

    /// Generate a summary report
    #[must_use]
    pub fn summary(&self) -> String {
        let mut report = String::from("Performance Comparison Summary\n");
        report.push_str("==============================\n\n");

        report.push_str(&format!("Status: {:?}\n\n", self.status));

        report.push_str("Task Duration Changes:\n");
        report.push_str(&format!(
            "  Mean: {:.1}%\n",
            self.task_duration_change.mean_change
        ));
        report.push_str(&format!(
            "  Median: {:.1}%\n",
            self.task_duration_change.median_change
        ));
        report.push_str(&format!(
            "  P95: {:.1}%\n",
            self.task_duration_change.p95_change
        ));
        report.push_str(&format!(
            "  P99: {:.1}%\n\n",
            self.task_duration_change.p99_change
        ));

        if !self.lock_contention_changes.is_empty() {
            report.push_str("Lock Contention Changes:\n");
            for change in &self.lock_contention_changes {
                report.push_str(&format!(
                    "  {}: {:.1}% (wait time: {:.1}%)\n",
                    change.resource_name, change.rate_change, change.wait_time_change
                ));
            }
            report.push('\n');
        }

        if !self.findings.is_empty() {
            report.push_str("Findings:\n");
            for finding in &self.findings {
                report.push_str(&format!(
                    "  [{:?}] {}\n",
                    finding.severity, finding.description
                ));
            }
        }

        report
    }
}

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

    #[test]
    fn test_calculate_change_percent() {
        assert_eq!(
            PerformanceComparison::calculate_change_percent(100.0, 110.0),
            10.0
        );
        assert_eq!(
            PerformanceComparison::calculate_change_percent(100.0, 90.0),
            -10.0
        );
        assert_eq!(
            PerformanceComparison::calculate_change_percent(0.0, 10.0),
            0.0
        );
    }

    #[test]
    fn test_snapshot_serialization() {
        let snapshot = PerformanceSnapshot::new("test_run".to_string());
        let json = serde_json::to_string(&snapshot).unwrap();
        let deserialized: PerformanceSnapshot = serde_json::from_str(&json).unwrap();
        assert_eq!(snapshot.run_id, deserialized.run_id);
    }
}