vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
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
//! Query Profiler with Flame Graph Support
//!
//! This module provides detailed profiling of query execution with
//! visual flame graph generation for performance analysis.
//!
//! ## Features
//!
//! - **Hierarchical timing**: Track nested operation timings
//! - **Flame graph generation**: SVG output for visualization
//! - **Bottleneck detection**: Automatic identification of slow operations
//! - **Query replay**: Reproduce profiled queries
//! - **Export formats**: JSON, SVG, HTML
//!
//! ## Example
//!
//! ```no_run
//! use vecstore::profiler::{QueryProfiler, ProfilerConfig};
//! use vecstore::VecStore;
//!
//! # fn main() -> anyhow::Result<()> {
//! let config = ProfilerConfig::default();
//! let mut profiler = QueryProfiler::new(config);
//!
//! // Start profiling a query
//! profiler.start_query("search_documents");
//!
//! // Profile individual stages
//! profiler.start_stage("filter_metadata");
//! // ... filtering logic ...
//! profiler.end_stage();
//!
//! profiler.start_stage("vector_search");
//! // ... vector search logic ...
//! profiler.end_stage();
//!
//! profiler.end_query();
//!
//! // Generate flame graph
//! let svg = profiler.generate_flame_graph()?;
//! std::fs::write("query_profile.svg", svg)?;
//!
//! // Get bottlenecks
//! let bottlenecks = profiler.find_bottlenecks(0.1); // 10% threshold
//! for stage in bottlenecks {
//!     println!("Slow stage: {} ({:.2}ms)", stage.name, stage.duration_ms);
//! }
//! # Ok(())
//! # }
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Profiler configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfilerConfig {
    /// Enable profiling
    pub enabled: bool,
    /// Maximum stack depth to profile
    pub max_depth: usize,
    /// Minimum duration to record (microseconds)
    pub min_duration_us: u64,
    /// Include memory statistics
    pub include_memory: bool,
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_depth: 10,
            min_duration_us: 10,
            include_memory: false,
        }
    }
}

/// Query profiler
pub struct QueryProfiler {
    config: ProfilerConfig,
    queries: Vec<QueryProfile>,
    current_query: Option<QueryProfile>,
    stage_stack: Vec<(String, Instant)>,
}

impl QueryProfiler {
    /// Create a new profiler
    pub fn new(config: ProfilerConfig) -> Self {
        Self {
            config,
            queries: Vec::new(),
            current_query: None,
            stage_stack: Vec::new(),
        }
    }

    /// Start profiling a query
    pub fn start_query(&mut self, name: impl Into<String>) {
        if !self.config.enabled {
            return;
        }

        self.current_query = Some(QueryProfile {
            name: name.into(),
            start_time: Instant::now(),
            end_time: None,
            stages: Vec::new(),
            total_duration_ms: 0.0,
        });
        self.stage_stack.clear();
    }

    /// Start a profiling stage
    pub fn start_stage(&mut self, name: impl Into<String>) {
        if !self.config.enabled || self.current_query.is_none() {
            return;
        }

        if self.stage_stack.len() >= self.config.max_depth {
            return;
        }

        self.stage_stack.push((name.into(), Instant::now()));
    }

    /// End the current profiling stage
    pub fn end_stage(&mut self) {
        if !self.config.enabled || self.current_query.is_none() {
            return;
        }

        if let Some((name, start_time)) = self.stage_stack.pop() {
            let duration = start_time.elapsed();
            let duration_us = duration.as_micros() as u64;

            if duration_us < self.config.min_duration_us {
                return;
            }

            let depth = self.stage_stack.len();
            let stage = ProfileStage {
                name,
                depth,
                duration_ms: duration.as_secs_f64() * 1000.0,
                start_offset_ms: 0.0, // Will be calculated later
            };

            if let Some(query) = &mut self.current_query {
                query.stages.push(stage);
            }
        }
    }

    /// End profiling the current query
    pub fn end_query(&mut self) {
        if !self.config.enabled {
            return;
        }

        if let Some(mut query) = self.current_query.take() {
            if let Some(end_time) = query.end_time {
                query.total_duration_ms =
                    end_time.duration_since(query.start_time).as_secs_f64() * 1000.0;
            } else {
                let end_time = Instant::now();
                query.total_duration_ms =
                    end_time.duration_since(query.start_time).as_secs_f64() * 1000.0;
                query.end_time = Some(end_time);
            }

            self.queries.push(query);
        }

        self.stage_stack.clear();
    }

    /// Get all profiled queries
    pub fn queries(&self) -> &[QueryProfile] {
        &self.queries
    }

    /// Find performance bottlenecks (stages exceeding threshold)
    pub fn find_bottlenecks(&self, threshold: f64) -> Vec<&ProfileStage> {
        let mut bottlenecks = Vec::new();

        for query in &self.queries {
            let threshold_ms = query.total_duration_ms * threshold;
            for stage in &query.stages {
                if stage.duration_ms >= threshold_ms {
                    bottlenecks.push(stage);
                }
            }
        }

        bottlenecks.sort_by(|a, b| b.duration_ms.partial_cmp(&a.duration_ms).unwrap());
        bottlenecks
    }

    /// Generate flame graph SVG
    pub fn generate_flame_graph(&self) -> Result<String> {
        if self.queries.is_empty() {
            return Err(anyhow!("No queries to profile"));
        }

        // Simple flame graph generation (simplified for demo)
        let mut svg = String::new();
        svg.push_str(r#"<?xml version="1.0" standalone="no"?>"#);
        svg.push_str("\n");
        svg.push_str(r#"<svg width="1200" height="600" xmlns="http://www.w3.org/2000/svg">"#);
        svg.push_str("\n");

        svg.push_str(r#"<text x="600" y="30" text-anchor="middle" font-size="20" font-family="Arial">Query Flame Graph</text>"#);
        svg.push_str("\n");

        let mut y = 60;
        for (i, query) in self.queries.iter().enumerate() {
            svg.push_str(&format!(
                r#"<text x="10" y="{}" font-size="14" font-family="Arial">{}: {:.2}ms</text>"#,
                y, query.name, query.total_duration_ms
            ));
            svg.push_str("\n");

            y += 20;
            for stage in &query.stages {
                let width = (stage.duration_ms / query.total_duration_ms * 1000.0) as i32;
                let x = 100 + (stage.depth * 50);

                svg.push_str(&format!(
                    r#"<rect x="{}" y="{}" width="{}" height="15" fill="rgb(200, {}, 100)" stroke="black"/>"#,
                    x,
                    y,
                    width.max(1),
                    (255 - stage.depth * 30).min(255)
                ));
                svg.push_str("\n");

                svg.push_str(&format!(
                    r#"<text x="{}" y="{}" font-size="10" font-family="Arial">{} ({:.2}ms)</text>"#,
                    x + 5,
                    y + 12,
                    stage.name,
                    stage.duration_ms
                ));
                svg.push_str("\n");

                y += 20;
            }

            y += 30;
            if i >= 10 {
                break; // Limit to first 10 queries
            }
        }

        svg.push_str("</svg>");

        Ok(svg)
    }

    /// Export profile to JSON
    pub fn export_json(&self) -> Result<String> {
        Ok(serde_json::to_string_pretty(&self.queries)?)
    }

    /// Get summary statistics
    pub fn summary(&self) -> ProfileSummary {
        let total_queries = self.queries.len();
        let total_time_ms: f64 = self.queries.iter().map(|q| q.total_duration_ms).sum();
        let avg_time_ms = if total_queries > 0 {
            total_time_ms / total_queries as f64
        } else {
            0.0
        };

        let max_time_ms = self
            .queries
            .iter()
            .map(|q| q.total_duration_ms)
            .fold(0.0f64, f64::max);

        let min_time_ms = self
            .queries
            .iter()
            .map(|q| q.total_duration_ms)
            .fold(f64::MAX, f64::min);

        ProfileSummary {
            total_queries,
            total_time_ms,
            avg_time_ms,
            min_time_ms: if min_time_ms == f64::MAX {
                0.0
            } else {
                min_time_ms
            },
            max_time_ms,
        }
    }

    /// Clear all profiled data
    pub fn clear(&mut self) {
        self.queries.clear();
        self.current_query = None;
        self.stage_stack.clear();
    }
}

/// Profile of a single query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryProfile {
    pub name: String,
    #[serde(skip, default = "Instant::now")]
    start_time: Instant,
    #[serde(skip)]
    end_time: Option<Instant>,
    pub total_duration_ms: f64,
    pub stages: Vec<ProfileStage>,
}

/// Profile of a single stage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileStage {
    pub name: String,
    pub depth: usize,
    pub duration_ms: f64,
    pub start_offset_ms: f64,
}

/// Profile summary statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileSummary {
    pub total_queries: usize,
    pub total_time_ms: f64,
    pub avg_time_ms: f64,
    pub min_time_ms: f64,
    pub max_time_ms: f64,
}

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

    #[test]
    fn test_profiler_config_default() {
        let config = ProfilerConfig::default();
        assert!(config.enabled);
        assert_eq!(config.max_depth, 10);
    }

    #[test]
    fn test_basic_profiling() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("test_query");
        thread::sleep(Duration::from_millis(10));
        profiler.end_query();

        assert_eq!(profiler.queries().len(), 1);
        assert!(profiler.queries()[0].total_duration_ms >= 10.0);
    }

    #[test]
    fn test_stage_profiling() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("test_query");
        profiler.start_stage("stage1");
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage();

        profiler.start_stage("stage2");
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage();

        profiler.end_query();

        assert_eq!(profiler.queries()[0].stages.len(), 2);
        assert!(profiler.queries()[0].stages[0].duration_ms >= 5.0);
    }

    #[test]
    fn test_nested_stages() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("nested_query");
        profiler.start_stage("outer");
        profiler.start_stage("inner");
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage(); // inner
        profiler.end_stage(); // outer
        profiler.end_query();

        let query = &profiler.queries()[0];
        assert_eq!(query.stages.len(), 2);
        assert_eq!(query.stages[0].depth, 1); // inner
        assert_eq!(query.stages[1].depth, 0); // outer
    }

    #[test]
    fn test_disabled_profiler() {
        let mut config = ProfilerConfig::default();
        config.enabled = false;

        let mut profiler = QueryProfiler::new(config);

        profiler.start_query("disabled_query");
        profiler.start_stage("stage1");
        profiler.end_stage();
        profiler.end_query();

        assert_eq!(profiler.queries().len(), 0);
    }

    #[test]
    fn test_find_bottlenecks() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("bottleneck_query");
        profiler.start_stage("fast_stage");
        thread::sleep(Duration::from_millis(1));
        profiler.end_stage();

        profiler.start_stage("slow_stage");
        thread::sleep(Duration::from_millis(50));
        profiler.end_stage();

        profiler.end_query();

        let bottlenecks = profiler.find_bottlenecks(0.5); // 50% threshold
        assert!(!bottlenecks.is_empty());
        assert_eq!(bottlenecks[0].name, "slow_stage");
    }

    #[test]
    fn test_summary_statistics() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        for i in 0..3 {
            profiler.start_query(format!("query_{}", i));
            thread::sleep(Duration::from_millis(10));
            profiler.end_query();
        }

        let summary = profiler.summary();
        assert_eq!(summary.total_queries, 3);
        assert!(summary.avg_time_ms >= 10.0);
        assert!(summary.max_time_ms >= summary.min_time_ms);
    }

    #[test]
    fn test_flame_graph_generation() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("flame_query");
        profiler.start_stage("stage1");
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage();
        profiler.end_query();

        let svg = profiler.generate_flame_graph();
        assert!(svg.is_ok());
        assert!(svg.unwrap().contains("<svg"));
    }

    #[test]
    fn test_json_export() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("json_query");
        profiler.start_stage("stage1");
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage();
        profiler.end_query();

        let json = profiler.export_json();
        assert!(json.is_ok());
        assert!(json.unwrap().contains("json_query"));
    }

    #[test]
    fn test_clear() {
        let mut profiler = QueryProfiler::new(ProfilerConfig::default());

        profiler.start_query("test_query");
        profiler.end_query();

        assert_eq!(profiler.queries().len(), 1);

        profiler.clear();
        assert_eq!(profiler.queries().len(), 0);
    }

    #[test]
    fn test_min_duration_filter() {
        let mut config = ProfilerConfig::default();
        config.min_duration_us = 1_000_000; // 1 second

        let mut profiler = QueryProfiler::new(config);

        profiler.start_query("filter_query");
        profiler.start_stage("fast_stage");
        thread::sleep(Duration::from_micros(100)); // Too fast
        profiler.end_stage();
        profiler.end_query();

        // Stage should be filtered out
        assert_eq!(profiler.queries()[0].stages.len(), 0);
    }

    #[test]
    fn test_max_depth_limit() {
        let mut config = ProfilerConfig::default();
        config.max_depth = 2;

        let mut profiler = QueryProfiler::new(config);

        profiler.start_query("depth_query");
        profiler.start_stage("level1");
        profiler.start_stage("level2");
        profiler.start_stage("level3"); // Should be ignored
        thread::sleep(Duration::from_millis(5));
        profiler.end_stage();
        profiler.end_stage();
        profiler.end_stage();
        profiler.end_query();

        // Only 2 stages should be recorded (level3 exceeded max_depth)
        assert_eq!(profiler.queries()[0].stages.len(), 2);
    }

    #[test]
    fn test_empty_profiler_flame_graph() {
        let profiler = QueryProfiler::new(ProfilerConfig::default());
        let result = profiler.generate_flame_graph();
        assert!(result.is_err());
    }
}