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
//! Comprehensive performance profiling and analysis for VoiRS SDK.
//!
//! This module provides detailed performance profiling capabilities including:
//! - Stage-by-stage timing analysis for synthesis pipeline
//! - Memory allocation tracking and profiling
//! - Bottleneck detection and recommendations
//! - Performance comparison and regression detection
//! - Real-time performance monitoring
//! - Historical performance tracking
//!
//! # Examples
//!
//! ```no_run
//! use voirs_sdk::prelude::*;
//! use voirs_sdk::profiling::{Profiler, ProfilerConfig};
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!     let pipeline = VoirsPipelineBuilder::new().build().await?;
//!
//!     // Create profiler
//!     let profiler = Profiler::new(ProfilerConfig::default());
//!
//!     // Start profiling session
//!     let session = profiler.start_session("synthesis_test").await;
//!
//!     // Perform synthesis with profiling
//!     let audio = pipeline.synthesize("Hello, world!").await?;
//!
//!     // End session and get report
//!     let report = profiler.end_session(session).await?;
//!
//!     // Display performance report
//!     println!("{}", report.summary());
//!     println!("Bottlenecks: {:?}", report.bottlenecks());
//!
//!     Ok(())
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

fn instant_now() -> Instant {
    Instant::now()
}

mod bottleneck_detector;
mod comparator;
mod memory_profiler;
mod pipeline_profiler;
mod report_generator;

pub use bottleneck_detector::{Bottleneck, BottleneckDetector, BottleneckSeverity};
pub use comparator::{ComparisonResult, PerformanceComparator, RegressionDetector};
pub use memory_profiler::{AllocationTracker, MemoryProfiler, MemorySnapshot};
pub use pipeline_profiler::{PipelineProfiler, PipelineStage, StageMetrics};
pub use report_generator::{PerformanceReport, ReportFormat, ReportGenerator};

/// Configuration for the performance profiler.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfilerConfig {
    /// Enable detailed timing profiling
    pub enable_timing: bool,

    /// Enable memory profiling
    pub enable_memory: bool,

    /// Enable bottleneck detection
    pub enable_bottleneck_detection: bool,

    /// Maximum number of sessions to keep in history
    pub max_history_size: usize,

    /// Sampling interval for real-time monitoring (milliseconds)
    pub sampling_interval_ms: u64,

    /// Enable automatic report generation
    pub auto_generate_reports: bool,

    /// Report output directory
    pub report_output_dir: Option<std::path::PathBuf>,

    /// Enable comparison with baseline
    pub enable_baseline_comparison: bool,

    /// Threshold for regression detection (percentage)
    pub regression_threshold_percent: f64,
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            enable_timing: true,
            enable_memory: true,
            enable_bottleneck_detection: true,
            max_history_size: 100,
            sampling_interval_ms: 100,
            auto_generate_reports: false,
            report_output_dir: None,
            enable_baseline_comparison: false,
            regression_threshold_percent: 10.0,
        }
    }
}

/// A profiling session tracking performance metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileSession {
    /// Unique session identifier
    pub id: String,

    /// Session name
    pub name: String,

    /// Start time (not serialized)
    #[serde(skip, default = "instant_now")]
    pub start_time: Instant,

    /// End time (if session is complete, not serialized)
    #[serde(skip, default)]
    pub end_time: Option<Instant>,

    /// Total duration in milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<Duration>,

    /// Stage metrics
    pub stage_metrics: HashMap<String, StageMetrics>,

    /// Memory snapshots
    pub memory_snapshots: Vec<MemorySnapshot>,

    /// Detected bottlenecks
    pub bottlenecks: Vec<Bottleneck>,

    /// Custom metadata
    pub metadata: HashMap<String, String>,
}

impl Default for ProfileSession {
    fn default() -> Self {
        Self::new("")
    }
}

impl ProfileSession {
    /// Create a new profiling session.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            name: name.into(),
            start_time: Instant::now(),
            end_time: None,
            duration: None,
            stage_metrics: HashMap::new(),
            memory_snapshots: Vec::new(),
            bottlenecks: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Mark session as complete.
    pub fn complete(&mut self) {
        let end_time = Instant::now();
        self.duration = Some(end_time.duration_since(self.start_time));
        self.end_time = Some(end_time);
    }

    /// Check if session is complete.
    pub fn is_complete(&self) -> bool {
        self.end_time.is_some()
    }

    /// Get session duration.
    pub fn duration(&self) -> Option<Duration> {
        self.duration
    }

    /// Add stage metrics.
    pub fn add_stage_metrics(&mut self, stage: String, metrics: StageMetrics) {
        self.stage_metrics.insert(stage, metrics);
    }

    /// Add memory snapshot.
    pub fn add_memory_snapshot(&mut self, snapshot: MemorySnapshot) {
        self.memory_snapshots.push(snapshot);
    }

    /// Add detected bottleneck.
    pub fn add_bottleneck(&mut self, bottleneck: Bottleneck) {
        self.bottlenecks.push(bottleneck);
    }

    /// Add custom metadata.
    pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.metadata.insert(key.into(), value.into());
    }
}

/// Main performance profiler for VoiRS SDK.
pub struct Profiler {
    config: ProfilerConfig,
    sessions: Arc<RwLock<VecDeque<ProfileSession>>>,
    pipeline_profiler: Arc<PipelineProfiler>,
    memory_profiler: Arc<MemoryProfiler>,
    bottleneck_detector: Arc<BottleneckDetector>,
    report_generator: Arc<ReportGenerator>,
    comparator: Arc<PerformanceComparator>,
    baseline_session: Arc<RwLock<Option<ProfileSession>>>,
}

impl Profiler {
    /// Create a new profiler with the given configuration.
    pub fn new(config: ProfilerConfig) -> Self {
        Self {
            config: config.clone(),
            sessions: Arc::new(RwLock::new(VecDeque::new())),
            pipeline_profiler: Arc::new(PipelineProfiler::new()),
            memory_profiler: Arc::new(MemoryProfiler::new()),
            bottleneck_detector: Arc::new(BottleneckDetector::new(
                config.regression_threshold_percent,
            )),
            report_generator: Arc::new(ReportGenerator::new(config.clone())),
            comparator: Arc::new(PerformanceComparator::new()),
            baseline_session: Arc::new(RwLock::new(None)),
        }
    }

    /// Start a new profiling session.
    pub async fn start_session(&self, name: impl Into<String>) -> ProfileSession {
        let session = ProfileSession::new(name);
        info!(
            "Started profiling session: {} ({})",
            session.name, session.id
        );
        session
    }

    /// End a profiling session and analyze results.
    pub async fn end_session(
        &self,
        mut session: ProfileSession,
    ) -> Result<PerformanceReport, crate::VoirsError> {
        session.complete();
        info!(
            "Ended profiling session: {} (duration: {:?})",
            session.name,
            session.duration()
        );

        // Detect bottlenecks if enabled
        if self.config.enable_bottleneck_detection {
            let bottlenecks = self.bottleneck_detector.detect(&session).await;
            for bottleneck in bottlenecks {
                session.add_bottleneck(bottleneck);
            }
        }

        // Store session in history
        let mut sessions = self.sessions.write().await;
        if sessions.len() >= self.config.max_history_size {
            sessions.pop_front();
        }
        sessions.push_back(session.clone());
        drop(sessions);

        // Compare with baseline if enabled
        let comparison = if self.config.enable_baseline_comparison {
            let baseline = self.baseline_session.read().await;
            if let Some(baseline_session) = baseline.as_ref() {
                Some(self.comparator.compare(&session, baseline_session).await)
            } else {
                None
            }
        } else {
            None
        };

        // Generate performance report
        let report = self.report_generator.generate(&session, comparison).await?;

        // Auto-save report if enabled
        if self.config.auto_generate_reports {
            if let Some(output_dir) = &self.config.report_output_dir {
                let report_path = output_dir.join(format!("{}.json", session.id));
                self.report_generator
                    .save_report(&report, &report_path)
                    .await?;
            }
        }

        Ok(report)
    }

    /// Set baseline session for comparison.
    pub async fn set_baseline(&self, session: ProfileSession) {
        let mut baseline = self.baseline_session.write().await;
        *baseline = Some(session);
        info!("Baseline session set for comparison");
    }

    /// Get all profiling sessions.
    pub async fn get_sessions(&self) -> Vec<ProfileSession> {
        self.sessions.read().await.iter().cloned().collect()
    }

    /// Clear session history.
    pub async fn clear_history(&self) {
        let mut sessions = self.sessions.write().await;
        sessions.clear();
        info!("Cleared profiling session history");
    }

    /// Get pipeline profiler for detailed stage profiling.
    pub fn pipeline_profiler(&self) -> Arc<PipelineProfiler> {
        self.pipeline_profiler.clone()
    }

    /// Get memory profiler for memory analysis.
    pub fn memory_profiler(&self) -> Arc<MemoryProfiler> {
        self.memory_profiler.clone()
    }

    /// Get bottleneck detector.
    pub fn bottleneck_detector(&self) -> Arc<BottleneckDetector> {
        self.bottleneck_detector.clone()
    }

    /// Generate aggregate report from multiple sessions.
    pub async fn generate_aggregate_report(
        &self,
        sessions: &[ProfileSession],
    ) -> Result<PerformanceReport, crate::VoirsError> {
        self.report_generator.generate_aggregate(sessions).await
    }
}

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

    #[test]
    fn test_profiler_config_default() {
        let config = ProfilerConfig::default();
        assert!(config.enable_timing);
        assert!(config.enable_memory);
        assert!(config.enable_bottleneck_detection);
        assert_eq!(config.max_history_size, 100);
    }

    #[test]
    fn test_profile_session_creation() {
        let session = ProfileSession::new("test_session");
        assert_eq!(session.name, "test_session");
        assert!(!session.is_complete());
        assert!(session.duration().is_none());
    }

    #[test]
    fn test_profile_session_completion() {
        let mut session = ProfileSession::new("test_session");
        session.complete();
        assert!(session.is_complete());
        assert!(session.duration().is_some());
    }

    #[test]
    fn test_profile_session_metadata() {
        let mut session = ProfileSession::new("test_session");
        session.add_metadata("key1", "value1");
        session.add_metadata("key2", "value2");
        assert_eq!(session.metadata.len(), 2);
        assert_eq!(session.metadata.get("key1"), Some(&"value1".to_string()));
    }

    #[tokio::test]
    async fn test_profiler_creation() {
        let config = ProfilerConfig::default();
        let profiler = Profiler::new(config);

        let sessions = profiler.get_sessions().await;
        assert_eq!(sessions.len(), 0);
    }

    #[tokio::test]
    async fn test_profiler_session_management() {
        let profiler = Profiler::new(ProfilerConfig::default());

        let session = profiler.start_session("test").await;
        assert_eq!(session.name, "test");

        // Note: We can't call end_session in this test without proper report generation
        // which requires the full implementation of sub-modules
    }

    #[tokio::test]
    async fn test_profiler_clear_history() {
        let profiler = Profiler::new(ProfilerConfig::default());
        profiler.clear_history().await;

        let sessions = profiler.get_sessions().await;
        assert_eq!(sessions.len(), 0);
    }
}