voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
//! Comprehensive performance profiling example for VoiRS SDK.
//!
//! This example demonstrates:
//! - Basic profiling session management
//! - Stage-by-stage timing analysis
//! - Memory profiling and leak detection
//! - Bottleneck detection and recommendations
//! - Performance comparison between sessions
//! - Regression detection
//! - Report generation in multiple formats
//! - Real-time performance monitoring
//!
//! ## Usage
//!
//! ```bash
//! cargo run --example performance_profiling --features emotion,cloning
//! ```

use std::sync::Arc;
use std::time::Duration;
use voirs_sdk::prelude::*;
use voirs_sdk::profiling::{
    PerformanceComparator, Profiler, ProfilerConfig, RegressionDetector, ReportGenerator,
};

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing for logging
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .init();

    println!("=== VoiRS SDK - Performance Profiling Example ===\n");

    // Create pipeline
    println!("Creating VoiRS pipeline...");
    let pipeline = Arc::new(
        VoirsPipelineBuilder::new()
            .with_test_mode(true)
            .with_quality(QualityLevel::High)
            .build()
            .await?,
    );
    println!("Pipeline created successfully!\n");

    // Example 1: Basic profiling session
    println!("Example 1: Basic Profiling Session");
    println!("{}", "-".repeat(60));
    basic_profiling_session(&pipeline).await?;
    println!();

    // Example 2: Stage-by-stage analysis
    println!("Example 2: Stage-by-Stage Performance Analysis");
    println!("{}", "-".repeat(60));
    stage_by_stage_analysis(&pipeline).await?;
    println!();

    // Example 3: Memory profiling
    println!("Example 3: Memory Profiling and Leak Detection");
    println!("{}", "-".repeat(60));
    memory_profiling(&pipeline).await?;
    println!();

    // Example 4: Bottleneck detection
    println!("Example 4: Automatic Bottleneck Detection");
    println!("{}", "-".repeat(60));
    bottleneck_detection(&pipeline).await?;
    println!();

    // Example 5: Performance comparison
    println!("Example 5: Performance Comparison Between Sessions");
    println!("{}", "-".repeat(60));
    performance_comparison(&pipeline).await?;
    println!();

    // Example 6: Regression detection
    println!("Example 6: Regression Detection");
    println!("{}", "-".repeat(60));
    regression_detection(&pipeline).await?;
    println!();

    // Example 7: Report generation
    println!("Example 7: Multi-Format Report Generation");
    println!("{}", "-".repeat(60));
    report_generation(&pipeline).await?;
    println!();

    // Example 8: Real-time monitoring
    println!("Example 8: Real-Time Performance Monitoring");
    println!("{}", "-".repeat(60));
    realtime_monitoring(&pipeline).await?;
    println!();

    println!("All profiling examples completed successfully!");

    Ok(())
}

/// Example 1: Basic profiling session
async fn basic_profiling_session(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Starting a basic profiling session...\n");

    // Create profiler with default configuration
    let profiler = Profiler::new(ProfilerConfig::default());

    // Start profiling session
    let session = profiler.start_session("basic_synthesis").await;
    println!("Session started: {}", session.name);

    // Perform some synthesis operations
    for i in 1..=5 {
        let text = format!("This is synthesis test number {}", i);
        let _audio = pipeline.synthesize(&text).await?;
        println!("  Completed synthesis {}/5", i);
    }

    // End session and get report
    let report = profiler.end_session(session).await?;

    println!("\nSession Summary:");
    println!(
        "  Duration: {:.2}ms",
        report.session.duration_seconds * 1000.0
    );
    println!("  Stages profiled: {}", report.stage_breakdown.len());
    println!(
        "  Memory snapshots: {}",
        report.memory_analysis.is_some() as usize
    );
    println!("  Bottlenecks detected: {}", report.bottlenecks.len());

    // Display stage metrics
    println!("\nStage Performance:");
    for stage in &report.stage_breakdown {
        println!(
            "  {}: {:.2}ms avg ({} executions)",
            stage.stage_name, stage.avg_duration_ms, stage.execution_count
        );
    }

    Ok(())
}

/// Example 2: Stage-by-stage performance analysis
async fn stage_by_stage_analysis(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Analyzing performance of each pipeline stage...\n");

    let config = ProfilerConfig {
        enable_timing: true,
        enable_memory: false, // Focus on timing only
        ..Default::default()
    };

    let profiler = Profiler::new(config);
    let session = profiler.start_session("stage_analysis").await;

    // Perform synthesis
    let text = "This is a detailed performance analysis of the synthesis pipeline stages.";
    let _audio = pipeline.synthesize(text).await?;

    let report = profiler.end_session(session).await?;

    println!("Detailed Stage Analysis:\n");

    // Sort stages by average duration
    let mut stages: Vec<_> = report.stage_breakdown.iter().collect();
    stages.sort_by(|a, b| {
        b.avg_duration_ms
            .partial_cmp(&a.avg_duration_ms)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    for stage in stages {
        println!("Stage: {}", stage.stage_name);
        println!("  Executions: {}", stage.execution_count);
        println!("  Average duration: {:.2}ms", stage.avg_duration_ms);
        println!("  Min duration: {:.2}ms", stage.min_duration_ms);
        println!("  Max duration: {:.2}ms", stage.max_duration_ms);
        println!("  Total duration: {:.2}ms", stage.total_duration_ms);
        println!("  Percentage of total: {:.1}%", stage.percentage_of_total);
        println!();
    }

    Ok(())
}

/// Example 3: Memory profiling and leak detection
async fn memory_profiling(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Profiling memory usage and checking for leaks...\n");

    let config = ProfilerConfig {
        enable_timing: false, // Focus on memory only
        enable_memory: true,
        ..Default::default()
    };

    let profiler = Profiler::new(config);
    let session = profiler.start_session("memory_profiling").await;

    // Perform multiple synthesis operations to observe memory patterns
    for i in 1..=10 {
        let text = format!("Memory profiling test iteration {}", i);
        let _audio = pipeline.synthesize(&text).await?;
    }

    let report = profiler.end_session(session).await?;

    println!("Memory Profiling Results:\n");

    if let Some(memory) = &report.memory_analysis {
        println!("Peak memory: {:.2} MB", memory.peak_mb);
        println!("Average memory: {:.2} MB", memory.average_mb);
        println!("Memory growth: {:+.1}%", memory.growth_percent);
        println!();

        // Check for potential memory leaks
        if memory.growth_percent > 50.0 {
            println!("WARNING: Significant memory growth detected!");
            println!("   This may indicate a memory leak.");
        } else {
            println!("Memory usage appears stable.");
        }
    } else {
        println!("No memory analysis data available.");
    }

    Ok(())
}

/// Example 4: Automatic bottleneck detection
async fn bottleneck_detection(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Detecting performance bottlenecks...\n");

    let config = ProfilerConfig {
        enable_timing: true,
        enable_memory: true,
        enable_bottleneck_detection: true,
        ..Default::default()
    };

    let profiler = Profiler::new(config);
    let session = profiler.start_session("bottleneck_detection").await;

    // Perform synthesis
    let text = "Testing bottleneck detection in the synthesis pipeline.";
    let _audio = pipeline.synthesize(text).await?;

    let report = profiler.end_session(session).await?;

    println!("Bottleneck Detection Results:\n");

    if report.bottlenecks.is_empty() {
        println!("No significant bottlenecks detected!");
    } else {
        println!(
            "Found {} potential bottleneck(s):\n",
            report.bottlenecks.len()
        );

        for (idx, bottleneck) in report.bottlenecks.iter().enumerate() {
            println!("Bottleneck #{}", idx + 1);
            println!("  Component: {}", bottleneck.component);
            println!("  Severity: {:?}", bottleneck.severity);
            println!("  Impact: {}", bottleneck.impact_description);
            if !bottleneck.recommendation.is_empty() {
                println!("  Recommendation: {}", bottleneck.recommendation);
            }
            println!();
        }
    }

    Ok(())
}

/// Example 5: Performance comparison between sessions
async fn performance_comparison(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Comparing performance between two sessions...\n");

    let profiler = Profiler::new(ProfilerConfig::default());

    // Session 1: Baseline
    println!("Running baseline session...");
    let session1 = profiler.start_session("baseline").await;
    let _audio1 = pipeline.synthesize("Baseline performance test.").await?;
    let report1 = profiler.end_session(session1).await?;
    println!(
        "Baseline duration: {:.2}ms\n",
        report1.session.duration_seconds * 1000.0
    );

    // Session 2: Comparison
    println!("Running comparison session...");
    let session2 = profiler.start_session("comparison").await;
    let _audio2 = pipeline.synthesize("Comparison performance test.").await?;
    let report2 = profiler.end_session(session2).await?;
    println!(
        "Comparison duration: {:.2}ms\n",
        report2.session.duration_seconds * 1000.0
    );

    // Compare sessions (need ProfileSession, not PerformanceReport)
    let all_sessions = profiler.get_sessions().await;
    if all_sessions.len() >= 2 {
        let comparator = PerformanceComparator::new();
        let comparison = comparator.compare(&all_sessions[0], &all_sessions[1]).await;

        println!("Performance Comparison:\n");
        println!("Overall change: {:.1}%", comparison.overall_change_percent);

        println!("\nStage-by-Stage Comparison:");
        for (stage, stage_cmp) in &comparison.stage_comparisons {
            let symbol = if stage_cmp.change_percent > 0.0 {
                "up"
            } else if stage_cmp.change_percent < 0.0 {
                "down"
            } else {
                "equal"
            };
            println!(
                "  {}: {} {:.1}%",
                stage,
                symbol,
                stage_cmp.change_percent.abs()
            );
        }

        if let Some(memory_cmp) = &comparison.memory_comparison {
            println!("\nMemory change: {:.1}%", memory_cmp.change_percent);
        }

        // Check for regressions
        if comparison.overall_change_percent > 10.0 {
            println!("\nPerformance regression detected!");
        } else if comparison.overall_change_percent < -10.0 {
            println!("\nPerformance improvement detected!");
        } else {
            println!("\nPerformance is stable.");
        }
    }

    Ok(())
}

/// Example 6: Regression detection across multiple sessions
async fn regression_detection(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Running regression detection across multiple sessions...\n");

    let profiler = Arc::new(Profiler::new(ProfilerConfig {
        max_history_size: 10,
        enable_baseline_comparison: true,
        regression_threshold_percent: 15.0,
        ..Default::default()
    }));

    // Run multiple sessions
    println!("Running 5 test sessions...");
    for i in 1..=5 {
        let session = profiler.start_session(&format!("session_{}", i)).await;
        let text = format!("Regression test iteration {}", i);
        let _audio = pipeline.synthesize(&text).await?;
        let report = profiler.end_session(session).await?;
        println!(
            "  Session {}: {:.2}ms",
            i,
            report.session.duration_seconds * 1000.0
        );
    }
    println!();

    // Detect regressions
    let detector = RegressionDetector::new(15.0, 5); // 15% threshold, 5 history
    let history = profiler.get_sessions().await;

    println!("Regression Detection Results:\n");

    if history.len() < 2 {
        println!("Not enough sessions for regression detection.");
    } else {
        let regressions = detector.detect_regressions(&history);

        if regressions.is_empty() {
            println!("No regressions detected!");
        } else {
            println!("Found {} regression(s):\n", regressions.len());

            for (idx, regression) in regressions.iter().enumerate() {
                println!("Regression #{}", idx + 1);
                println!("  Description: {}", regression.description);
                println!("  Severity: {}", regression.severity);
                println!();
            }
        }
    }

    Ok(())
}

/// Example 7: Multi-format report generation
async fn report_generation(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Generating performance reports in multiple formats...\n");

    let profiler = Profiler::new(ProfilerConfig::default());
    let session = profiler.start_session("report_generation").await;

    // Perform synthesis
    let text = "Generating comprehensive performance reports.";
    let _audio = pipeline.synthesize(text).await?;

    let prof_session = session.clone();
    let report = profiler.end_session(session).await?;

    // Display the report summary
    println!("=== Performance Report ===");
    println!("{}", report.summary());
    println!();

    // Use report generator for detailed output
    let generator = ReportGenerator::new(ProfilerConfig::default());
    let detailed_report = generator.generate(&prof_session, None).await?;

    println!(
        "=== Detailed Report (JSON preview) ===\n{}\n",
        serde_json::to_string_pretty(&detailed_report)
            .unwrap_or_default()
            .chars()
            .take(300)
            .collect::<String>()
    );

    println!("Reports generated successfully!");

    Ok(())
}

/// Example 8: Real-time performance monitoring
async fn realtime_monitoring(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Monitoring performance in real-time...\n");

    let config = ProfilerConfig {
        sampling_interval_ms: 50, // Sample every 50ms
        enable_timing: true,
        enable_memory: true,
        ..Default::default()
    };

    let profiler = Profiler::new(config);
    let session = profiler.start_session("realtime_monitoring").await;

    println!("Performing synthesis with real-time monitoring...");

    // Simulate multiple operations
    for i in 1..=5 {
        let text = format!("Real-time monitoring test {}", i);
        let _audio = pipeline.synthesize(&text).await?;

        // In a real application, you could query profiler state here
        println!("  Operation {} completed", i);

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

    let report = profiler.end_session(session).await?;

    println!("\nReal-Time Monitoring Summary:");
    println!(
        "  Total duration: {:.2}ms",
        report.session.duration_seconds * 1000.0
    );
    println!(
        "  Memory analysis: {}",
        if report.memory_analysis.is_some() {
            "available"
        } else {
            "not available"
        }
    );
    println!("  Stages tracked: {}", report.stage_breakdown.len());

    // Display memory analysis if available
    if let Some(memory) = &report.memory_analysis {
        println!("\nMemory Usage Summary:");
        println!("  Peak: {:.2} MB", memory.peak_mb);
        println!("  Average: {:.2} MB", memory.average_mb);
    }

    Ok(())
}