trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
//! Mobile Performance Profiler for Debugging
//!
//! This module has been refactored into a modular architecture for better maintainability.
//! All functionality has been organized into focused sub-modules while maintaining
//! backward compatibility through comprehensive re-exports.
//!
//! # Architecture Overview
//!
//! The mobile performance profiler is organized into the following modules:
//! - `types`: Core types, metrics structures, and shared data models
//! - `config`: Configuration management for profiling parameters
//! - `metrics`: Metrics collection, snapshots, and aggregation
//! - `bottleneck_detection`: Performance bottleneck detection and analysis
//! - `optimization`: Optimization engine and suggestion generation
//! - `monitoring`: Real-time monitoring, alerting, and event handling
//! - `export`: Data export, visualization, and reporting
//! - `session`: Session management and profiling lifecycle
//! - `analysis`: Performance analysis, trending, and health assessment
//! - `profiler`: Main profiler coordinator and public API
//!
//! # Usage
//!
//! All previous functionality remains available at the same import paths:
//!
//! ```rust
//! use trustformers_mobile::mobile_performance_profiler_legacy::{
//!     MobilePerformanceProfiler, MobileProfilerConfig,
//!     MobileMetricsCollector, BottleneckDetector
//! };
//! ```

// Import the modular structure
use crate::mobile_performance_profiler;

// Re-export everything to maintain backward compatibility
pub use mobile_performance_profiler::*;

// Legacy re-exports for backward compatibility
pub use mobile_performance_profiler::{
    AlertManager,

    AlertType,
    BottleneckDetector,
    BottleneckType,
    CollectionStatistics,

    CpuProfilingConfig,
    ExportConfig,

    ExportFormat,
    HealthStatus,

    InferenceMetrics,
    MemoryMetrics,
    MemoryProfilingConfig,
    MobileMetricsCollector,

    // Metrics types
    MobileMetricsSnapshot,
    // Core service types
    MobilePerformanceProfiler,
    // Configuration types
    MobileProfilerConfig,
    OptimizationEngine,
    OptimizationSuggestion,
    PerformanceAlert,
    // Analysis types
    PerformanceAnalyzer,
    PerformanceBottleneck,
    // Export types
    ProfilerExportManager,
    // Session types
    ProfilingSession,
    // Monitoring types
    RealTimeMonitor,
    SamplingConfig,
    SessionInfo,

    SessionMetadata,
    SuggestionType,
    SystemHealth,
    TrendDirection,
    TrendingMetrics,
    VisualizationEngine,
};

// Legacy compatibility types (maintain exact same interface as before)
pub type MobilePerformanceProfilerLegacy = MobilePerformanceProfiler;

// Legacy initialization functions for backward compatibility
// Note: Specific init functions not available in current implementation

// Additional convenience functions

/// Create a default mobile performance profiler
pub fn create_default_mobile_profiler(
) -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let config = MobileProfilerConfig::default();
    Ok(MobilePerformanceProfiler::new(config)?)
}

/// Create a mobile profiler optimized for high-frequency profiling
pub fn create_high_frequency_profiler(
) -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let mut config = MobileProfilerConfig::default();
    config.sampling.cpu_sampling_interval_ms = 50;
    config.sampling.memory_sampling_interval_ms = 100;
    config.sampling.thermal_sampling_interval_ms = 200;
    config.sampling.battery_sampling_interval_ms = 1000;
    config.mode = ProfilingMode::Debug; // High frequency profiling with detailed debugging
    Ok(MobilePerformanceProfiler::new(config)?)
}

/// Create a mobile profiler optimized for minimal battery impact
pub fn create_battery_optimized_profiler(
) -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let mut config = MobileProfilerConfig::default();
    config.sampling.cpu_sampling_interval_ms = 1000;
    config.sampling.memory_sampling_interval_ms = 2000;
    config.sampling.thermal_sampling_interval_ms = 5000;
    config.sampling.battery_sampling_interval_ms = 10000;
    config.mode = ProfilingMode::Production; // Lightweight profiling for battery optimization
    Ok(MobilePerformanceProfiler::new(config)?)
}

/// Create a mobile profiler with custom sampling intervals
pub fn create_custom_sampling_profiler(
    cpu_interval_ms: u64,
    memory_interval_ms: u64,
    thermal_interval_ms: u64,
    battery_interval_ms: u64,
) -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let mut config = MobileProfilerConfig::default();
    config.sampling.cpu_sampling_interval_ms = cpu_interval_ms;
    config.sampling.memory_sampling_interval_ms = memory_interval_ms;
    config.sampling.thermal_sampling_interval_ms = thermal_interval_ms;
    config.sampling.battery_sampling_interval_ms = battery_interval_ms;
    Ok(MobilePerformanceProfiler::new(config)?)
}

/// Create a mobile profiler optimized for GPU-intensive applications
pub fn create_gpu_optimized_profiler(
) -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let mut config = MobileProfilerConfig::default();
    config.gpu_profiling.enable_gpu_metrics = true;
    config.gpu_profiling.enable_memory_tracking = true;
    config.gpu_profiling.enable_performance_counters = true;
    config.gpu_profiling.sampling_interval_ms = 100;
    Ok(MobilePerformanceProfiler::new(config)?)
}

/// Get mobile profiler capabilities
pub fn get_profiler_capabilities() -> ProfilerCapabilities {
    get_mobile_profiler_capabilities()
}

/// Validate profiler configuration
fn validate_profiler_config(
    config: &MobileProfilerConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    if config.sampling.interval_ms == 0 {
        return Err("Sampling interval must be greater than 0".into());
    }
    if config.sampling.max_samples == 0 {
        return Err("Max samples must be greater than 0".into());
    }
    Ok(())
}

/// Validate that the mobile profiler is properly configured and functional
pub fn validate_mobile_profiler_system(
) -> Result<ProfilerValidationReport, Box<dyn std::error::Error>> {
    let config = MobileProfilerConfig::default();
    validate_profiler_config(&config)?;

    let profiler = MobilePerformanceProfiler::new(config)?;

    // Test basic profiler operations
    let health = profiler.health_check()?;
    let modern_capabilities = profiler.get_capabilities()?;

    let validation_passed = matches!(
        health.status,
        HealthStatus::Excellent | HealthStatus::Good | HealthStatus::Healthy
    ) && modern_capabilities.real_time_monitoring;

    // Convert modern capabilities to legacy format
    let legacy_capabilities = ProfilerCapabilities {
        supported_metrics: vec![
            "CPU".to_string(),
            "Memory".to_string(),
            "Thermal".to_string(),
            "Battery".to_string(),
            "GPU".to_string(),
            "Network".to_string(),
            "Inference".to_string(),
        ],
        supported_platforms: vec![
            "iOS".to_string(),
            "Android".to_string(),
            "Generic".to_string(),
        ],
        real_time_profiling: modern_capabilities.real_time_monitoring,
        gpu_profiling_support: modern_capabilities.gpu_profiling,
        thermal_monitoring: modern_capabilities.thermal_monitoring,
        battery_monitoring: modern_capabilities.battery_monitoring,
        memory_profiling: modern_capabilities.memory_profiling,
        cpu_profiling: modern_capabilities.cpu_profiling,
        network_monitoring: modern_capabilities.network_profiling,
        bottleneck_detection: true,
        optimization_suggestions: true,
        export_formats: vec![
            "JSON".to_string(),
            "CSV".to_string(),
            "HTML".to_string(),
            "Binary".to_string(),
        ],
    };

    Ok(ProfilerValidationReport {
        validation_passed,
        profiler_health: health.status,
        capabilities: legacy_capabilities,
        validation_errors: vec![],
        recommendations: health.recommendations,
    })
}

/// Mobile profiler validation report
#[derive(Debug, Clone)]
pub struct ProfilerValidationReport {
    pub validation_passed: bool,
    pub profiler_health: HealthStatus,
    pub capabilities: ProfilerCapabilities,
    pub validation_errors: Vec<String>,
    pub recommendations: Vec<String>,
}

/// Mobile profiler capabilities
#[derive(Debug, Clone)]
pub struct ProfilerCapabilities {
    pub supported_metrics: Vec<String>,
    pub supported_platforms: Vec<String>,
    pub real_time_profiling: bool,
    pub gpu_profiling_support: bool,
    pub thermal_monitoring: bool,
    pub battery_monitoring: bool,
    pub memory_profiling: bool,
    pub cpu_profiling: bool,
    pub network_monitoring: bool,
    pub bottleneck_detection: bool,
    pub optimization_suggestions: bool,
    pub export_formats: Vec<String>,
}

/// Get mobile profiler capabilities
pub fn get_mobile_profiler_capabilities() -> ProfilerCapabilities {
    ProfilerCapabilities {
        supported_metrics: vec![
            "CPU".to_string(),
            "Memory".to_string(),
            "Thermal".to_string(),
            "Battery".to_string(),
            "GPU".to_string(),
            "Network".to_string(),
            "Inference".to_string(),
        ],
        supported_platforms: vec![
            "iOS".to_string(),
            "Android".to_string(),
            "Generic".to_string(),
        ],
        real_time_profiling: true,
        gpu_profiling_support: true,
        thermal_monitoring: true,
        battery_monitoring: true,
        memory_profiling: true,
        cpu_profiling: true,
        network_monitoring: true,
        bottleneck_detection: true,
        optimization_suggestions: true,
        export_formats: vec![
            "JSON".to_string(),
            "CSV".to_string(),
            "HTML".to_string(),
            "Binary".to_string(),
        ],
    }
}

/// Utility functions for common profiling patterns

/// Quick performance assessment for immediate decision making
pub fn quick_performance_assessment() -> Result<SystemHealth, Box<dyn std::error::Error>> {
    let profiler = create_default_mobile_profiler()?;
    let snapshot = profiler.take_snapshot()?;
    Ok(profiler.assess_system_health()?)
}

/// Start profiling with smart defaults based on device characteristics
pub fn start_smart_profiling() -> Result<MobilePerformanceProfiler, Box<dyn std::error::Error>> {
    let device_info = crate::device_info::MobileDeviceDetector::detect()?;

    let profiler = match device_info.performance_scores.overall_tier {
        crate::device_info::PerformanceTier::VeryLow => create_battery_optimized_profiler()?,
        crate::device_info::PerformanceTier::Low => create_battery_optimized_profiler()?,
        crate::device_info::PerformanceTier::Budget => create_default_mobile_profiler()?,
        crate::device_info::PerformanceTier::Medium => create_default_mobile_profiler()?,
        crate::device_info::PerformanceTier::Mid => create_default_mobile_profiler()?,
        crate::device_info::PerformanceTier::High => create_high_frequency_profiler()?,
        crate::device_info::PerformanceTier::VeryHigh => create_high_frequency_profiler()?,
        crate::device_info::PerformanceTier::Flagship => create_high_frequency_profiler()?,
    };

    Ok(profiler)
}

/// Get performance recommendations based on current device state
pub fn get_performance_recommendations(
) -> Result<Vec<OptimizationSuggestion>, Box<dyn std::error::Error>> {
    let profiler = create_default_mobile_profiler()?;
    let snapshot = profiler.take_snapshot()?;
    Ok(profiler.get_optimization_suggestions()?)
}

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

    #[test]
    fn test_mobile_profiler_creation() {
        let profiler = create_default_mobile_profiler();
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_high_frequency_profiler() {
        let profiler = create_high_frequency_profiler();
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_battery_optimized_profiler() {
        let profiler = create_battery_optimized_profiler();
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_custom_sampling_profiler() {
        let profiler = create_custom_sampling_profiler(500, 1000, 2000, 5000);
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_gpu_optimized_profiler() {
        let profiler = create_gpu_optimized_profiler();
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_profiler_capabilities() {
        let capabilities = get_profiler_capabilities();
        assert!(!capabilities.supported_metrics.is_empty());
        assert!(capabilities.real_time_profiling);
        assert!(capabilities.bottleneck_detection);
        assert!(capabilities.optimization_suggestions);
    }

    #[test]
    fn test_validation_system() {
        let report = validate_mobile_profiler_system();
        assert!(report.is_ok());

        if let Ok(validation) = report {
            assert!(validation.validation_passed);
        }
    }

    #[test]
    fn test_quick_performance_assessment() {
        let assessment = quick_performance_assessment();
        assert!(assessment.is_ok());
    }

    #[test]
    fn test_backward_compatibility() {
        // Test that old code patterns still work
        let config = MobileProfilerConfig::default();
        let profiler = MobilePerformanceProfiler::new(config);
        assert!(profiler.is_ok());

        // Test legacy type alias
        if let Ok(profiler) = profiler {
            let _legacy_profiler: MobilePerformanceProfilerLegacy = profiler;
        }
    }

    #[test]
    fn test_module_integration() {
        // Test that all modules work together seamlessly
        let profiler = create_default_mobile_profiler().expect("Operation failed");

        let snapshot = profiler.take_snapshot();
        assert!(snapshot.is_ok());

        let health = profiler.health_check();
        let health_unwrapped = health.expect("Operation failed");
        // Health should be Good or better (Excellent, Good, or Healthy are all acceptable)
        assert!(
            matches!(
                health_unwrapped.status,
                HealthStatus::Excellent | HealthStatus::Good | HealthStatus::Healthy
            ),
            "Expected healthy status, got: {:?}",
            health_unwrapped.status
        );
    }

    #[test]
    fn test_profiler_capabilities_completeness() {
        let capabilities = get_mobile_profiler_capabilities();

        // Verify all expected metrics are supported
        let expected_metrics = vec![
            "CPU",
            "Memory",
            "Thermal",
            "Battery",
            "GPU",
            "Network",
            "Inference",
        ];
        for metric in expected_metrics {
            assert!(capabilities.supported_metrics.contains(&metric.to_string()));
        }

        // Verify all expected platforms are supported
        let expected_platforms = vec!["iOS", "Android", "Generic"];
        for platform in expected_platforms {
            assert!(capabilities.supported_platforms.contains(&platform.to_string()));
        }

        // Verify all expected export formats are supported
        let expected_formats = vec!["JSON", "CSV", "HTML", "Binary"];
        for format in expected_formats {
            assert!(capabilities.export_formats.contains(&format.to_string()));
        }
    }

    #[test]
    fn test_smart_profiling_initialization() {
        let result = start_smart_profiling();
        assert!(result.is_ok());
    }

    #[test]
    fn test_performance_recommendations() {
        let recommendations = get_performance_recommendations();
        assert!(recommendations.is_ok());
    }

    #[test]
    fn test_validation_report_structure() {
        let config = MobileProfilerConfig::default();
        let profiler = MobilePerformanceProfiler::new(config).expect("Operation failed");
        let health = profiler.health_check();
        let capabilities = profiler.get_capabilities();

        let health_unwrapped = health.expect("Operation failed");
        let modern_capabilities = capabilities.expect("Operation failed");

        // Convert modern capabilities to legacy format
        let legacy_capabilities = ProfilerCapabilities {
            supported_metrics: vec![
                "cpu_usage".to_string(),
                "memory_usage".to_string(),
                "gpu_utilization".to_string(),
                "thermal_state".to_string(),
                "battery_level".to_string(),
            ],
            supported_platforms: vec!["android".to_string(), "ios".to_string()],
            real_time_profiling: modern_capabilities.real_time_monitoring,
            gpu_profiling_support: modern_capabilities.gpu_profiling,
            thermal_monitoring: modern_capabilities.thermal_monitoring,
            battery_monitoring: modern_capabilities.battery_monitoring,
            memory_profiling: modern_capabilities.memory_profiling,
            cpu_profiling: true,
            network_monitoring: true,
            bottleneck_detection: true,
            optimization_suggestions: true,
            export_formats: vec!["json".to_string(), "csv".to_string()],
        };

        let report = ProfilerValidationReport {
            validation_passed: true,
            profiler_health: health_unwrapped.status,
            capabilities: legacy_capabilities,
            validation_errors: vec![],
            recommendations: health_unwrapped.recommendations,
        };

        assert!(report.validation_passed);
        // Health should be Good or better (Excellent, Good, or Healthy are all acceptable)
        assert!(
            matches!(
                report.profiler_health,
                HealthStatus::Excellent | HealthStatus::Good | HealthStatus::Healthy
            ),
            "Expected healthy status, got: {:?}",
            report.profiler_health
        );
    }
}