162 lines
4.8 KiB
Rust
162 lines
4.8 KiB
Rust
// 代码优化
|
|
// 提供性能优化建议和热点分析
|
|
|
|
use std::collections::HashMap;
|
|
|
|
/// 优化建议类型
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum OptimizationType {
|
|
CachingImprovement, // 缓存改进
|
|
AlgorithmOptimization, // 算法优化
|
|
ConcurrencyEnhancement,// 并发增强
|
|
MemoryOptimization, // 内存优化
|
|
IOOptimization, // I/O优化
|
|
}
|
|
|
|
/// 优化建议
|
|
#[derive(Debug, Clone)]
|
|
pub struct OptimizationSuggestion {
|
|
pub optimization_type: OptimizationType,
|
|
pub description: String,
|
|
pub priority: u8, // 1-10, 10最高
|
|
pub estimated_improvement: f64, // 预期改进百分比
|
|
}
|
|
|
|
/// 热点分析结果
|
|
#[derive(Debug, Clone)]
|
|
pub struct HotspotAnalysis {
|
|
pub function_name: String,
|
|
pub execution_count: u64,
|
|
pub total_time_ms: f64,
|
|
pub avg_time_ms: f64,
|
|
}
|
|
|
|
/// 代码优化系统
|
|
pub struct CodeOptimizer {
|
|
suggestions: Vec<OptimizationSuggestion>,
|
|
hotspots: Vec<HotspotAnalysis>,
|
|
}
|
|
|
|
impl CodeOptimizer {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
suggestions: Vec::new(),
|
|
hotspots: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// 分析热点
|
|
pub fn analyze_hotspots(&mut self, function_name: String, execution_count: u64, total_time_ms: f64) {
|
|
let hotspot = HotspotAnalysis {
|
|
function_name,
|
|
execution_count,
|
|
total_time_ms,
|
|
avg_time_ms: total_time_ms / execution_count as f64,
|
|
};
|
|
|
|
self.hotspots.push(hotspot);
|
|
}
|
|
|
|
/// 添加优化建议
|
|
pub fn add_suggestion(&mut self, suggestion: OptimizationSuggestion) {
|
|
self.suggestions.push(suggestion);
|
|
}
|
|
|
|
/// 生成优化报告
|
|
pub fn generate_optimization_report(&self) -> String {
|
|
let mut report = String::from("=== 代码优化报告 ===\n\n");
|
|
|
|
// 热点分析
|
|
report.push_str("热点函数分析:\n");
|
|
let mut sorted_hotspots = self.hotspots.clone();
|
|
sorted_hotspots.sort_by(|a, b| b.total_time_ms.partial_cmp(&a.total_time_ms).unwrap());
|
|
|
|
for (i, hotspot) in sorted_hotspots.iter().take(5).enumerate() {
|
|
report.push_str(&format!("{}. {} - 执行{}次, 总耗时{:.2}ms, 平均{:.2}ms\n",
|
|
i + 1,
|
|
hotspot.function_name,
|
|
hotspot.execution_count,
|
|
hotspot.total_time_ms,
|
|
hotspot.avg_time_ms
|
|
));
|
|
}
|
|
|
|
// 优化建议
|
|
report.push_str("\n优化建议:\n");
|
|
let mut sorted_suggestions = self.suggestions.clone();
|
|
sorted_suggestions.sort_by(|a, b| b.priority.cmp(&a.priority));
|
|
|
|
for (i, suggestion) in sorted_suggestions.iter().enumerate() {
|
|
report.push_str(&format!("{}. [优先级{}] {:?}: {}\n 预期改进: {:.1}%\n",
|
|
i + 1,
|
|
suggestion.priority,
|
|
suggestion.optimization_type,
|
|
suggestion.description,
|
|
suggestion.estimated_improvement
|
|
));
|
|
}
|
|
|
|
report
|
|
}
|
|
|
|
/// 获取热点列表
|
|
pub fn get_hotspots(&self) -> &[HotspotAnalysis] {
|
|
&self.hotspots
|
|
}
|
|
|
|
/// 获取优化建议
|
|
pub fn get_suggestions(&self) -> &[OptimizationSuggestion] {
|
|
&self.suggestions
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_hotspot_analysis() {
|
|
let mut optimizer = CodeOptimizer::new();
|
|
|
|
optimizer.analyze_hotspots("validate_transaction".to_string(), 1000, 500.0);
|
|
optimizer.analyze_hotspots("execute_contract".to_string(), 500, 1000.0);
|
|
|
|
let hotspots = optimizer.get_hotspots();
|
|
assert_eq!(hotspots.len(), 2);
|
|
assert_eq!(hotspots[0].avg_time_ms, 0.5);
|
|
}
|
|
|
|
#[test]
|
|
fn test_optimization_suggestions() {
|
|
let mut optimizer = CodeOptimizer::new();
|
|
|
|
optimizer.add_suggestion(OptimizationSuggestion {
|
|
optimization_type: OptimizationType::CachingImprovement,
|
|
description: "添加LRU缓存".to_string(),
|
|
priority: 8,
|
|
estimated_improvement: 30.0,
|
|
});
|
|
|
|
let suggestions = optimizer.get_suggestions();
|
|
assert_eq!(suggestions.len(), 1);
|
|
assert_eq!(suggestions[0].priority, 8);
|
|
}
|
|
|
|
#[test]
|
|
fn test_generate_report() {
|
|
let mut optimizer = CodeOptimizer::new();
|
|
|
|
optimizer.analyze_hotspots("test_function".to_string(), 100, 100.0);
|
|
optimizer.add_suggestion(OptimizationSuggestion {
|
|
optimization_type: OptimizationType::AlgorithmOptimization,
|
|
description: "使用更高效的算法".to_string(),
|
|
priority: 9,
|
|
estimated_improvement: 50.0,
|
|
});
|
|
|
|
let report = optimizer.generate_optimization_report();
|
|
assert!(report.contains("代码优化报告"));
|
|
assert!(report.contains("test_function"));
|
|
}
|
|
}
|