forked from Timi16/soroban-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorig_flamegraph.rs
More file actions
177 lines (149 loc) · 11.1 KB
/
Copy pathorig_flamegraph.rs
File metadata and controls
177 lines (149 loc) · 11.1 KB
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
use crate::profiler::analyzer::{FunctionProfile, OptimizationReport};
use crate::Result;
use std::io::Write;
#[derive(Debug, Clone)]
pub struct FlameGraphStack {
pub stack: Vec<String>,
pub count: u64,
}
pub struct FlameGraphGenerator;
impl FlameGraphGenerator {
pub fn from_report(report: &OptimizationReport) -> Vec<FlameGraphStack> {
let mut stacks = Vec::new();
for function in &report.functions {
let cpu_per_unit = if function.total_cpu > 0 {
function.total_cpu as f64 / 1000.0
} else {
1.0
};
let stack_count = (function.total_cpu as f64 / cpu_per_unit).max(1.0) as u64;
stacks.push(FlameGraphStack {
stack: vec![function.name.clone()],
count: stack_count,
});
for op in &function.operations {
let op_cost = (op.cpu_cost + op.memory_cost) as f64;
if op_cost > 0.0 {
let op_count = (op_cost / cpu_per_unit).max(1.0) as u64;
stacks.push(FlameGraphStack {
stack: vec![
function.name.clone(),
format!("{};{}", op.operation, op.location),
],
count: op_count,
});
}
}
for (idx, access) in function.storage_accesses.iter().enumerate() {
let cost = access.total_cpu as f64;
if cost > 0.0 {
let access_count = (cost / cpu_per_unit).max(1.0) as u64;
stacks.push(FlameGraphStack {
stack: vec![
function.name.clone(),
format!(
"storage;key{};access_count={}",
idx,
access.access_count
),
],
count: access_count,
});
}
}
}
stacks
}
pub fn to_collapsed_stack_format(stacks: &[FlameGraphStack]) -> String {
let mut output = String::new();
for stack in stacks {
let stack_str = stack.stack.join(";");
output.push_str(&format!("{} {}\n", stack_str, stack.count));
}
output
}
pub fn generate_svg(stacks: &[FlameGraphStack], width: usize, height: usize) -> Result<String> {
let collapsed = Self::to_collapsed_stack_format(stacks);
let reader = std::io::Cursor::new(collapsed);
let mut renderer = inferno::flamegraph::Renderer::default()
.width(width)
.height(height)
.image_width(width)
.font_size(12);
let mut svg = Vec::new();
renderer.render(reader, &mut svg)?;
Ok(String::from_utf8(svg)?)
}
pub fn write_collapsed_stack_file<P: AsRef<std::path::Path>>(
stacks: &[FlameGraphStack],
path: P,
) -> Result<()> {
let collapsed = Self::to_collapsed_stack_format(stacks);
std::fs::write(path, collapsed)?;
Ok(())
}
pub fn write_svg_file<P: AsRef<std::path::Path>>(
stacks: &[FlameGraphStack],
path: P,
width: usize,
height: usize,
) -> Result<()> {
let svg = Self::generate_svg(stacks, width, height)?;
std::fs::write(path, svg)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn create_test_report() -> OptimizationReport {
OptimizationReport {
contract_path: "/test/contract.wasm".to_string(),
functions: vec![FunctionProfile {
name: "test_function".to_string(),
total_cpu: 1000,
total_memory: 5000,
wall_time_ms: 100,
operations: vec![],
storage_accesses: HashMap::new(),
}],
suggestions: vec![],
total_cpu: 1000,
total_memory: 5000,
potential_cpu_savings: 0,
potential_memory_savings: 0,
}
}
#[test]
fn test_flame_graph_generation_from_report() {
let report = create_test_report();
let stacks = FlameGraphGenerator::from_report(&report);
assert!(!stacks.is_empty());
assert_eq!(stacks[0].stack[0], "test_function");
assert!(stacks[0].count > 0);
}
#[test]
fn test_collapsed_stack_format() {
let stacks = vec![FlameGraphStack {
stack: vec!["func1".to_string(), "func2".to_string()],
count: 42,
}];
let output = FlameGraphGenerator::to_collapsed_stack_format(&stacks);
assert!(output.contains("func1;func2 42"));
}
#[test]
fn test_write_collapsed_stack_file() {
let stacks = vec![FlameGraphStack {
stack: vec!["test_func".to_string()],
count: 100,
}];
let temp_dir = std::env::temp_dir();
let file_path = temp_dir.join("test_flamegraph.stacks");
assert!(FlameGraphGenerator::write_collapsed_stack_file(&stacks, &file_path).is_ok());
assert!(file_path.exists());
let content = std::fs::read_to_string(&file_path).unwrap();
assert!(content.contains("test_func 100"));
let _ = std::fs::remove_file(&file_path);
}
}