kotoba-profiler 0.1.16

Advanced profiling and performance analysis tools for KotobaDB
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
//! KotobaDB Performance Profiler CLI
//!
//! Command-line interface for running comprehensive performance profiling on KotobaDB.

use clap::{Parser, Subcommand};
use kotoba_profiler::{Profiler, ProfilingConfig};
use std::path::PathBuf;
use std::time::Duration;

#[derive(Parser)]
#[command(name = "kotoba-profiler")]
#[command(about = "Comprehensive performance profiling tool for KotobaDB")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Run comprehensive profiling session
    Profile {
        /// Profiling duration in seconds
        #[arg(short, long, default_value = "60")]
        duration: u64,

        /// Database path to profile
        #[arg(short, long, default_value = "/tmp/kotoba_profile.db")]
        db_path: PathBuf,

        /// Output directory for profiling reports
        #[arg(short, long, default_value = "profiling_reports")]
        output_dir: String,

        /// Enable CPU profiling
        #[arg(long, default_value = "true")]
        cpu: bool,

        /// Enable memory profiling
        #[arg(long, default_value = "true")]
        memory: bool,

        /// Enable I/O profiling
        #[arg(long, default_value = "true")]
        io: bool,

        /// Enable query profiling
        #[arg(long, default_value = "true")]
        query: bool,

        /// Sampling interval in milliseconds
        #[arg(long, default_value = "100")]
        sampling_interval_ms: u64,
    },

    /// Run CPU profiling only
    CpuProfile {
        /// Profiling duration in seconds
        #[arg(short, long, default_value = "30")]
        duration: u64,

        /// Output file for flame graph
        #[arg(short, long, default_value = "cpu_flame_graph.txt")]
        output: PathBuf,
    },

    /// Run memory profiling only
    MemoryProfile {
        /// Profiling duration in seconds
        #[arg(short, long, default_value = "30")]
        duration: u64,

        /// Output directory for memory reports
        #[arg(short, long, default_value = "memory_reports")]
        output_dir: String,
    },

    /// Analyze existing profiling data
    Analyze {
        /// Input profiling data file
        #[arg(short, long)]
        input: PathBuf,

        /// Output directory for analysis reports
        #[arg(short, long, default_value = "analysis_reports")]
        output_dir: String,
    },

    /// Generate optimization recommendations
    Recommend {
        /// Current system metrics (JSON format)
        #[arg(short, long)]
        metrics: Option<PathBuf>,

        /// Output file for recommendations
        #[arg(short, long, default_value = "optimization_recommendations.txt")]
        output: PathBuf,
    },
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Profile {
            duration,
            db_path,
            output_dir,
            cpu,
            memory,
            io,
            query,
            sampling_interval_ms,
        } => {
            run_comprehensive_profiling(
                duration,
                db_path,
                output_dir,
                cpu,
                memory,
                io,
                query,
                sampling_interval_ms,
            ).await
        }

        Commands::CpuProfile { duration, output } => {
            run_cpu_profiling(duration, output).await
        }

        Commands::MemoryProfile { duration, output_dir } => {
            run_memory_profiling(duration, output_dir).await
        }

        Commands::Analyze { input, output_dir } => {
            run_analysis(input, output_dir).await
        }

        Commands::Recommend { metrics, output } => {
            generate_recommendations(metrics, output).await
        }
    }
}

async fn run_comprehensive_profiling(
    duration: u64,
    db_path: PathBuf,
    output_dir: String,
    enable_cpu: bool,
    enable_memory: bool,
    enable_io: bool,
    enable_query: bool,
    sampling_interval_ms: u64,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("🚀 Starting KotobaDB Comprehensive Profiling");
    println!("==========================================");
    println!("Duration: {}s", duration);
    println!("Database: {}", db_path.display());
    println!("Output: {}", output_dir);
    println!("CPU Profiling: {}", if enable_cpu { "Enabled" } else { "Disabled" });
    println!("Memory Profiling: {}", if enable_memory { "Enabled" } else { "Disabled" });
    println!("I/O Profiling: {}", if enable_io { "Enabled" } else { "Disabled" });
    println!("Query Profiling: {}", if enable_query { "Enabled" } else { "Disabled" });
    println!("Sampling Interval: {}ms", sampling_interval_ms);
    println!();

    // Create profiling configuration
    let config = ProfilingConfig {
        enable_cpu_profiling: enable_cpu,
        enable_memory_profiling: enable_memory,
        enable_io_profiling: enable_io,
        enable_query_profiling: enable_query,
        sampling_interval: Duration::from_millis(sampling_interval_ms),
        max_snapshots: 10000,
        flame_graph_output: true,
    };

    // Initialize profiler
    let mut profiler = Profiler::with_config(config);

    // Setup output directory
    std::fs::create_dir_all(&output_dir)?;

    // Start profiling
    profiler.start_profiling().await?;

    // Simulate database workload during profiling
    println!("Running database workload simulation...");
    run_workload_simulation(duration).await;

    // Stop profiling and generate report
    let report = profiler.stop_profiling().await?;

    // Save profiling report
    let report_path = std::path::Path::new(&output_dir).join("profiling_report.json");
    std::fs::write(&report_path, report.to_json()?)?;
    println!("📄 Profiling report saved to: {}", report_path.display());

    // Generate summary
    let summary_path = std::path::Path::new(&output_dir).join("PROFILING_SUMMARY.txt");
    std::fs::write(&summary_path, report.summary())?;
    println!("📋 Summary saved to: {}", summary_path.display());

    // Generate flame graph if CPU profiling was enabled
    if let Some(flame_graph) = report.to_flame_graph() {
        let flame_path = std::path::Path::new(&output_dir).join("cpu_flame_graph.txt");
        std::fs::write(&flame_path, flame_graph)?;
        println!("🔥 Flame graph saved to: {}", flame_path.display());
    }

    println!("\n✅ Profiling completed successfully!");
    Ok(())
}

async fn run_cpu_profiling(duration: u64, output: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
    println!("🔥 Starting CPU Profiling");
    println!("========================");
    println!("Duration: {}s", duration);
    println!("Output: {}", output.display());
    println!();

    let mut profiler = Profiler::with_config(ProfilingConfig {
        enable_cpu_profiling: true,
        enable_memory_profiling: false,
        enable_io_profiling: false,
        enable_query_profiling: false,
        sampling_interval: Duration::from_millis(10),
        max_snapshots: 10000,
        flame_graph_output: true,
    });

    profiler.start_profiling().await?;
    run_workload_simulation(duration).await;
    let report = profiler.stop_profiling().await?;

    if let Some(flame_graph) = report.to_flame_graph() {
        std::fs::write(&output, flame_graph)?;
        println!("✅ Flame graph saved to: {}", output.display());
    } else {
        println!("❌ CPU profiling data not available");
    }

    Ok(())
}

async fn run_memory_profiling(duration: u64, output_dir: String) -> Result<(), Box<dyn std::error::Error>> {
    println!("💾 Starting Memory Profiling");
    println!("===========================");
    println!("Duration: {}s", duration);
    println!("Output: {}", output_dir);
    println!();

    std::fs::create_dir_all(&output_dir)?;

    let mut profiler = Profiler::with_config(ProfilingConfig {
        enable_cpu_profiling: false,
        enable_memory_profiling: true,
        enable_io_profiling: false,
        enable_query_profiling: false,
        sampling_interval: Duration::from_millis(50),
        max_snapshots: 10000,
        flame_graph_output: false,
    });

    profiler.start_profiling().await?;
    run_memory_intensive_workload(duration).await;
    let report = profiler.stop_profiling().await?;

    let report_path = std::path::Path::new(&output_dir).join("memory_profile.json");
    std::fs::write(&report_path, report.to_json()?)?;
    println!("✅ Memory profile saved to: {}", report_path.display());

    Ok(())
}

async fn run_analysis(input: PathBuf, output_dir: String) -> Result<(), Box<dyn std::error::Error>> {
    println!("🔍 Analyzing Profiling Data");
    println!("==========================");
    println!("Input: {}", input.display());
    println!("Output: {}", output_dir);
    println!();

    // Load profiling data
    let data = std::fs::read_to_string(&input)?;
    let report: crate::ProfilingReport = serde_json::from_str(&data)?;

    std::fs::create_dir_all(&output_dir)?;

    // Generate analysis report
    let analysis_path = std::path::Path::new(&output_dir).join("analysis_report.json");
    std::fs::write(&analysis_path, serde_json::to_string_pretty(&report)?)?;
    println!("✅ Analysis report saved to: {}", analysis_path.display());

    // Generate summary
    let summary_path = std::path::Path::new(&output_dir).join("ANALYSIS_SUMMARY.txt");
    std::fs::write(&summary_path, report.summary())?;
    println!("📋 Analysis summary saved to: {}", summary_path.display());

    Ok(())
}

async fn generate_recommendations(metrics: Option<PathBuf>, output: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
    println!("💡 Generating Optimization Recommendations");
    println!("=========================================");

    let system_analysis = if let Some(metrics_path) = metrics {
        let data = std::fs::read_to_string(&metrics_path)?;
        Some(serde_json::from_str(&data)?)
    } else {
        // Generate mock system analysis for demonstration
        Some(crate::system_monitor::SystemAnalysis {
            monitoring_duration: Duration::from_secs(300),
            average_cpu_usage: 65.0,
            peak_cpu_usage: 85.0,
            average_memory_usage: 72.0,
            peak_memory_usage: 88.0,
            total_disk_read_mb: 1024.0,
            total_disk_write_mb: 512.0,
            total_network_rx_mb: 256.0,
            total_network_tx_mb: 128.0,
            resource_trends: crate::system_monitor::ResourceTrends {
                cpu_trend: crate::system_monitor::Trend::Increasing,
                memory_trend: crate::system_monitor::Trend::Stable,
                disk_trend: crate::system_monitor::Trend::Stable,
                network_trend: crate::system_monitor::Trend::Stable,
            },
            bottlenecks: vec![
                crate::system_monitor::SystemBottleneck {
                    resource_type: crate::system_monitor::ResourceType::Cpu,
                    severity: crate::system_monitor::Severity::Medium,
                    description: "CPU usage trending upward".to_string(),
                    utilization_percent: 75.0,
                    duration_seconds: 120.0,
                }
            ],
            utilization_patterns: crate::system_monitor::UtilizationPatterns {
                peak_hours: vec![9, 10, 11, 14, 15, 16],
                cpu_spike_frequency: 0.15,
                memory_growth_rate: 2.5,
                io_burst_pattern: true,
                network_burst_pattern: false,
            },
            recommendations: vec!["Monitor CPU usage trends".to_string()],
        })
    };

    let advisor = crate::performance_advisor::PerformanceAdvisor::new();
    let bottlenecks = advisor.identify_bottlenecks(
        &None, // CPU analysis
        &None, // Memory analysis
        &None, // I/O analysis
        &None, // Query analysis
        &system_analysis,
    ).await;

    let recommendations = advisor.generate_recommendations(&bottlenecks, &system_analysis).await;

    // Generate recommendations report
    let mut content = format!("KotobaDB Optimization Recommendations\n");
    content.push_str(&format!("Generated: {}\n\n", chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")));

    if !bottlenecks.is_empty() {
        content.push_str("Identified Bottlenecks:\n");
        content.push_str("=====================\n");
        for bottleneck in &bottlenecks {
            content.push_str(&format!("{} ({}): {}\n",
                bottleneck.bottleneck_type, bottleneck.severity, bottleneck.description));
        }
        content.push_str("\n");
    }

    if !recommendations.is_empty() {
        content.push_str("Optimization Recommendations:\n");
        content.push_str("=============================\n");
        for rec in &recommendations {
            content.push_str(&format!("Priority: {:?} | Category: {:?} | Effort: {:?}\n",
                rec.priority, rec.category, rec.implementation_effort));
            content.push_str(&format!("Title: {}\n", rec.title));
            content.push_str(&format!("Description: {}\n", rec.description));
            content.push_str(&format!("Expected Impact: {:.1}%\n", rec.expected_impact * 100.0));
            content.push_str("Actions:\n");
            for action in &rec.actions {
                content.push_str(&format!("  - {}\n", action));
            }
            content.push_str("\n");
        }
    } else {
        content.push_str("No specific recommendations. System appears to be performing well.\n");
    }

    std::fs::write(&output, content)?;
    println!("✅ Recommendations saved to: {}", output.display());

    Ok(())
}

/// Simulate database workload during profiling
async fn run_workload_simulation(duration_secs: u64) {
    use rand::Rng;

    let start_time = std::time::Instant::now();
    let mut rng = rand::thread_rng();

    println!("Running workload simulation for {} seconds...", duration_secs);

    while start_time.elapsed().as_secs() < duration_secs {
        // Simulate various database operations
        let operation_type = rng.gen_range(0..10);

        match operation_type {
            0..=3 => {
                // Simulate CPU-intensive operation (sorting, computation)
                let mut data: Vec<i32> = (0..1000).map(|_| rng.gen()).collect();
                data.sort();
                tokio::time::sleep(Duration::from_millis(rng.gen_range(1..5))).await;
            }
            4..=6 => {
                // Simulate memory-intensive operation
                let mut allocations = Vec::new();
                for _ in 0..100 {
                    allocations.push(vec![0u8; rng.gen_range(1024..10240)]);
                }
                drop(allocations);
                tokio::time::sleep(Duration::from_millis(rng.gen_range(1..3))).await;
            }
            7..=8 => {
                // Simulate I/O-intensive operation
                tokio::time::sleep(Duration::from_millis(rng.gen_range(5..20))).await;
            }
            _ => {
                // Simulate query operation
                tokio::time::sleep(Duration::from_millis(rng.gen_range(2..10))).await;
            }
        }
    }

    println!("Workload simulation completed");
}

/// Run memory-intensive workload for memory profiling
async fn run_memory_intensive_workload(duration_secs: u64) {
    use rand::Rng;

    let start_time = std::time::Instant::now();
    let mut rng = rand::thread_rng();

    println!("Running memory-intensive workload simulation...");

    let mut allocations = Vec::new();

    while start_time.elapsed().as_secs() < duration_secs {
        // Allocate memory in various patterns
        match rng.gen_range(0..5) {
            0 => {
                // Large allocation
                allocations.push(vec![0u8; 1024 * 1024]); // 1MB
            }
            1 => {
                // Many small allocations
                for _ in 0..1000 {
                    allocations.push(vec![0u8; rng.gen_range(64..1024)]);
                }
            }
            2 => {
                // String allocations
                for _ in 0..100 {
                    allocations.push(format!("Memory test string {}", rng.gen::<u64>()).into_bytes());
                }
            }
            3 => {
                // Free some allocations
                if allocations.len() > 100 {
                    allocations.drain(0..50);
                }
            }
            _ => {
                // Complex data structures
                let mut map = std::collections::HashMap::new();
                for i in 0..100 {
                    map.insert(format!("key_{}", i), vec![rng.gen::<u8>(); 100]);
                }
                allocations.push(serde_json::to_vec(&map).unwrap_or_default());
            }
        }

        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    // Clean up
    drop(allocations);
    println!("Memory-intensive workload simulation completed");
}