pluggable 0.1.0

A comprehensive, async plugin system for Rust applications with dependency management and security
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
//! Benchmarking utilities for plugin performance testing
//!
//! This module provides tools for measuring and analyzing plugin performance,
//! including execution time, memory usage, and throughput metrics.

use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

use crate::core::{Plugin, PluginContext, PluginResult};

/// Performance metrics for plugin execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginPerformanceMetrics {
    /// Plugin name
    pub plugin_name: String,
    /// Total execution time including initialization and cleanup
    pub total_time: Duration,
    /// Time spent in initialization phase
    pub init_time: Duration,
    /// Time spent in execution phase
    pub execute_time: Duration,
    /// Time spent in cleanup phase
    pub cleanup_time: Duration,
    /// Number of iterations (for benchmarking)
    pub iterations: u32,
    /// Average time per iteration
    pub avg_time_per_iteration: Duration,
    /// Minimum execution time observed
    pub min_time: Duration,
    /// Maximum execution time observed
    pub max_time: Duration,
    /// Standard deviation of execution times
    pub std_deviation: f64,
    /// Memory usage metrics (if available)
    pub memory_metrics: Option<MemoryMetrics>,
}

/// Memory usage metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryMetrics {
    /// Peak memory usage during execution (in bytes)
    pub peak_memory: u64,
    /// Memory allocated during execution (in bytes)
    pub allocated_memory: u64,
    /// Memory deallocated during execution (in bytes)
    pub deallocated_memory: u64,
}

/// Benchmark configuration
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of iterations to run
    pub iterations: u32,
    /// Warmup iterations (not counted in results)
    pub warmup_iterations: u32,
    /// Timeout for each iteration
    pub timeout: Duration,
    /// Whether to collect memory metrics
    pub collect_memory_metrics: bool,
    /// Whether to run iterations in parallel
    pub parallel: bool,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            iterations: 100,
            warmup_iterations: 10,
            timeout: Duration::from_secs(30),
            collect_memory_metrics: false,
            parallel: false,
        }
    }
}

/// Plugin benchmark runner
pub struct PluginBenchmark {
    config: BenchmarkConfig,
    results: Vec<PluginPerformanceMetrics>,
}

impl PluginBenchmark {
    /// Create a new benchmark runner
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            results: Vec::new(),
        }
    }

    /// Run benchmark for a single plugin
    pub async fn benchmark_plugin<P: Plugin>(
        &mut self,
        mut plugin: P,
        context: &PluginContext,
        config: serde_json::Value,
    ) -> PluginResult<PluginPerformanceMetrics> {
        let plugin_name = plugin.metadata().name.clone();
        let mut execution_times = Vec::new();
        let mut init_times = Vec::new();
        let mut execute_times = Vec::new();
        let mut cleanup_times = Vec::new();

        // Warmup iterations
        for _ in 0..self.config.warmup_iterations {
            let _ = self
                .run_single_iteration(&mut plugin, context, &config)
                .await?;
        }

        // Benchmark iterations
        for _ in 0..self.config.iterations {
            let iteration_metrics = self
                .run_single_iteration(&mut plugin, context, &config)
                .await?;

            execution_times.push(iteration_metrics.total_time);
            init_times.push(iteration_metrics.init_time);
            execute_times.push(iteration_metrics.execute_time);
            cleanup_times.push(iteration_metrics.cleanup_time);
        }

        // Calculate statistics
        let total_time = execution_times.iter().sum::<Duration>() / execution_times.len() as u32;
        let init_time = init_times.iter().sum::<Duration>() / init_times.len() as u32;
        let execute_time = execute_times.iter().sum::<Duration>() / execute_times.len() as u32;
        let cleanup_time = cleanup_times.iter().sum::<Duration>() / cleanup_times.len() as u32;

        let min_time = *execution_times.iter().min().unwrap();
        let max_time = *execution_times.iter().max().unwrap();

        let avg_nanos = execution_times
            .iter()
            .map(|d| d.as_nanos() as f64)
            .sum::<f64>()
            / execution_times.len() as f64;
        let variance = execution_times
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as f64 - avg_nanos;
                diff * diff
            })
            .sum::<f64>()
            / execution_times.len() as f64;
        let std_deviation = variance.sqrt();

        let metrics = PluginPerformanceMetrics {
            plugin_name,
            total_time,
            init_time,
            execute_time,
            cleanup_time,
            iterations: self.config.iterations,
            avg_time_per_iteration: total_time,
            min_time,
            max_time,
            std_deviation,
            memory_metrics: None, // TODO: Implement memory tracking
        };

        self.results.push(metrics.clone());
        Ok(metrics)
    }

    /// Run a single benchmark iteration
    async fn run_single_iteration<P: Plugin>(
        &self,
        plugin: &mut P,
        context: &PluginContext,
        config: &serde_json::Value,
    ) -> PluginResult<PluginPerformanceMetrics> {
        let start_time = Instant::now();

        // Initialize
        let init_start = Instant::now();
        plugin.initialize(config.clone(), context).await?;
        let init_time = init_start.elapsed();

        // Execute
        let execute_start = Instant::now();
        let mut context_clone = context.clone();
        let _output = plugin.execute(&mut context_clone).await?;
        let execute_time = execute_start.elapsed();

        // Cleanup
        let cleanup_start = Instant::now();
        plugin.cleanup(context).await?;
        let cleanup_time = cleanup_start.elapsed();

        let total_time = start_time.elapsed();

        Ok(PluginPerformanceMetrics {
            plugin_name: plugin.metadata().name.clone(),
            total_time,
            init_time,
            execute_time,
            cleanup_time,
            iterations: 1,
            avg_time_per_iteration: total_time,
            min_time: total_time,
            max_time: total_time,
            std_deviation: 0.0,
            memory_metrics: None,
        })
    }

    /// Get all benchmark results
    pub fn get_results(&self) -> &[PluginPerformanceMetrics] {
        &self.results
    }

    /// Generate a performance report
    pub fn generate_report(&self) -> PerformanceReport {
        PerformanceReport::new(&self.results)
    }

    /// Compare two plugins' performance
    pub fn compare_plugins(
        metrics1: &PluginPerformanceMetrics,
        metrics2: &PluginPerformanceMetrics,
    ) -> PluginComparison {
        let speedup = metrics1.avg_time_per_iteration.as_nanos() as f64
            / metrics2.avg_time_per_iteration.as_nanos() as f64;

        PluginComparison {
            plugin1: metrics1.plugin_name.clone(),
            plugin2: metrics2.plugin_name.clone(),
            speedup,
            faster_plugin: if speedup > 1.0 {
                metrics2.plugin_name.clone()
            } else {
                metrics1.plugin_name.clone()
            },
            time_difference: metrics1
                .avg_time_per_iteration
                .abs_diff(metrics2.avg_time_per_iteration),
        }
    }
}

/// Plugin performance comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginComparison {
    pub plugin1: String,
    pub plugin2: String,
    pub speedup: f64,
    pub faster_plugin: String,
    pub time_difference: Duration,
}

/// Performance report generator
#[derive(Debug, Clone)]
pub struct PerformanceReport {
    metrics: Vec<PluginPerformanceMetrics>,
}

impl PerformanceReport {
    /// Create a new performance report
    pub fn new(metrics: &[PluginPerformanceMetrics]) -> Self {
        Self {
            metrics: metrics.to_vec(),
        }
    }

    /// Generate a text report
    pub fn to_text(&self) -> String {
        let mut report = String::new();
        report.push_str("Plugin Performance Report\n");
        report.push_str("=========================\n\n");

        for metric in &self.metrics {
            report.push_str(&format!("Plugin: {}\n", metric.plugin_name));
            report.push_str(&format!("  Iterations: {}\n", metric.iterations));
            report.push_str(&format!(
                "  Average Time: {:?}\n",
                metric.avg_time_per_iteration
            ));
            report.push_str(&format!("  Min Time: {:?}\n", metric.min_time));
            report.push_str(&format!("  Max Time: {:?}\n", metric.max_time));
            report.push_str(&format!("  Std Deviation: {:.2}ns\n", metric.std_deviation));
            report.push_str(&format!("  Init Time: {:?}\n", metric.init_time));
            report.push_str(&format!("  Execute Time: {:?}\n", metric.execute_time));
            report.push_str(&format!("  Cleanup Time: {:?}\n", metric.cleanup_time));
            report.push('\n');
        }

        report
    }

    /// Generate a JSON report
    pub fn to_json(&self) -> PluginResult<String> {
        serde_json::to_string_pretty(&self.metrics)
            .map_err(|e| crate::core::PluginError::SerializationError(e.to_string()))
    }

    /// Get the fastest plugin
    pub fn fastest_plugin(&self) -> Option<&PluginPerformanceMetrics> {
        self.metrics.iter().min_by_key(|m| m.avg_time_per_iteration)
    }

    /// Get the slowest plugin
    pub fn slowest_plugin(&self) -> Option<&PluginPerformanceMetrics> {
        self.metrics.iter().max_by_key(|m| m.avg_time_per_iteration)
    }

    /// Calculate overall statistics
    pub fn overall_stats(&self) -> Option<OverallStats> {
        if self.metrics.is_empty() {
            return None;
        }

        let total_plugins = self.metrics.len();
        let avg_execution_time = self
            .metrics
            .iter()
            .map(|m| m.avg_time_per_iteration.as_nanos())
            .sum::<u128>()
            / total_plugins as u128;

        Some(OverallStats {
            total_plugins,
            avg_execution_time: Duration::from_nanos(avg_execution_time as u64),
            fastest_plugin: self.fastest_plugin().map(|m| m.plugin_name.clone()),
            slowest_plugin: self.slowest_plugin().map(|m| m.plugin_name.clone()),
        })
    }
}

/// Overall performance statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OverallStats {
    pub total_plugins: usize,
    pub avg_execution_time: Duration,
    pub fastest_plugin: Option<String>,
    pub slowest_plugin: Option<String>,
}

/// Utilities for creating performance tests
pub mod perf_test_utils {
    use super::*;
    use crate::core::testing::MockPlugin;

    /// Create a benchmark config for quick testing
    pub fn quick_benchmark_config() -> BenchmarkConfig {
        BenchmarkConfig {
            iterations: 10,
            warmup_iterations: 2,
            timeout: Duration::from_secs(5),
            collect_memory_metrics: false,
            parallel: false,
        }
    }

    /// Create a thorough benchmark config
    pub fn thorough_benchmark_config() -> BenchmarkConfig {
        BenchmarkConfig {
            iterations: 1000,
            warmup_iterations: 100,
            timeout: Duration::from_secs(30),
            collect_memory_metrics: true,
            parallel: false,
        }
    }

    /// Create a mock plugin with configurable delay for performance testing
    pub fn delayed_mock_plugin(name: &str, delay_ms: u64) -> MockPlugin {
        MockPlugin::new(name, "1.0.0").with_execute(move |_| {
            std::thread::sleep(Duration::from_millis(delay_ms));
            Ok(crate::core::PluginOutput::success(serde_json::json!({
                "delay_ms": delay_ms
            })))
        })
    }

    /// Create a CPU-intensive mock plugin for stress testing
    pub fn cpu_intensive_plugin(name: &str, iterations: u64) -> MockPlugin {
        MockPlugin::new(name, "1.0.0").with_execute(move |_| {
            // Perform CPU-intensive work
            let mut sum = 0u64;
            for i in 0..iterations {
                sum = sum.wrapping_add(i * i);
            }

            Ok(crate::core::PluginOutput::success(serde_json::json!({
                "cpu_work_result": sum,
                "iterations": iterations
            })))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{testing::MockPlugin, PluginContext};
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_plugin_benchmark_basic() {
        let temp_dir = TempDir::new().unwrap();
        let context = PluginContext::new("benchmark-test", temp_dir.path().to_path_buf());

        let plugin = MockPlugin::new("benchmark-plugin", "1.0.0");
        let config = BenchmarkConfig {
            iterations: 5,
            warmup_iterations: 1,
            ..Default::default()
        };

        let mut benchmark = PluginBenchmark::new(config);
        let metrics = benchmark
            .benchmark_plugin(plugin, &context, serde_json::json!({}))
            .await
            .unwrap();

        assert_eq!(metrics.plugin_name, "benchmark-plugin");
        assert_eq!(metrics.iterations, 5);
        assert!(metrics.total_time > Duration::from_nanos(0));
    }

    #[tokio::test]
    async fn test_performance_comparison() {
        let temp_dir = TempDir::new().unwrap();
        let context = PluginContext::new("comparison-test", temp_dir.path().to_path_buf());

        let fast_plugin = perf_test_utils::delayed_mock_plugin("fast", 1);
        let slow_plugin = perf_test_utils::delayed_mock_plugin("slow", 10);

        let config = perf_test_utils::quick_benchmark_config();
        let mut benchmark = PluginBenchmark::new(config);

        let fast_metrics = benchmark
            .benchmark_plugin(fast_plugin, &context, serde_json::json!({}))
            .await
            .unwrap();
        let slow_metrics = benchmark
            .benchmark_plugin(slow_plugin, &context, serde_json::json!({}))
            .await
            .unwrap();

        let comparison = PluginBenchmark::compare_plugins(&slow_metrics, &fast_metrics);
        assert_eq!(comparison.faster_plugin, "fast");
        assert!(comparison.speedup > 1.0);
    }

    #[test]
    fn test_performance_report() {
        let metrics = vec![
            PluginPerformanceMetrics {
                plugin_name: "plugin1".to_string(),
                total_time: Duration::from_millis(100),
                init_time: Duration::from_millis(10),
                execute_time: Duration::from_millis(80),
                cleanup_time: Duration::from_millis(10),
                iterations: 10,
                avg_time_per_iteration: Duration::from_millis(100),
                min_time: Duration::from_millis(90),
                max_time: Duration::from_millis(110),
                std_deviation: 5.0,
                memory_metrics: None,
            },
            PluginPerformanceMetrics {
                plugin_name: "plugin2".to_string(),
                total_time: Duration::from_millis(200),
                init_time: Duration::from_millis(20),
                execute_time: Duration::from_millis(160),
                cleanup_time: Duration::from_millis(20),
                iterations: 10,
                avg_time_per_iteration: Duration::from_millis(200),
                min_time: Duration::from_millis(180),
                max_time: Duration::from_millis(220),
                std_deviation: 10.0,
                memory_metrics: None,
            },
        ];

        let report = PerformanceReport::new(&metrics);

        assert_eq!(report.fastest_plugin().unwrap().plugin_name, "plugin1");
        assert_eq!(report.slowest_plugin().unwrap().plugin_name, "plugin2");

        let stats = report.overall_stats().unwrap();
        assert_eq!(stats.total_plugins, 2);
        assert_eq!(stats.fastest_plugin, Some("plugin1".to_string()));
        assert_eq!(stats.slowest_plugin, Some("plugin2".to_string()));

        let text_report = report.to_text();
        assert!(text_report.contains("Plugin: plugin1"));
        assert!(text_report.contains("Plugin: plugin2"));
    }
}