things3-cli 1.0.0

CLI tool for Things 3 with integrated MCP server
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Performance testing utilities for MCP operations

use crate::mcp::test_harness::McpTestHarness;
use serde_json::json;
use std::time::{Duration, Instant};
use tokio::time::timeout;

/// Performance test configuration
#[derive(Debug, Clone)]
pub struct PerformanceTestConfig {
    /// Maximum allowed duration for operations
    pub max_duration: Duration,
    /// Number of iterations to run for benchmarking
    pub iterations: usize,
    /// Whether to run concurrent tests
    pub run_concurrent: bool,
    /// Whether to include memory usage tracking
    pub track_memory: bool,
}

impl Default for PerformanceTestConfig {
    fn default() -> Self {
        Self {
            max_duration: Duration::from_secs(1),
            iterations: 10,
            run_concurrent: true,
            track_memory: false,
        }
    }
}

/// Performance test results
#[derive(Debug, Clone)]
pub struct PerformanceTestResults {
    /// Average duration across all iterations
    pub average_duration: Duration,
    /// Minimum duration observed
    pub min_duration: Duration,
    /// Maximum duration observed
    pub max_duration: Duration,
    /// Standard deviation of durations
    pub std_deviation: Duration,
    /// Number of successful operations
    pub success_count: usize,
    /// Number of failed operations
    pub failure_count: usize,
    /// Whether the test passed the performance threshold
    pub passed: bool,
}

/// Performance test runner for MCP operations
pub struct McpPerformanceTestRunner {
    harness: McpTestHarness,
    config: PerformanceTestConfig,
}

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

impl McpPerformanceTestRunner {
    /// Create a new performance test runner
    #[must_use]
    pub fn new() -> Self {
        Self {
            harness: McpTestHarness::new(),
            config: PerformanceTestConfig::default(),
        }
    }

    /// Create a new performance test runner with custom configuration
    #[must_use]
    pub fn with_config(config: PerformanceTestConfig) -> Self {
        Self {
            harness: McpTestHarness::new(),
            config,
        }
    }

    /// Run performance tests for tool calls
    pub async fn test_tool_performance(
        &self,
        tool_name: &str,
        arguments: Option<serde_json::Value>,
    ) -> PerformanceTestResults {
        let mut durations = Vec::new();
        let mut success_count = 0;
        let mut failure_count = 0;

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = timeout(
                self.config.max_duration,
                self.harness.call_tool(tool_name, arguments.clone()),
            )
            .await;

            match result {
                Ok(Ok(_)) => {
                    let duration = start.elapsed();
                    durations.push(duration);
                    success_count += 1;
                }
                Ok(Err(_)) | Err(_) => {
                    failure_count += 1;
                }
            }
        }

        self.calculate_results(&durations, success_count, failure_count)
    }

    /// Run performance tests for resource reads
    pub async fn test_resource_performance(&self, uri: &str) -> PerformanceTestResults {
        let mut durations = Vec::new();
        let mut success_count = 0;
        let mut failure_count = 0;

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = timeout(self.config.max_duration, self.harness.read_resource(uri)).await;

            match result {
                Ok(Ok(_)) => {
                    let duration = start.elapsed();
                    durations.push(duration);
                    success_count += 1;
                }
                Ok(Err(_)) | Err(_) => {
                    failure_count += 1;
                }
            }
        }

        self.calculate_results(&durations, success_count, failure_count)
    }

    /// Run performance tests for prompt calls
    pub async fn test_prompt_performance(
        &self,
        prompt_name: &str,
        arguments: Option<serde_json::Value>,
    ) -> PerformanceTestResults {
        let mut durations = Vec::new();
        let mut success_count = 0;
        let mut failure_count = 0;

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = timeout(
                self.config.max_duration,
                self.harness.get_prompt(prompt_name, arguments.clone()),
            )
            .await;

            match result {
                Ok(Ok(_)) => {
                    let duration = start.elapsed();
                    durations.push(duration);
                    success_count += 1;
                }
                Ok(Err(_)) | Err(_) => {
                    failure_count += 1;
                }
            }
        }

        self.calculate_results(&durations, success_count, failure_count)
    }

    /// Run concurrent performance tests
    pub async fn test_concurrent_performance(&self) -> ConcurrentPerformanceResults {
        if !self.config.run_concurrent {
            return ConcurrentPerformanceResults::default();
        }

        let start = Instant::now();

        // Run multiple operations concurrently
        let tool_futures = vec![
            self.harness.call_tool("get_inbox", None),
            self.harness.call_tool("get_today", None),
            self.harness.call_tool("get_areas", None),
        ];

        let resource_futures = vec![
            self.harness.read_resource("things://inbox"),
            self.harness.read_resource("things://today"),
        ];

        let prompt_futures = vec![self
            .harness
            .get_prompt("task_review", Some(json!({"task_title": "Test"})))];

        let tool_results = timeout(
            self.config.max_duration,
            futures::future::join_all(tool_futures),
        )
        .await;
        let resource_results = timeout(
            self.config.max_duration,
            futures::future::join_all(resource_futures),
        )
        .await;
        let prompt_results = timeout(
            self.config.max_duration,
            futures::future::join_all(prompt_futures),
        )
        .await;

        let mut success_count = 0;
        let mut total_operations = 0;

        if let Ok(results) = tool_results {
            success_count += results.iter().filter(|r| r.is_ok()).count();
            total_operations += results.len();
        }

        if let Ok(results) = resource_results {
            success_count += results.iter().filter(|r| r.is_ok()).count();
            total_operations += results.len();
        }

        if let Ok(results) = prompt_results {
            success_count += results.iter().filter(|r| r.is_ok()).count();
            total_operations += results.len();
        }

        let total_duration = start.elapsed();

        ConcurrentPerformanceResults {
            total_duration,
            success_count,
            total_operations,
            #[allow(clippy::cast_precision_loss)]
            operations_per_second: success_count as f64 / total_duration.as_secs_f64(),
        }
    }

    /// Run comprehensive performance tests
    pub async fn run_comprehensive_tests(&self) -> ComprehensivePerformanceResults {
        let tool_results = self.test_tool_performance("get_inbox", None).await;
        let resource_results = self.test_resource_performance("things://inbox").await;
        let prompt_results = self
            .test_prompt_performance("task_review", Some(json!({"task_title": "Test"})))
            .await;
        let concurrent_results = self.test_concurrent_performance().await;

        ComprehensivePerformanceResults {
            tool_performance: tool_results,
            resource_performance: resource_results,
            prompt_performance: prompt_results,
            concurrent_performance: concurrent_results,
        }
    }

    /// Calculate performance test results
    #[allow(clippy::cast_precision_loss)]
    fn calculate_results(
        &self,
        durations: &[Duration],
        success_count: usize,
        failure_count: usize,
    ) -> PerformanceTestResults {
        if durations.is_empty() {
            return PerformanceTestResults {
                average_duration: Duration::ZERO,
                min_duration: Duration::ZERO,
                max_duration: Duration::ZERO,
                std_deviation: Duration::ZERO,
                success_count,
                failure_count,
                passed: false,
            };
        }

        let total_duration: Duration = durations.iter().sum();
        let average_duration = total_duration / u32::try_from(durations.len()).unwrap_or(1);

        let min_duration = durations.iter().min().copied().unwrap_or(Duration::ZERO);
        let max_duration = durations.iter().max().copied().unwrap_or(Duration::ZERO);

        // Calculate standard deviation
        let variance: f64 = durations
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as f64 - average_duration.as_nanos() as f64;
                diff * diff
            })
            .sum::<f64>()
            / durations.len() as f64;

        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let std_deviation = Duration::from_nanos(variance.sqrt() as u64);

        let passed = average_duration <= self.config.max_duration && failure_count == 0;

        PerformanceTestResults {
            average_duration,
            min_duration,
            max_duration,
            std_deviation,
            success_count,
            failure_count,
            passed,
        }
    }
}

/// Results for concurrent performance tests
#[derive(Debug, Clone, Default)]
pub struct ConcurrentPerformanceResults {
    pub total_duration: Duration,
    pub success_count: usize,
    pub total_operations: usize,
    pub operations_per_second: f64,
}

/// Comprehensive performance test results
#[derive(Debug, Clone)]
pub struct ComprehensivePerformanceResults {
    pub tool_performance: PerformanceTestResults,
    pub resource_performance: PerformanceTestResults,
    pub prompt_performance: PerformanceTestResults,
    pub concurrent_performance: ConcurrentPerformanceResults,
}

impl ComprehensivePerformanceResults {
    /// Check if all performance tests passed
    #[must_use]
    pub fn all_passed(&self) -> bool {
        self.tool_performance.passed
            && self.resource_performance.passed
            && self.prompt_performance.passed
    }

    /// Get the overall performance score (0.0 to 1.0)
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn performance_score(&self) -> f64 {
        let mut score = 0.0;
        let mut count = 0;

        if self.tool_performance.success_count > 0 {
            score += self.tool_performance.success_count as f64
                / (self.tool_performance.success_count + self.tool_performance.failure_count)
                    as f64;
            count += 1;
        }

        if self.resource_performance.success_count > 0 {
            score += self.resource_performance.success_count as f64
                / (self.resource_performance.success_count
                    + self.resource_performance.failure_count) as f64;
            count += 1;
        }

        if self.prompt_performance.success_count > 0 {
            score += self.prompt_performance.success_count as f64
                / (self.prompt_performance.success_count + self.prompt_performance.failure_count)
                    as f64;
            count += 1;
        }

        if count > 0 {
            score / f64::from(count)
        } else {
            0.0
        }
    }
}

/// Memory usage tracker for performance tests
pub struct MemoryTracker {
    initial_memory: Option<usize>,
    peak_memory: Option<usize>,
}

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

impl MemoryTracker {
    #[must_use]
    pub fn new() -> Self {
        Self {
            initial_memory: None,
            peak_memory: None,
        }
    }

    pub fn start(&mut self) {
        self.initial_memory = Some(Self::get_current_memory_usage());
        self.peak_memory = self.initial_memory;
    }

    pub fn update(&mut self) {
        let current = Self::get_current_memory_usage();
        if let Some(peak) = self.peak_memory {
            if current > peak {
                self.peak_memory = Some(current);
            }
        } else {
            self.peak_memory = Some(current);
        }
    }

    #[must_use]
    pub fn get_memory_usage(&self) -> Option<usize> {
        self.peak_memory
            .and_then(|peak| self.initial_memory.map(|initial| peak - initial))
    }

    fn get_current_memory_usage() -> usize {
        // This is a simplified implementation
        // In a real implementation, you might use system-specific APIs
        // or external tools to get accurate memory usage
        0
    }
}

/// Performance benchmark for specific MCP operations
pub struct McpBenchmark {
    harness: McpTestHarness,
    config: PerformanceTestConfig,
}

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

impl McpBenchmark {
    #[must_use]
    pub fn new() -> Self {
        Self {
            harness: McpTestHarness::new(),
            config: PerformanceTestConfig::default(),
        }
    }

    #[must_use]
    pub fn with_config(config: PerformanceTestConfig) -> Self {
        Self {
            harness: McpTestHarness::new(),
            config,
        }
    }

    /// Benchmark a specific tool call
    ///
    /// # Panics
    /// Panics if the tool call fails during benchmarking
    pub async fn benchmark_tool(
        &self,
        tool_name: &str,
        arguments: Option<serde_json::Value>,
    ) -> BenchmarkResults {
        let mut durations = Vec::new();
        let mut memory_tracker = MemoryTracker::new();

        memory_tracker.start();

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = self.harness.call_tool(tool_name, arguments.clone()).await;

            let duration = start.elapsed();
            durations.push(duration);

            memory_tracker.update();

            // Ensure the operation succeeded
            assert!(
                result.is_ok(),
                "Tool call '{tool_name}' failed during benchmark"
            );
        }

        Self::calculate_benchmark_results(&durations, &memory_tracker)
    }

    /// Benchmark a specific resource read
    ///
    /// # Panics
    /// Panics if the resource read fails during benchmarking
    pub async fn benchmark_resource(&self, uri: &str) -> BenchmarkResults {
        let mut durations = Vec::new();
        let mut memory_tracker = MemoryTracker::new();

        memory_tracker.start();

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = self.harness.read_resource(uri).await;

            let duration = start.elapsed();
            durations.push(duration);

            memory_tracker.update();

            // Ensure the operation succeeded
            assert!(
                result.is_ok(),
                "Resource read '{uri}' failed during benchmark"
            );
        }

        Self::calculate_benchmark_results(&durations, &memory_tracker)
    }

    /// Benchmark a specific prompt call
    ///
    /// # Panics
    /// Panics if the prompt call fails during benchmarking
    pub async fn benchmark_prompt(
        &self,
        prompt_name: &str,
        arguments: Option<serde_json::Value>,
    ) -> BenchmarkResults {
        let mut durations = Vec::new();
        let mut memory_tracker = MemoryTracker::new();

        memory_tracker.start();

        for _ in 0..self.config.iterations {
            let start = Instant::now();

            let result = self
                .harness
                .get_prompt(prompt_name, arguments.clone())
                .await;

            let duration = start.elapsed();
            durations.push(duration);

            memory_tracker.update();

            // Ensure the operation succeeded
            assert!(
                result.is_ok(),
                "Prompt call '{prompt_name}' failed during benchmark"
            );
        }

        Self::calculate_benchmark_results(&durations, &memory_tracker)
    }

    #[allow(
        clippy::cast_precision_loss,
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss
    )]
    fn calculate_benchmark_results(
        durations: &[Duration],
        memory_tracker: &MemoryTracker,
    ) -> BenchmarkResults {
        if durations.is_empty() {
            return BenchmarkResults::default();
        }

        let total_duration: Duration = durations.iter().sum();
        let average_duration = total_duration / u32::try_from(durations.len()).unwrap_or(1);

        let min_duration = durations.iter().min().copied().unwrap_or(Duration::ZERO);
        let max_duration = durations.iter().max().copied().unwrap_or(Duration::ZERO);

        // Calculate percentiles
        let mut sorted_durations = durations.to_owned();
        sorted_durations.sort();

        let p50_index = (sorted_durations.len() * 50) / 100;
        let p95_index = (sorted_durations.len() * 95) / 100;
        let p99_index = (sorted_durations.len() * 99) / 100;

        let p50 = sorted_durations
            .get(p50_index)
            .copied()
            .unwrap_or(Duration::ZERO);
        let p95 = sorted_durations
            .get(p95_index)
            .copied()
            .unwrap_or(Duration::ZERO);
        let p99 = sorted_durations
            .get(p99_index)
            .copied()
            .unwrap_or(Duration::ZERO);

        BenchmarkResults {
            iterations: durations.len(),
            average_duration,
            min_duration,
            max_duration,
            p50_duration: p50,
            p95_duration: p95,
            p99_duration: p99,
            memory_usage: memory_tracker.get_memory_usage(),
        }
    }
}

/// Results for benchmark tests
#[derive(Debug, Clone, Default)]
pub struct BenchmarkResults {
    pub iterations: usize,
    pub average_duration: Duration,
    pub min_duration: Duration,
    pub max_duration: Duration,
    pub p50_duration: Duration,
    pub p95_duration: Duration,
    pub p99_duration: Duration,
    pub memory_usage: Option<usize>,
}

impl BenchmarkResults {
    /// Get the operations per second
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn operations_per_second(&self) -> f64 {
        if self.average_duration.as_nanos() > 0 {
            1_000_000_000.0 / self.average_duration.as_nanos() as f64
        } else {
            0.0
        }
    }

    /// Check if the benchmark meets performance requirements
    #[must_use]
    pub fn meets_requirements(
        &self,
        max_average_duration: Duration,
        min_ops_per_second: f64,
    ) -> bool {
        self.average_duration <= max_average_duration
            && self.operations_per_second() >= min_ops_per_second
    }
}

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

    #[tokio::test]
    async fn test_performance_test_runner() {
        let runner = McpPerformanceTestRunner::new();

        // Test tool performance
        let results = runner.test_tool_performance("get_inbox", None).await;
        assert!(results.success_count > 0);
        assert!(results.average_duration > Duration::ZERO);

        // Test resource performance
        let results = runner.test_resource_performance("things://inbox").await;
        assert!(results.success_count > 0);

        // Test prompt performance
        let results = runner
            .test_prompt_performance("task_review", Some(json!({"task_title": "Test"})))
            .await;
        assert!(results.success_count > 0);
    }

    #[tokio::test]
    async fn test_concurrent_performance() {
        let runner = McpPerformanceTestRunner::new();

        let results = runner.test_concurrent_performance().await;
        assert!(results.success_count > 0);
        assert!(results.operations_per_second > 0.0);
    }

    #[tokio::test]
    async fn test_comprehensive_performance() {
        let runner = McpPerformanceTestRunner::new();

        let results = runner.run_comprehensive_tests().await;
        assert!(results.tool_performance.success_count > 0);
        assert!(results.resource_performance.success_count > 0);
        assert!(results.prompt_performance.success_count > 0);

        let score = results.performance_score();
        assert!(score > 0.0);
        assert!(score <= 1.0);
    }

    #[tokio::test]
    async fn test_benchmark() {
        let benchmark = McpBenchmark::new();

        // Test tool benchmark
        let results = benchmark.benchmark_tool("get_inbox", None).await;
        assert!(results.iterations > 0);
        assert!(results.average_duration > Duration::ZERO);
        assert!(results.operations_per_second() > 0.0);

        // Test resource benchmark
        let results = benchmark.benchmark_resource("things://inbox").await;
        assert!(results.iterations > 0);

        // Test prompt benchmark
        let results = benchmark
            .benchmark_prompt("task_review", Some(json!({"task_title": "Test"})))
            .await;
        assert!(results.iterations > 0);
    }

    #[tokio::test]
    async fn test_benchmark_requirements() {
        let benchmark = McpBenchmark::new();

        let results = benchmark.benchmark_tool("get_inbox", None).await;

        // Test that it meets reasonable requirements
        let meets_requirements = results.meets_requirements(Duration::from_secs(1), 1.0);
        assert!(meets_requirements);
    }

    #[tokio::test]
    async fn test_memory_tracker() {
        let mut tracker = MemoryTracker::new();
        tracker.start();
        tracker.update();

        // Memory usage should be available
        let usage = tracker.get_memory_usage();
        assert!(usage.is_some());
    }

    #[tokio::test]
    async fn test_performance_config() {
        let config = PerformanceTestConfig {
            max_duration: Duration::from_millis(500),
            iterations: 5,
            run_concurrent: false,
            track_memory: true,
        };

        let runner = McpPerformanceTestRunner::with_config(config);
        let results = runner.test_tool_performance("get_inbox", None).await;

        assert_eq!(results.success_count + results.failure_count, 5);
    }
}