torsh-profiler 0.1.2

Performance profiling and monitoring for ToRSh
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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Grafana dashboard integration for torsh-profiler
//!
//! This module provides Grafana dashboard generation and management functionality,
//! allowing automatic creation of monitoring dashboards from profiling data.

use crate::{TorshError, TorshResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Grafana dashboard generator
pub struct GrafanaDashboardGenerator {
    dashboard: Dashboard,
}

/// Complete Grafana dashboard structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Dashboard {
    pub title: String,
    pub uid: Option<String>,
    pub tags: Vec<String>,
    pub timezone: String,
    pub editable: bool,
    pub panels: Vec<Panel>,
    pub templating: Templating,
    pub time: TimeRange,
    pub refresh: String,
    #[serde(rename = "schemaVersion")]
    pub schema_version: u32,
    pub version: u32,
}

/// Grafana panel configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Panel {
    pub id: u32,
    pub title: String,
    #[serde(rename = "type")]
    pub panel_type: String,
    pub datasource: String,
    pub targets: Vec<Target>,
    #[serde(rename = "gridPos")]
    pub grid_pos: GridPos,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub field_config: Option<FieldConfig>,
}

/// Prometheus query target
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Target {
    pub expr: String,
    #[serde(rename = "legendFormat")]
    pub legend_format: String,
    #[serde(rename = "refId")]
    pub ref_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interval: Option<String>,
}

/// Panel grid position
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GridPos {
    pub h: u32, // height
    pub w: u32, // width
    pub x: u32, // x position
    pub y: u32, // y position
}

/// Dashboard templating configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Templating {
    pub list: Vec<Variable>,
}

/// Dashboard variable
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Variable {
    pub name: String,
    #[serde(rename = "type")]
    pub var_type: String,
    pub datasource: String,
    pub query: String,
    pub multi: bool,
    #[serde(rename = "includeAll")]
    pub include_all: bool,
}

/// Time range configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeRange {
    pub from: String,
    pub to: String,
}

/// Field configuration for panels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldConfig {
    pub defaults: FieldDefaults,
    pub overrides: Vec<serde_json::Value>,
}

/// Default field configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldDefaults {
    pub unit: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thresholds: Option<Thresholds>,
}

/// Threshold configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Thresholds {
    pub mode: String,
    pub steps: Vec<ThresholdStep>,
}

/// Individual threshold step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdStep {
    pub value: f64,
    pub color: String,
}

impl GrafanaDashboardGenerator {
    /// Create a new Grafana dashboard generator
    pub fn new(title: &str) -> Self {
        Self {
            dashboard: Dashboard {
                title: title.to_string(),
                uid: None,
                tags: vec!["torsh".to_string(), "profiling".to_string()],
                timezone: "browser".to_string(),
                editable: true,
                panels: Vec::new(),
                templating: Templating { list: Vec::new() },
                time: TimeRange {
                    from: "now-1h".to_string(),
                    to: "now".to_string(),
                },
                refresh: "10s".to_string(),
                schema_version: 36,
                version: 1,
            },
        }
    }

    /// Set dashboard UID
    pub fn with_uid(mut self, uid: &str) -> Self {
        self.dashboard.uid = Some(uid.to_string());
        self
    }

    /// Add tags to the dashboard
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.dashboard.tags.extend(tags);
        self
    }

    /// Set time range
    pub fn with_time_range(mut self, from: &str, to: &str) -> Self {
        self.dashboard.time.from = from.to_string();
        self.dashboard.time.to = to.to_string();
        self
    }

    /// Set refresh interval
    pub fn with_refresh(mut self, refresh: &str) -> Self {
        self.dashboard.refresh = refresh.to_string();
        self
    }

    /// Add a graph panel
    pub fn add_graph_panel(
        &mut self,
        title: &str,
        query: &str,
        legend: &str,
        grid_pos: GridPos,
    ) -> &mut Self {
        let panel = Panel {
            id: self.dashboard.panels.len() as u32 + 1,
            title: title.to_string(),
            panel_type: "timeseries".to_string(),
            datasource: "Prometheus".to_string(),
            targets: vec![Target {
                expr: query.to_string(),
                legend_format: legend.to_string(),
                ref_id: "A".to_string(),
                interval: None,
            }],
            grid_pos,
            options: None,
            field_config: Some(FieldConfig {
                defaults: FieldDefaults {
                    unit: "short".to_string(),
                    min: None,
                    max: None,
                    thresholds: None,
                },
                overrides: Vec::new(),
            }),
        };
        self.dashboard.panels.push(panel);
        self
    }

    /// Add a heatmap panel
    pub fn add_heatmap_panel(&mut self, title: &str, query: &str, grid_pos: GridPos) -> &mut Self {
        let panel = Panel {
            id: self.dashboard.panels.len() as u32 + 1,
            title: title.to_string(),
            panel_type: "heatmap".to_string(),
            datasource: "Prometheus".to_string(),
            targets: vec![Target {
                expr: query.to_string(),
                legend_format: "".to_string(),
                ref_id: "A".to_string(),
                interval: None,
            }],
            grid_pos,
            options: None,
            field_config: None,
        };
        self.dashboard.panels.push(panel);
        self
    }

    /// Add a gauge panel
    pub fn add_gauge_panel(
        &mut self,
        title: &str,
        query: &str,
        unit: &str,
        min: f64,
        max: f64,
        grid_pos: GridPos,
    ) -> &mut Self {
        let panel = Panel {
            id: self.dashboard.panels.len() as u32 + 1,
            title: title.to_string(),
            panel_type: "gauge".to_string(),
            datasource: "Prometheus".to_string(),
            targets: vec![Target {
                expr: query.to_string(),
                legend_format: "".to_string(),
                ref_id: "A".to_string(),
                interval: None,
            }],
            grid_pos,
            options: None,
            field_config: Some(FieldConfig {
                defaults: FieldDefaults {
                    unit: unit.to_string(),
                    min: Some(min),
                    max: Some(max),
                    thresholds: Some(Thresholds {
                        mode: "absolute".to_string(),
                        steps: vec![
                            ThresholdStep {
                                value: min,
                                color: "green".to_string(),
                            },
                            ThresholdStep {
                                value: (max - min) * 0.7 + min,
                                color: "yellow".to_string(),
                            },
                            ThresholdStep {
                                value: (max - min) * 0.9 + min,
                                color: "red".to_string(),
                            },
                        ],
                    }),
                },
                overrides: Vec::new(),
            }),
        };
        self.dashboard.panels.push(panel);
        self
    }

    /// Add a stat panel
    pub fn add_stat_panel(
        &mut self,
        title: &str,
        query: &str,
        unit: &str,
        grid_pos: GridPos,
    ) -> &mut Self {
        let panel = Panel {
            id: self.dashboard.panels.len() as u32 + 1,
            title: title.to_string(),
            panel_type: "stat".to_string(),
            datasource: "Prometheus".to_string(),
            targets: vec![Target {
                expr: query.to_string(),
                legend_format: "".to_string(),
                ref_id: "A".to_string(),
                interval: None,
            }],
            grid_pos,
            options: None,
            field_config: Some(FieldConfig {
                defaults: FieldDefaults {
                    unit: unit.to_string(),
                    min: None,
                    max: None,
                    thresholds: None,
                },
                overrides: Vec::new(),
            }),
        };
        self.dashboard.panels.push(panel);
        self
    }

    /// Add a variable to the dashboard
    pub fn add_variable(
        &mut self,
        name: &str,
        query: &str,
        multi: bool,
        include_all: bool,
    ) -> &mut Self {
        let variable = Variable {
            name: name.to_string(),
            var_type: "query".to_string(),
            datasource: "Prometheus".to_string(),
            query: query.to_string(),
            multi,
            include_all,
        };
        self.dashboard.templating.list.push(variable);
        self
    }

    /// Get a reference to the dashboard
    pub fn dashboard(&self) -> &Dashboard {
        &self.dashboard
    }

    /// Build the dashboard
    pub fn build(self) -> Dashboard {
        self.dashboard
    }

    /// Export dashboard as JSON
    pub fn export_json(&self) -> TorshResult<String> {
        serde_json::to_string_pretty(&self.dashboard).map_err(|e| {
            TorshError::operation_error(&format!("Failed to serialize dashboard: {}", e))
        })
    }

    /// Export dashboard to file
    pub fn export_to_file(&self, path: &str) -> TorshResult<()> {
        let json = self.export_json()?;
        std::fs::write(path, json).map_err(|e| {
            TorshError::operation_error(&format!("Failed to write dashboard file: {}", e))
        })
    }
}

/// Pre-built dashboard templates
pub struct DashboardTemplates;

impl DashboardTemplates {
    /// Create a comprehensive profiling dashboard
    pub fn create_profiling_dashboard() -> GrafanaDashboardGenerator {
        let mut dashboard = GrafanaDashboardGenerator::new("ToRSh Profiling Overview")
            .with_uid("torsh-profiling-overview")
            .with_tags(vec!["performance".to_string(), "monitoring".to_string()])
            .with_time_range("now-15m", "now")
            .with_refresh("5s");

        // Add operation duration graph
        dashboard.add_graph_panel(
            "Operation Duration (P95)",
            r#"histogram_quantile(0.95, sum(rate(torsh_operation_duration_microseconds_bucket[5m])) by (le, operation))"#,
            "{{operation}}",
            GridPos { h: 8, w: 12, x: 0, y: 0 },
        );

        // Add operation rate graph
        dashboard.add_graph_panel(
            "Operation Rate",
            "rate(torsh_operation_total[5m])",
            "{{operation}}",
            GridPos {
                h: 8,
                w: 12,
                x: 12,
                y: 0,
            },
        );

        // Add memory allocation gauge
        dashboard.add_gauge_panel(
            "Memory Allocated",
            "sum(torsh_memory_allocated_bytes) by (operation)",
            "bytes",
            0.0,
            1e9, // 1GB
            GridPos {
                h: 6,
                w: 6,
                x: 0,
                y: 8,
            },
        );

        // Add FLOPS gauge
        dashboard.add_gauge_panel(
            "FLOPS",
            "rate(torsh_flops_total[1m])",
            "ops",
            0.0,
            1e9, // 1 GFLOPS
            GridPos {
                h: 6,
                w: 6,
                x: 6,
                y: 8,
            },
        );

        // Add thread activity stat
        dashboard.add_stat_panel(
            "Active Threads",
            "count(torsh_thread_activity)",
            "short",
            GridPos {
                h: 6,
                w: 6,
                x: 12,
                y: 8,
            },
        );

        // Add profiling overhead stat
        dashboard.add_stat_panel(
            "Profiling Overhead (Avg)",
            "avg(torsh_profiling_overhead_microseconds)",
            "µs",
            GridPos {
                h: 6,
                w: 6,
                x: 18,
                y: 8,
            },
        );

        // Add operation duration heatmap
        dashboard.add_heatmap_panel(
            "Operation Duration Heatmap",
            "sum(rate(torsh_operation_duration_microseconds_bucket[5m])) by (le)",
            GridPos {
                h: 8,
                w: 24,
                x: 0,
                y: 14,
            },
        );

        // Add operation variable
        dashboard.add_variable(
            "operation",
            "label_values(torsh_operation_total, operation)",
            true,
            true,
        );

        dashboard
    }

    /// Create a memory profiling dashboard
    pub fn create_memory_dashboard() -> GrafanaDashboardGenerator {
        let mut dashboard = GrafanaDashboardGenerator::new("ToRSh Memory Profiling")
            .with_uid("torsh-memory-profiling")
            .with_tags(vec!["memory".to_string(), "profiling".to_string()])
            .with_time_range("now-30m", "now")
            .with_refresh("10s");

        // Memory allocated over time
        dashboard.add_graph_panel(
            "Memory Allocated Over Time",
            "torsh_memory_allocated_bytes",
            "{{operation}}",
            GridPos {
                h: 8,
                w: 12,
                x: 0,
                y: 0,
            },
        );

        // Memory deallocated over time
        dashboard.add_graph_panel(
            "Memory Deallocated Over Time",
            "torsh_memory_deallocated_bytes",
            "{{operation}}",
            GridPos {
                h: 8,
                w: 12,
                x: 12,
                y: 0,
            },
        );

        // Net memory usage
        dashboard.add_graph_panel(
            "Net Memory Usage",
            "torsh_memory_allocated_bytes - torsh_memory_deallocated_bytes",
            "{{operation}}",
            GridPos {
                h: 8,
                w: 24,
                x: 0,
                y: 8,
            },
        );

        // Memory allocation rate
        dashboard.add_stat_panel(
            "Allocation Rate",
            "rate(torsh_memory_allocated_bytes[5m])",
            "Bps",
            GridPos {
                h: 6,
                w: 12,
                x: 0,
                y: 16,
            },
        );

        // Memory deallocation rate
        dashboard.add_stat_panel(
            "Deallocation Rate",
            "rate(torsh_memory_deallocated_bytes[5m])",
            "Bps",
            GridPos {
                h: 6,
                w: 12,
                x: 12,
                y: 16,
            },
        );

        dashboard
    }

    /// Create a performance metrics dashboard
    pub fn create_performance_dashboard() -> GrafanaDashboardGenerator {
        let mut dashboard = GrafanaDashboardGenerator::new("ToRSh Performance Metrics")
            .with_uid("torsh-performance-metrics")
            .with_tags(vec!["performance".to_string(), "metrics".to_string()])
            .with_time_range("now-1h", "now")
            .with_refresh("5s");

        // FLOPS over time
        dashboard.add_graph_panel(
            "FLOPS Over Time",
            "rate(torsh_flops_total[1m])",
            "{{operation}}",
            GridPos {
                h: 8,
                w: 12,
                x: 0,
                y: 0,
            },
        );

        // Bytes transferred over time
        dashboard.add_graph_panel(
            "Bytes Transferred",
            "rate(torsh_bytes_transferred_total[1m])",
            "{{operation}} - {{direction}}",
            GridPos {
                h: 8,
                w: 12,
                x: 12,
                y: 0,
            },
        );

        // Operation latency percentiles
        dashboard.add_graph_panel(
            "Operation Latency Percentiles",
            r#"histogram_quantile(0.50, sum(rate(torsh_operation_duration_microseconds_bucket[5m])) by (le, operation))"#,
            "P50",
            GridPos { h: 8, w: 24, x: 0, y: 8 },
        );

        // Throughput gauge
        dashboard.add_gauge_panel(
            "Throughput",
            "sum(rate(torsh_operation_total[1m]))",
            "ops",
            0.0,
            1000.0,
            GridPos {
                h: 6,
                w: 8,
                x: 0,
                y: 16,
            },
        );

        // Avg operation duration
        dashboard.add_gauge_panel(
            "Avg Operation Duration",
            "avg(torsh_operation_duration_microseconds)",
            "µs",
            0.0,
            10000.0,
            GridPos {
                h: 6,
                w: 8,
                x: 8,
                y: 16,
            },
        );

        // Max operation duration
        dashboard.add_gauge_panel(
            "Max Operation Duration",
            "max(torsh_operation_duration_microseconds)",
            "µs",
            0.0,
            100000.0,
            GridPos {
                h: 6,
                w: 8,
                x: 16,
                y: 16,
            },
        );

        dashboard
    }
}

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

    #[test]
    fn test_dashboard_creation() {
        let dashboard = GrafanaDashboardGenerator::new("Test Dashboard");
        assert_eq!(dashboard.dashboard.title, "Test Dashboard");
        assert!(dashboard.dashboard.panels.is_empty());
    }

    #[test]
    fn test_add_graph_panel() {
        let mut dashboard = GrafanaDashboardGenerator::new("Test");
        dashboard.add_graph_panel(
            "Test Graph",
            "up",
            "{{job}}",
            GridPos {
                h: 8,
                w: 12,
                x: 0,
                y: 0,
            },
        );
        assert_eq!(dashboard.dashboard.panels.len(), 1);
        assert_eq!(dashboard.dashboard.panels[0].title, "Test Graph");
    }

    #[test]
    fn test_export_json() {
        let dashboard = GrafanaDashboardGenerator::new("Test");
        let json = dashboard.export_json();
        assert!(json.is_ok());
        assert!(json.unwrap().contains("Test"));
    }

    #[test]
    fn test_profiling_dashboard_template() {
        let dashboard = DashboardTemplates::create_profiling_dashboard();
        assert!(dashboard.dashboard.panels.len() > 0);
        assert_eq!(
            dashboard.dashboard.uid,
            Some("torsh-profiling-overview".to_string())
        );
    }

    #[test]
    fn test_memory_dashboard_template() {
        let dashboard = DashboardTemplates::create_memory_dashboard();
        assert!(dashboard.dashboard.panels.len() > 0);
        assert!(dashboard.dashboard.title.contains("Memory"));
    }

    #[test]
    fn test_performance_dashboard_template() {
        let dashboard = DashboardTemplates::create_performance_dashboard();
        assert!(dashboard.dashboard.panels.len() > 0);
        assert!(dashboard.dashboard.title.contains("Performance"));
    }
}