Skip to main content

ass_renderer/debug/benchmarking/
report.rs

1//! Performance report and summary statistics generation
2
3#[cfg(feature = "nostd")]
4use alloc::{string::String, vec::Vec};
5#[cfg(not(feature = "nostd"))]
6use std::{string::String, vec::Vec};
7
8use super::{PerformanceBenchmark, PerformanceReport, PerformanceSummary};
9
10impl PerformanceBenchmark {
11    /// Generate performance report
12    pub fn generate_performance_report(&self) -> PerformanceReport {
13        PerformanceReport {
14            results: self.historical_results.clone(),
15            summary: self.calculate_performance_summary(),
16        }
17    }
18
19    /// Calculate performance summary statistics
20    fn calculate_performance_summary(&self) -> PerformanceSummary {
21        if self.historical_results.is_empty() {
22            return PerformanceSummary::default();
23        }
24
25        let total_tests = self.historical_results.len();
26        let performance_ratios: Vec<f64> = self
27            .historical_results
28            .iter()
29            .filter_map(|r| r.performance_ratio)
30            .collect();
31
32        let avg_performance_ratio = if !performance_ratios.is_empty() {
33            performance_ratios.iter().sum::<f64>() / performance_ratios.len() as f64
34        } else {
35            1.0
36        };
37
38        let avg_compatibility = self
39            .historical_results
40            .iter()
41            .map(|r| r.compatibility_score)
42            .sum::<f64>()
43            / total_tests as f64;
44
45        let avg_fps = self
46            .historical_results
47            .iter()
48            .filter_map(|r| r.our_performance.fps)
49            .sum::<f64>()
50            / total_tests as f64;
51
52        PerformanceSummary {
53            total_tests,
54            avg_performance_ratio,
55            avg_compatibility_score: avg_compatibility,
56            avg_fps,
57            fastest_test: self.find_fastest_test(),
58            slowest_test: self.find_slowest_test(),
59        }
60    }
61
62    /// Find fastest test
63    fn find_fastest_test(&self) -> Option<String> {
64        self.historical_results
65            .iter()
66            .min_by(|a, b| {
67                a.our_performance
68                    .avg_render_time_ms
69                    .partial_cmp(&b.our_performance.avg_render_time_ms)
70                    .unwrap_or(std::cmp::Ordering::Equal)
71            })
72            .map(|r| r.test_name.clone())
73    }
74
75    /// Find slowest test
76    fn find_slowest_test(&self) -> Option<String> {
77        self.historical_results
78            .iter()
79            .max_by(|a, b| {
80                a.our_performance
81                    .avg_render_time_ms
82                    .partial_cmp(&b.our_performance.avg_render_time_ms)
83                    .unwrap_or(std::cmp::Ordering::Equal)
84            })
85            .map(|r| r.test_name.clone())
86    }
87}