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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Performance Analysis Module
use std::time::Duration;
use crate::global_ts_manager;
/// High-performance monitoring interface
#[derive(Debug, Clone)]
pub struct Perf;
impl Perf {
/// Enable performance monitoring
pub fn enable() {
tracing::info!("π usls Performance monitoring enabled");
}
/// Disable performance monitoring
pub fn disable() {
tracing::info!("βΈοΈ usls Performance monitoring disabled");
}
/// Check if monitoring is enabled
pub fn is_enabled() -> bool {
global_ts_manager().is_enabled()
}
/// Get performance statistics
pub fn stats() -> Option<(Duration, Duration, usize)> {
global_ts_manager().global_stats()
}
/// Clear all performance data
pub fn clear() {
global_ts_manager().clear_all();
tracing::info!("π§Ή Performance data cleared");
}
/// Export performance data as JSON
pub fn export_json() -> Result<String, Box<dyn std::error::Error>> {
use serde_json::json;
let stats = Self::stats();
let data = json!({
"timestamp": chrono::Utc::now().to_rfc3339(),
"enabled": Self::is_enabled(),
"global_stats": stats.map(|(total, avg, count)| json!({
"total_time_ms": total.as_millis(),
"average_time_ms": avg.as_millis(),
"operation_count": count
}))
});
Ok(serde_json::to_string_pretty(&data)?)
}
/// Show traditional table format
pub fn table() {
global_ts_manager().print_enhanced_summary();
}
/// Show ASCII chart visualization
pub fn ascii() {
Self::show_ascii_chart();
}
/// Show performance data with optional table
pub fn show(show_table: bool) {
Self::show_ascii_chart();
if show_table {
Self::table();
}
}
/// Show enhanced ASCII performance chart with detailed breakdown
fn show_ascii_chart() {
let manager = global_ts_manager();
let mut has_data = false;
// Detailed data collection - using Duration to preserve precision
let mut dataloader_details = Vec::new();
let mut model_details = Vec::new();
let mut annotator_details = Vec::new();
let mut dataloader_total = Duration::ZERO;
let mut _model_total = Duration::ZERO;
let mut annotator_total = Duration::ZERO;
let mut _total_images = 0;
// Collect global data with detailed breakdown
if let Ok(ts) = manager.global().lock() {
if !ts.is_empty() {
has_data = true;
for name in ts.get_names() {
let durations = &ts[name.as_str()];
let total_duration = durations.iter().sum::<Duration>();
let count = durations.len();
let avg_duration = if count > 0 {
total_duration / count as u32
} else {
Duration::ZERO
};
if name.contains("load") || name.contains("read") {
dataloader_total += total_duration;
dataloader_details.push((
name.clone(),
total_duration,
count,
avg_duration,
));
if name.contains("image") || name.contains("load") {
_total_images += count;
}
} else if name.contains("annotate")
|| name.contains("draw")
|| name.contains("render")
{
annotator_total += total_duration;
annotator_details.push((name.clone(), total_duration, count, avg_duration));
} else {
_model_total += total_duration;
model_details.push((name.clone(), total_duration, count, avg_duration));
}
}
}
}
// Collect module data with detailed breakdown - dynamically get all registered modules
let module_names = manager.get_module_names();
for module_name in module_names {
if let Ok(ts) = manager.module(&module_name).lock() {
if !ts.is_empty() {
has_data = true;
for name in ts.get_names() {
let durations = &ts[name.as_str()];
let total_duration = durations.iter().sum::<Duration>();
let count = durations.len();
let avg_duration = if count > 0 {
total_duration / count as u32
} else {
Duration::ZERO
};
let full_name = format!("{}::{}", module_name, name);
// Categorize based on module type
if module_name == "DATALOADER"
|| name.contains("load")
|| name.contains("read")
{
dataloader_total += total_duration;
dataloader_details.push((
full_name,
total_duration,
count,
avg_duration,
));
if name.contains("image") || name.contains("load") {
_total_images += count;
}
} else if module_name == "ANNOTATOR"
|| name.contains("annotate")
|| name.contains("draw")
|| name.contains("render")
{
annotator_total += total_duration;
annotator_details.push((
full_name,
total_duration,
count,
avg_duration,
));
} else if module_name != "ENGINE" {
// All non-system modules are considered model modules
_model_total += total_duration;
model_details.push((full_name, total_duration, count, avg_duration));
}
}
}
}
}
if !has_data {
println!("\nπ No performance data available");
return;
}
println!("\n\nπ usls Performance Analysis Chart");
println!("βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n");
// Extract unique model names from model_details
let mut model_totals: std::collections::HashMap<String, Duration> =
std::collections::HashMap::new();
// Preserve Duration precision throughout calculation
for (name, total_duration, _, _) in &model_details {
if name.contains("::") {
// Extract from MODULE::task format
let parts: Vec<&str> = name.split("::").collect();
if parts.len() >= 2 {
let module_name = parts[0];
*model_totals
.entry(module_name.to_string())
.or_insert(Duration::ZERO) += *total_duration;
}
}
}
// Build main category data with individual models
let mut main_data: Vec<(String, Duration)> =
vec![("π Data Loading".to_string(), dataloader_total)];
// Add each model as a separate category
let mut sorted_models: Vec<_> = model_totals.into_iter().collect();
sorted_models.sort_by(|a, b| b.1.cmp(&a.1));
for (model_name, total_time) in sorted_models {
main_data.push((format!("π€ {}", model_name), total_time));
}
main_data.push(("π¨ Visualization".to_string(), annotator_total));
let main_data: Vec<_> = main_data
.into_iter()
.filter(|(_, v)| *v > Duration::ZERO)
.collect();
if main_data.is_empty() {
println!(" No categorized performance data available");
return;
}
let max_value = main_data
.iter()
.map(|(_, v)| *v)
.max()
.unwrap_or(Duration::ZERO);
let chart_width = 40;
// Calculate total time for percentage calculation
let total_time: Duration = main_data.iter().map(|(_, v)| *v).sum();
// Calculate the maximum display width for intelligent alignment
// Use character count instead of byte length for proper emoji handling
let mut max_display_width = main_data
.iter()
.map(|(n, _)| n.chars().count())
.max()
.unwrap_or(0);
// Also consider sub-item lengths for proper alignment
for (name, _) in &main_data {
if !name.contains("Data Loading") && !name.contains("Visualization") {
// Extract model name without emoji for filtering
let model_name = if let Some(stripped) = name.strip_prefix("π€ ") {
stripped // Skip "π€ " prefix
} else {
name
};
// Calculate sub-item lengths for this model using the same logic as print_tree_breakdown
let model_filter_lower = model_name.to_lowercase();
for (detail_name, _, _, _) in &model_details {
if let Some(module_end) = detail_name.find("::") {
let module_name = &detail_name[..module_end];
if module_name.to_lowercase() == model_filter_lower {
let task_name = &detail_name[module_end + 2..];
let formatted_task = format_task_with_emoji(task_name);
// Format: " ββ 𧬠textual-inference" or " ββ 𧬠textual-inference"
let tree_prefix = format!(" ββ {}", formatted_task);
max_display_width = max_display_width.max(tree_prefix.chars().count());
}
}
}
}
}
// Dynamic minimum width: longest item + 1 for better spacing
let alignment_width = max_display_width + 1;
// Find the longest bar length for alignment
let max_bar_length = main_data
.iter()
.map(|(_, value)| {
if max_value > Duration::ZERO {
let ratio = value.as_nanos() as f64 / max_value.as_nanos() as f64;
let calculated_length = (ratio * chart_width as f64) as usize;
calculated_length.max(3) // Ensure minimum visibility
} else {
3
}
})
.max()
.unwrap_or(chart_width);
for (name, value) in &main_data {
// Ensure minimum bar length for visibility
let min_main_bar_length = 3;
let bar_length = if max_value > Duration::ZERO {
let ratio = value.as_nanos() as f64 / max_value.as_nanos() as f64;
let calculated_length = (ratio * chart_width as f64) as usize;
calculated_length.max(min_main_bar_length) // Ensure minimum visibility
} else {
min_main_bar_length
};
let bar = "β".repeat(bar_length);
// Calculate percentage
let percentage = if total_time > Duration::ZERO {
(value.as_nanos() as f64 / total_time.as_nanos() as f64) * 100.0
} else {
0.0
};
// Calculate padding to align values from the longest bar position
let value_start_position = alignment_width + 1 + max_bar_length; // +1 for the β separator
let current_position = alignment_width + 1 + bar_length;
let base_padding = if value_start_position > current_position {
value_start_position - current_position
} else {
1 // At least one space
};
// Add extra space for non-longest bars
let extra_padding = if bar_length < max_bar_length { 1 } else { 0 };
let padding = " ".repeat(base_padding + extra_padding);
println!(
"{:<width$}β{}{}{:.3?} ({:.2}%)",
name,
bar,
padding,
value,
percentage,
width = alignment_width,
);
// Add tree-style breakdown for model inference categories
if !name.contains("Data Loading") && !name.contains("Visualization") {
// Extract model name without emoji for filtering
let model_name = if let Some(stripped) = name.strip_prefix("π€ ") {
stripped // Skip "π€ " prefix
} else {
name
};
print_tree_breakdown(
&model_details,
model_name,
max_value,
chart_width,
alignment_width,
);
}
}
// Use the already calculated max_bar_length for scale line
println!(
"{:<width$}β{}",
"",
"β".repeat(max_bar_length),
width = alignment_width + 1
);
println!(
"{:<width$}0{:>bar_width$} {:.3?}",
"",
"",
total_time,
width = alignment_width + 1,
bar_width = max_bar_length.saturating_sub(3), // -3 to account for the extra 2 spaces
);
println!();
println!();
}
}
// Helper function to format task name with appropriate emoji
fn format_task_with_emoji(task_name: &str) -> String {
use rand::prelude::*;
let lower = task_name.to_lowercase();
let fallback_emojis = ["π΄", "π ", "π‘", "π’", "π΅", "π£", "β«οΈ", "βͺοΈ", "π€"];
let emoji = match lower.as_str() {
name if name.contains("preprocess") => "π§",
name if name.contains("postprocess") => "π¦",
name if name.contains("inference") || name.contains("forward") => "π§¬",
name if name.contains("generate") => "π²",
_ => fallback_emojis.choose(&mut rand::rng()).map_or("π’", |v| v),
};
format!("{} {}", emoji, task_name)
}
// Helper function to print tree-style model breakdown
fn print_tree_breakdown(
model_details: &[(String, Duration, usize, Duration)],
model_filter: &str,
max_value: Duration,
chart_width: usize,
alignment_width: usize,
) {
// Collect tasks that belong to this model
let mut tasks: Vec<(String, Duration)> = Vec::new();
let model_filter_lower = model_filter.to_lowercase();
for (name, _total_duration, _count, avg_duration) in model_details {
// Check if this task belongs to the current model
// Format: "module::task" where module matches model_filter (case insensitive)
if let Some(module_end) = name.find("::") {
let module_name = &name[..module_end];
if module_name.to_lowercase() == model_filter_lower {
// Convert to display format: MODULE::task with appropriate emoji
let task_name = &name[module_end + 2..];
let formatted_task = format_task_with_emoji(task_name);
tasks.push((formatted_task, *avg_duration));
}
}
}
if tasks.is_empty() {
return;
}
// Sort by time (descending)
tasks.sort_by(|a, b| b.1.cmp(&a.1));
// Print tree-style breakdown using the same scale as main chart
for (i, (task_name, avg_time)) in tasks.iter().enumerate() {
let is_last = i == tasks.len() - 1;
let branch = if is_last { "ββ" } else { "ββ" };
// Calculate bar length using the same scale as main chart
let min_bar_length = 8; // Minimum bar length for visibility
let bar_length = if max_value > Duration::ZERO {
let ratio = avg_time.as_nanos() as f64 / max_value.as_nanos() as f64;
let calculated_length = (ratio * chart_width as f64) as usize;
calculated_length.max(min_bar_length) // Ensure minimum visibility
} else {
min_bar_length
};
let bar = "β".repeat(bar_length);
// Calculate consistent spacing for tree items with intelligent alignment
let tree_prefix = format!(" {} {}", branch, task_name);
// Calculate padding to align values from the longest bar position
let value_start_position = alignment_width + 1 + chart_width; // +1 for the β separator
let current_position = alignment_width + 1 + bar_length;
let base_padding = if value_start_position > current_position {
value_start_position - current_position
} else {
1 // At least one space
};
// Add extra space for non-longest bars (sub-items always get extra space)
let extra_padding = 1;
let padding = " ".repeat(base_padding + extra_padding);
println!(
"{:<width$}β{}{}{:.3?}",
tree_prefix,
bar,
padding,
avg_time,
width = alignment_width
);
}
}
/// Show performance data with optional table
/// Default shows ASCII chart, set show_table=true to include detailed table
pub fn perf(show_table: bool) {
Perf::show(show_table);
}
/// Show traditional table format
pub fn table() {
Perf::table();
}
/// Show ASCII chart visualization
pub fn ascii() {
Perf::ascii();
}
/// Enable performance monitoring
pub fn enable() {
Perf::enable();
}
/// Disable performance monitoring
pub fn disable() {
Perf::disable();
}
/// Check if monitoring is enabled
pub fn is_enabled() -> bool {
Perf::is_enabled()
}
/// Get performance statistics
pub fn stats() -> Option<(Duration, Duration, usize)> {
Perf::stats()
}
/// Clear all performance data
pub fn clear() {
Perf::clear();
}
/// Export performance data to JSON
pub fn export_json() -> Result<String, Box<dyn std::error::Error>> {
Perf::export_json()
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
use std::time::Duration;
#[test]
fn test_perf_interface() {
assert!(is_enabled());
// Simulate operation
crate::elapsed_global!("test_op", {
thread::sleep(Duration::from_millis(1));
});
// Test all visualization modes
perf(false); // ASCII only
perf(true); // ASCII + table
ascii();
table();
clear();
}
}