claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
//! Application profiler for performance analysis

use super::*;
use crate::cli::error::Result;
use crate::cli::history::store::{EnhancedHistoryStore, FullTextSearch, SearchField};
use crate::cli::history::{HistoryEntry, HistorySearch, HistoryStore};
use crate::cli::session::SessionManager;
use std::sync::Arc;
use tempfile::TempDir;
use uuid::Uuid;

/// Main profiler for the application
pub struct ApplicationProfiler {
    temp_dir: TempDir,
    results: Vec<ProfilingResult>,
}

impl ApplicationProfiler {
    /// Create a new application profiler
    pub fn new() -> Result<Self> {
        let temp_dir = TempDir::new()?;
        Ok(Self {
            temp_dir,
            results: Vec::new(),
        })
    }

    /// Run a comprehensive profiling suite with the given configuration
    pub async fn run_profiling_suite(
        &mut self,
        config: ProfilingConfig,
    ) -> Result<Vec<ProfilingResult>> {
        println!(
            "Starting profiling suite with {} operations on {} entries",
            config.operation_count, config.dataset_size
        );

        let mut suite_results = Vec::new();

        for operation_type in &config.operation_types {
            let result = match operation_type {
                OperationType::HistoryStore => self.profile_history_storage(&config).await?,
                OperationType::HistorySearch => self.profile_history_search(&config).await?,
                OperationType::FullTextSearch => self.profile_full_text_search(&config).await?,
                OperationType::IndexOptimization => {
                    self.profile_index_optimization(&config).await?
                }
                OperationType::SessionManagement => {
                    self.profile_session_management(&config).await?
                }
                OperationType::BatchOperations => self.profile_batch_operations(&config).await?,
            };

            suite_results.push(result);
        }

        self.results.extend(suite_results.clone());
        Ok(suite_results)
    }

    /// Profile history storage operations
    async fn profile_history_storage(&self, config: &ProfilingConfig) -> Result<ProfilingResult> {
        let storage_path = self.temp_dir.path().join("history_profile.json");
        let mut store = HistoryStore::new(storage_path)?;

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        // Generate test data
        let entries = self.generate_test_entries(config.dataset_size);

        // Warmup if configured
        if config.warmup {
            for entry in entries.iter().take(config.warmup_iterations) {
                store.store_entry(entry.clone()).await?;
            }
        }

        let timer = Timer::start("history_storage");

        // Perform storage operations
        for entry in entries.iter().take(config.operation_count) {
            let storage_timer = Timer::start("single_store");
            store.store_entry(entry.clone()).await?;
            let storage_duration = storage_timer.stop();

            metrics.storage_metrics.writes += 1;
            metrics.storage_metrics.write_time += storage_duration;
        }

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = config.operation_count;
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();
        metrics.calculate_storage_averages();

        Ok(ProfilingResult {
            scenario_name: "History Storage".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Profile history search operations
    async fn profile_history_search(&self, config: &ProfilingConfig) -> Result<ProfilingResult> {
        let storage_path = self.temp_dir.path().join("search_profile.json");
        let mut store = HistoryStore::new(storage_path)?;

        // Populate with test data
        let entries = self.generate_test_entries(config.dataset_size);
        for entry in &entries {
            store.store_entry(entry.clone()).await?;
        }

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        // Generate search criteria
        let search_patterns = self.generate_search_patterns(&entries);

        let timer = Timer::start("history_search");

        // Perform search operations
        for i in 0..config.operation_count {
            let pattern = &search_patterns[i % search_patterns.len()];
            let search_timer = Timer::start("single_search");

            let _results = store.search(pattern).await?;

            let search_duration = search_timer.stop();
            metrics.search_metrics.search_count += 1;
            metrics.search_metrics.total_search_time += search_duration;
        }

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = config.operation_count;
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();
        metrics.calculate_search_averages();

        Ok(ProfilingResult {
            scenario_name: "History Search".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Profile full-text search operations
    async fn profile_full_text_search(&self, config: &ProfilingConfig) -> Result<ProfilingResult> {
        let storage_path = self.temp_dir.path().join("fulltext_profile");
        let store = EnhancedHistoryStore::new(storage_path)?;

        // Populate with test data
        let entries = self.generate_test_entries(config.dataset_size);
        // Note: For now, we simulate this. In a real implementation,
        // we would populate the enhanced store with test data

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        // Generate full-text search queries
        let search_queries = self.generate_fulltext_queries(&entries);

        let timer = Timer::start("fulltext_search");

        // Perform full-text search operations
        for i in 0..config.operation_count {
            let query = &search_queries[i % search_queries.len()];
            let search_timer = Timer::start("single_fulltext_search");

            let _results = store.full_text_search(query).await?;

            let search_duration = search_timer.stop();
            metrics.search_metrics.search_count += 1;
            metrics.search_metrics.total_search_time += search_duration;
        }

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = config.operation_count;
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();
        metrics.calculate_search_averages();

        Ok(ProfilingResult {
            scenario_name: "Full-Text Search".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Profile index optimization operations
    async fn profile_index_optimization(
        &self,
        config: &ProfilingConfig,
    ) -> Result<ProfilingResult> {
        let storage_path = self.temp_dir.path().join("index_profile");
        let mut store = EnhancedHistoryStore::new(storage_path)?;

        // Populate with test data first
        let _entries = self.generate_test_entries(config.dataset_size);
        // Simulate populating the store

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        let timer = Timer::start("index_optimization");

        // Perform index optimization
        let optimization_result = store.optimize_index().await?;

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = 1; // Single optimization operation
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();

        let mut metadata = HashMap::new();
        metadata.insert(
            "optimization_duration".to_string(),
            format!("{:?}", optimization_result.optimization_duration),
        );
        metadata.insert(
            "words_removed".to_string(),
            optimization_result.removed_words.to_string(),
        );
        metadata.insert(
            "memory_saved".to_string(),
            optimization_result.memory_saved.to_string(),
        );

        Ok(ProfilingResult {
            scenario_name: "Index Optimization".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata,
            config: config.clone(),
        })
    }

    /// Profile session management operations
    async fn profile_session_management(
        &self,
        config: &ProfilingConfig,
    ) -> Result<ProfilingResult> {
        let _storage_path = self.temp_dir.path().join("session_profile");
        let session_manager = Arc::new(std::sync::Mutex::new(SessionManager::new(
            self.temp_dir.path().to_path_buf(),
        )?));

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        let timer = Timer::start("session_management");

        // Perform session operations
        for i in 0..config.operation_count {
            let session_name = format!("test_session_{}", i);
            let session_timer = Timer::start("session_operation");

            // Create session
            let _session =
                session_manager
                    .lock()
                    .unwrap()
                    .create_session(session_name, None, None)?;

            let session_duration = session_timer.stop();
            metrics.storage_metrics.writes += 1;
            metrics.storage_metrics.write_time += session_duration;
        }

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = config.operation_count;
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();
        metrics.calculate_storage_averages();

        Ok(ProfilingResult {
            scenario_name: "Session Management".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Profile batch operations
    async fn profile_batch_operations(&self, config: &ProfilingConfig) -> Result<ProfilingResult> {
        let storage_path = self.temp_dir.path().join("batch_profile.json");
        let mut store = HistoryStore::new(storage_path)?;

        let mut metrics = PerformanceMetrics::new();
        let initial_memory = get_memory_usage_mb();
        metrics.memory_usage.initial_mb = initial_memory;

        // Generate test data
        let entries = self.generate_test_entries(config.dataset_size);

        let timer = Timer::start("batch_operations");

        // Perform batch storage operations
        let batch_size = 100;
        let mut batch_count = 0;

        for chunk in entries.chunks(batch_size) {
            let batch_timer = Timer::start("batch_store");

            for entry in chunk {
                store.store_entry(entry.clone()).await?;
            }

            let batch_duration = batch_timer.stop();
            metrics.storage_metrics.writes += chunk.len();
            metrics.storage_metrics.write_time += batch_duration;
            batch_count += 1;

            if batch_count * batch_size >= config.operation_count {
                break;
            }
        }

        metrics.elapsed_time = timer.stop();
        metrics.operation_count = config.operation_count.min(entries.len());
        metrics.memory_usage.final_mb = get_memory_usage_mb();
        metrics.peak_memory_mb = metrics.memory_usage.final_mb.max(initial_memory);

        metrics.calculate_ops_per_second();
        metrics.calculate_storage_averages();

        Ok(ProfilingResult {
            scenario_name: "Batch Operations".to_string(),
            dataset_size: config.dataset_size,
            metrics,
            metadata: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Generate test history entries for profiling
    fn generate_test_entries(&self, count: usize) -> Vec<HistoryEntry> {
        let mut entries = Vec::with_capacity(count);
        let session_id = Uuid::new_v4();

        for i in 0..count {
            let entry = HistoryEntry::new(
                session_id,
                format!("test_command_{}", i % 50), // 50 different command types
                vec![format!("arg_{}", i), format!("value_{}", i % 10)],
                format!(
                    "Output for operation {}: {}",
                    i,
                    "sample output text ".repeat(i % 20)
                ),
                i % 10 != 0,             // 90% success rate
                100 + (i % 1000) as u64, // Variable duration
            )
            .with_tags(vec![
                format!("tag_{}", i % 5),
                if i % 3 == 0 {
                    "important".to_string()
                } else {
                    "normal".to_string()
                },
            ]);

            entries.push(entry);
        }

        entries
    }

    /// Generate search patterns for profiling
    fn generate_search_patterns(&self, _entries: &[HistoryEntry]) -> Vec<HistorySearch> {
        let mut patterns = Vec::new();

        // Command pattern searches
        for i in 0..10 {
            patterns.push(HistorySearch {
                command_pattern: Some(format!("test_command_{}", i)),
                limit: 50,
                ..Default::default()
            });
        }

        // Output pattern searches
        patterns.push(HistorySearch {
            output_pattern: Some("sample output".to_string()),
            limit: 100,
            ..Default::default()
        });

        // Success/failure searches
        patterns.push(HistorySearch {
            success_only: true,
            limit: 200,
            ..Default::default()
        });

        patterns.push(HistorySearch {
            failures_only: true,
            limit: 50,
            ..Default::default()
        });

        // Tag-based searches
        patterns.push(HistorySearch {
            tags: vec!["important".to_string()],
            limit: 100,
            ..Default::default()
        });

        patterns
    }

    /// Generate full-text search queries for profiling
    fn generate_fulltext_queries(&self, _entries: &[HistoryEntry]) -> Vec<FullTextSearch> {
        vec![
            FullTextSearch {
                query: "test command output".to_string(),
                fields: vec![SearchField::All],
                fuzzy: false,
                highlight: false,
            },
            FullTextSearch {
                query: "sample".to_string(),
                fields: vec![SearchField::Output],
                fuzzy: true,
                highlight: true,
            },
            FullTextSearch {
                query: "arg_".to_string(),
                fields: vec![SearchField::Args],
                fuzzy: false,
                highlight: false,
            },
            FullTextSearch {
                query: "important operation".to_string(),
                fields: vec![SearchField::Tags, SearchField::Output],
                fuzzy: true,
                highlight: true,
            },
        ]
    }

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

    /// Save all results to a directory
    pub async fn save_results(&self, output_dir: &std::path::Path) -> Result<()> {
        tokio::fs::create_dir_all(output_dir).await?;

        for (i, result) in self.results.iter().enumerate() {
            let filename = format!("profile_result_{}.json", i);
            let filepath = output_dir.join(filename);
            result.save_to_file(&filepath).await?;
        }

        // Save summary report
        let summary_path = output_dir.join("profiling_summary.txt");
        let summary = self.generate_summary_report();
        tokio::fs::write(&summary_path, summary).await?;

        Ok(())
    }

    /// Generate a summary report of all profiling results
    fn generate_summary_report(&self) -> String {
        let mut report = String::new();
        report.push_str("Profiling Summary Report\n");
        report.push_str("=======================\n\n");

        for result in &self.results {
            report.push_str(&result.generate_summary());
            report.push_str("\n\n");
        }

        // Add performance comparison
        if self.results.len() > 1 {
            report.push_str("Performance Comparison:\n");
            report.push_str("-----------------------\n");

            let mut ops_per_second: Vec<_> = self
                .results
                .iter()
                .map(|r| (&r.scenario_name, r.metrics.ops_per_second))
                .collect();
            ops_per_second
                .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

            for (name, ops) in ops_per_second {
                report.push_str(&format!("{}: {:.2} ops/sec\n", name, ops));
            }
        }

        report
    }
}

impl Default for ApplicationProfiler {
    fn default() -> Self {
        Self::new().expect("Failed to create temporary directory for profiling")
    }
}