Skip to main content

memscope_rs/query/
presets.rs

1//! Query Presets - Predefined common queries
2//!
3//! This module provides preset queries for common use cases,
4//! making it easy to get standard information without writing
5//! custom queries.
6
7use super::types::Query;
8
9/// Preset query factory
10pub struct QueryPresets;
11
12impl QueryPresets {
13    /// Create a query to find top allocations by size
14    pub fn top_allocations_by_size(limit: usize) -> Query {
15        Query::TopAllocationsBySize { limit }
16    }
17
18    /// Create a query to find top allocations by count
19    pub fn top_allocations_by_count(limit: usize) -> Query {
20        Query::TopAllocationsByCount { limit }
21    }
22
23    /// Create a query to find memory leaks
24    pub fn memory_leaks(min_age_ms: u64) -> Query {
25        Query::MemoryLeaks { min_age_ms }
26    }
27
28    /// Create a query to find large allocations
29    pub fn large_allocations(min_size: usize) -> Query {
30        Query::LargeAllocations { min_size }
31    }
32
33    /// Create a query to get thread memory statistics
34    pub fn thread_memory_stats(thread_id: u64) -> Query {
35        Query::ThreadMemoryStats { thread_id }
36    }
37
38    /// Create a query to get scope memory statistics
39    pub fn scope_memory_stats(scope_name: String) -> Query {
40        Query::ScopeMemoryStats { scope_name }
41    }
42
43    /// Create a query to get type memory statistics
44    pub fn type_memory_stats(type_name: String) -> Query {
45        Query::TypeMemoryStats { type_name }
46    }
47
48    /// Create a query to get system-wide summary
49    pub fn system_summary() -> Query {
50        Query::Summary
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_top_allocations_by_size() {
60        let query = QueryPresets::top_allocations_by_size(10);
61        assert!(matches!(query, Query::TopAllocationsBySize { limit: 10 }));
62    }
63
64    #[test]
65    fn test_memory_leaks() {
66        let query = QueryPresets::memory_leaks(1000);
67        assert!(matches!(query, Query::MemoryLeaks { min_age_ms: 1000 }));
68    }
69
70    #[test]
71    fn test_large_allocations() {
72        let query = QueryPresets::large_allocations(1024);
73        assert!(matches!(query, Query::LargeAllocations { min_size: 1024 }));
74    }
75}