rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
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
//! 缓存性能对比示例
//!
//! 本示例对比启用缓存和未启用缓存的数据库操作性能差异
//! 使用 SQLite 数据库进行测试,并正确配置L1和L2缓存

use rat_logger::{LoggerBuilder, debug, handler::term::TermConfig};
use rat_quickdb::manager::shutdown;
use rat_quickdb::types::*;
use rat_quickdb::*;
use rat_quickdb::{ModelOperations, datetime_field, float_field, integer_field, string_field};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use tokio::time::sleep;

// 定义缓存数据库用户模型
define_model! {
    /// 用户模型(缓存版本)
    struct CachedUser {
        id: String,
        name: String,
        email: String,
        age: i32,
        created_at: chrono::DateTime<chrono::Utc>,
    }
    collection = "users",
    database = "cached_db",
    fields = {
        id: string_field(None, None, None).required().unique(),
        name: string_field(Some(100), Some(1), None).required(),
        email: string_field(Some(255), Some(1), None).required(),
        age: integer_field(Some(0), Some(150)).required(),
        created_at: datetime_field().required(),
    }
    indexes = [
        { fields: ["name"], unique: false, name: "idx_name" },
        { fields: ["age"], unique: false, name: "idx_age" },
        { fields: ["email"], unique: true, name: "idx_email" },
        { fields: ["created_at"], unique: false, name: "idx_created_at" },
    ],
}

// 定义非缓存数据库用户模型

/// 性能测试结果
#[derive(Debug, Clone)]
struct PerformanceResult {
    operation: String,
    with_cache: Duration,
    without_cache: Duration,
    improvement_ratio: f64,
    cache_hit_rate: Option<f64>,
}

impl PerformanceResult {
    fn new(operation: String, with_cache: Duration, without_cache: Duration) -> Self {
        let improvement_ratio = if with_cache.as_millis() > 0 {
            without_cache.as_millis() as f64 / with_cache.as_millis() as f64
        } else {
            1.0
        };

        Self {
            operation,
            with_cache,
            without_cache,
            improvement_ratio,
            cache_hit_rate: None,
        }
    }

    fn with_cache_hit_rate(mut self, hit_rate: f64) -> Self {
        self.cache_hit_rate = Some(hit_rate);
        self
    }
}

/// SQLite缓存绕过性能测试
struct CacheBypassTest {
    /// 测试结果
    results: Vec<PerformanceResult>,
}

impl CacheBypassTest {
    /// 初始化测试环境
    async fn new() -> QuickDbResult<Self> {
        println!("🚀 初始化缓存性能对比测试环境...");

        // 初始化日志系统
        LoggerBuilder::new()
            .add_terminal_with_config(TermConfig::default())
            .init()
            .expect("日志初始化失败");

        rat_quickdb::init();

        // 创建带缓存的数据库配置(L1 + L2)
        let cached_config = Self::create_cached_database_config();

        // 添加数据库配置
        add_database(cached_config).await?;

        // 设置默认数据库别名为缓存数据库
        set_default_alias("cached_db").await?;

        println!("✅ 测试环境初始化完成");

        Ok(Self {
            results: Vec::new(),
        })
    }

    /// 创建带缓存的数据库配置(L1 + L2)
    fn create_cached_database_config() -> DatabaseConfig {
        // L1缓存配置(内存缓存)
        let l1_config = L1CacheConfig {
            max_capacity: 1000, // 最大1000个条目
            max_memory_mb: 64,  // 64MB内存限制
            enable_stats: true, // 启用统计
        };

        // L2缓存配置(磁盘缓存)
        let l2_config = Some(
            L2CacheConfig::new("./test_cache".to_string())
                .with_max_disk_mb(512) // 512MB磁盘缓存
                .with_compression_level(3) // ZSTD压缩级别
                .enable_wal(true) // 启用WAL模式
                .clear_on_startup(true), // 启动时清理缓存
        );

        // TTL配置
        let ttl_config = TtlConfig {
            default_ttl_secs: 1800,   // 默认30分钟
            max_ttl_secs: 7200,       // 最大2小时
            check_interval_secs: 120, // 检查间隔2分钟
        };

        // 压缩配置
        let compression_config = CompressionConfig {
            enabled: true,
            algorithm: CompressionAlgorithm::Zstd,
            threshold_bytes: 1024, // 1KB以上才压缩
        };

        // 完整的缓存配置
        let cache_config = CacheConfig {
            enabled: true,
            strategy: CacheStrategy::Lru,
            l1_config,
            l2_config,
            ttl_config,
            compression_config,
            version: "v1".to_string(),
        };

        DatabaseConfig {
            db_type: DatabaseType::SQLite,
            connection: ConnectionConfig::SQLite {
                path: "./test_data/cache_performance_cached.db".to_string(),
                create_if_missing: true,
            },
            pool: PoolConfig::builder()
                .max_connections(10)
                .min_connections(1)
                .connection_timeout(10)
                .idle_timeout(300)
                .max_lifetime(1800)
                .max_retries(3)
                .retry_interval_ms(1000)
                .keepalive_interval_sec(60)
                .health_check_timeout_sec(10)
                .build()
                .unwrap(),
            alias: "cached_db".to_string(),
            cache: Some(cache_config),
            id_strategy: IdStrategy::Uuid,
        }
    }

    
    /// 运行所有性能测试
    async fn run_all_tests(&mut self) -> QuickDbResult<()> {
        // 1. 设置测试数据
        self.setup_test_data().await?;

        // 2. 预热缓存
        self.warmup_cache().await?;

        // 3. 运行各项测试
        self.test_query_operations().await?;
        self.test_repeated_queries().await?;
        self.test_batch_queries().await?;
        self.test_update_operations().await?;

        Ok(())
    }

    /// 设置测试数据
    async fn setup_test_data(&mut self) -> QuickDbResult<()> {
        println!("\n🔧 设置测试数据...");

        // 清理可能存在的测试数据
        println!("  清理可能存在的测试数据...");
        let _ = drop_table("cached_db", "users").await;

        // 创建测试用户数据
        let users = vec![
            self.create_cached_user("user1", "张三", "zhangsan@example.com", 25),
            self.create_cached_user("user2", "李四", "lisi@example.com", 30),
            self.create_cached_user("user3", "王五", "wangwu@example.com", 28),
            self.create_cached_user("user4", "赵六", "zhaoliu@example.com", 35),
            self.create_cached_user("user5", "钱七", "qianqi@example.com", 22),
        ];

        // 批量用户数据
        let batch_users: Vec<CachedUser> = (6..=25)
            .map(|i| {
                self.create_cached_user(
                    &format!("batch_user_{}", i),
                    &format!("批量用户{}", i),
                    &format!("batch{}@example.com", i),
                    20 + (i % 30),
                )
            })
            .collect();

        // 创建测试数据到数据库
        println!("  创建测试数据到数据库...");
        for user in users.iter().chain(batch_users.iter()) {
            let mut user_clone = user.clone();
            user_clone.save().await?;
        }

        println!(
            "  ✅ 创建了 {} 条测试记录",
            users.len() + batch_users.len()
        );
        Ok(())
    }

    /// 创建缓存用户数据
    fn create_cached_user(&self, id: &str, name: &str, email: &str, age: i32) -> CachedUser {
        CachedUser {
            id: id.to_string(),
            name: name.to_string(),
            email: email.to_string(),
            age,
            created_at: chrono::Utc::now(),
        }
    }

    
    /// 缓存预热
    async fn warmup_cache(&mut self) -> QuickDbResult<()> {
        println!("\n🔥 缓存预热...");

        // 设置使用缓存数据库
        set_default_alias("cached_db").await?;

        // 执行一些查询操作来预热缓存
        let conditions = vec![QueryCondition {
            field: "age".to_string(),
            operator: QueryOperator::Gt,
            value: DataValue::Int(20),
        }];

        // 预热查询
        let _result = ModelManager::<CachedUser>::find(conditions, None).await?;

        // 按ID查询预热
        let _result = ModelManager::<CachedUser>::find_by_id("user1").await?;
        let _result = ModelManager::<CachedUser>::find_by_id("user2").await?;

        println!("  ✅ 缓存预热完成");
        Ok(())
    }

    /// 测试查询操作性能
    async fn test_query_operations(&mut self) -> QuickDbResult<()> {
        println!("\n🔍 测试查询操作性能...");

        let conditions = vec![QueryCondition {
            field: "name".to_string(),
            operator: QueryOperator::Eq,
            value: DataValue::String("张三".to_string()),
        }];

        // 第一次查询(冷启动,从数据库读取)
        set_default_alias("cached_db").await?;
        let start = Instant::now();
        let _result1 = ModelManager::<CachedUser>::find(conditions.clone(), None).await?;
        let first_query_duration = start.elapsed();

        // 第二次查询(缓存命中)
        let start = Instant::now();
        let _result2 = ModelManager::<CachedUser>::find(conditions, None).await?;
        let cached_duration = start.elapsed();

        let result = PerformanceResult::new(
            "单次查询操作".to_string(),
            cached_duration,
            first_query_duration,
        );

        println!("  ✅ 首次查询(数据库): {:?}", first_query_duration);
        println!("  ✅ 缓存查询: {:?}", cached_duration);
        println!("  📈 性能提升: {:.2}x", result.improvement_ratio);

        self.results.push(result);
        Ok(())
    }

    /// 测试重复查询(bypass_cache vs 缓存)
    async fn test_repeated_queries(&mut self) -> QuickDbResult<()> {
        println!("\n🔄 测试重复查询性能(bypass_cache vs 缓存)...");

        let conditions = vec![QueryCondition {
            field: "age".to_string(),
            operator: QueryOperator::Gt,
            value: DataValue::Int(20),
        }];

        let query_count = 10;

        // 测量强制跳过缓存的查询时间
        set_default_alias("cached_db").await?;
        let start = Instant::now();
        for _ in 0..query_count {
            let _result = ModelManager::<CachedUser>::find_with_cache_control(conditions.clone(), None, true).await?;
            // 短暂延迟以模拟真实场景
            sleep(Duration::from_millis(5)).await;
        }
        let non_cached_duration = start.elapsed();

        // 首次查询(建立缓存)
        set_default_alias("cached_db").await?;
        let _result = ModelManager::<CachedUser>::find(conditions.clone(), None).await?;

        // 测试重复查询(应该从缓存读取)
        let start = Instant::now();
        for _ in 0..query_count {
            let _result = ModelManager::<CachedUser>::find(conditions.clone(), None).await?;
            // 短暂延迟以模拟真实场景
            sleep(Duration::from_millis(5)).await;
        }
        let cached_duration = start.elapsed();

        // 计算平均单次查询时间
        let avg_cached_time = cached_duration / query_count;
        let avg_non_cached_time = non_cached_duration / query_count;

        let result = PerformanceResult::new(
            format!("重复查询 ({}次)", query_count),
            avg_cached_time,
            avg_non_cached_time,
        )
        .with_cache_hit_rate(95.0); // 假设95%的缓存命中率

        println!("  ✅ 强制跳过缓存总耗时: {:?}", non_cached_duration);
        println!("  ✅ 使用缓存总耗时: {:?}", cached_duration);
        println!("  ✅ 强制跳过缓存平均查询: {:?}", avg_non_cached_time);
        println!("  ✅ 使用缓存平均查询: {:?}", avg_cached_time);
        println!("  📈 性能提升: {:.2}x", result.improvement_ratio);
        println!(
            "  🎯 缓存命中率: {:.1}%",
            result.cache_hit_rate.unwrap_or(0.0)
        );

        self.results.push(result);
        Ok(())
    }

    /// 测试批量查询性能
    async fn test_batch_queries(&mut self) -> QuickDbResult<()> {
        println!("\n📦 测试批量查询性能...");

        let user_ids = vec!["user1", "user2", "user3", "user4", "user5"];

        // 首次批量查询(建立缓存)
        set_default_alias("cached_db").await?;
        let start = Instant::now();
        for user_id in &user_ids {
            let _result = ModelManager::<CachedUser>::find_by_id(user_id).await?;
        }
        let first_batch_duration = start.elapsed();

        // 第二次批量查询(缓存命中)
        let start = Instant::now();
        for user_id in &user_ids {
            let _result = ModelManager::<CachedUser>::find_by_id(user_id).await?;
        }
        let cached_duration = start.elapsed();

        let result = PerformanceResult::new(
            format!("批量ID查询 ({}条记录)", user_ids.len()),
            cached_duration,
            first_batch_duration,
        );

        println!("  ✅ 首次批量查询: {:?}", first_batch_duration);
        println!("  ✅ 缓存批量查询: {:?}", cached_duration);
        println!("  📈 性能提升: {:.2}x", result.improvement_ratio);

        self.results.push(result);
        Ok(())
    }

    /// 测试更新操作性能
    async fn test_update_operations(&mut self) -> QuickDbResult<()> {
        println!("\n✏️ 测试更新操作性能...");

        let conditions = vec![QueryCondition {
            field: "name".to_string(),
            operator: QueryOperator::Eq,
            value: DataValue::String("张三".to_string()),
        }];

        // 查找要更新的用户
        set_default_alias("cached_db").await?;
        let users = ModelManager::<CachedUser>::find(conditions.clone(), None).await?;
        if let Some(user) = users.first() {
            // 第一次更新操作
            let start = Instant::now();
            let mut user_clone = user.clone();
            user_clone.age = 26;
            user_clone.email = "zhangsan_new@example.com".to_string();
            let mut updates = HashMap::new();
            updates.insert("age".to_string(), DataValue::Int(26));
            updates.insert(
                "email".to_string(),
                DataValue::String("zhangsan_new@example.com".to_string()),
            );
            let _update_result = user_clone.update(updates).await?;
            let first_update_duration = start.elapsed();

            // 恢复数据以便第二次更新
            let mut user_restore = user_clone.clone();
            user_restore.age = 25;
            user_restore.email = "zhangsan@example.com".to_string();
            let mut restore_updates = HashMap::new();
            restore_updates.insert("age".to_string(), DataValue::Int(25));
            restore_updates.insert(
                "email".to_string(),
                DataValue::String("zhangsan@example.com".to_string()),
            );
            let _restore_result = user_restore.update(restore_updates).await?;

            // 第二次更新操作(可能有缓存优化)
            let start = Instant::now();
            let mut user_update2 = user.clone();
            user_update2.age = 26;
            user_update2.email = "zhangsan_new@example.com".to_string();
            let mut updates2 = HashMap::new();
            updates2.insert("age".to_string(), DataValue::Int(26));
            updates2.insert(
                "email".to_string(),
                DataValue::String("zhangsan_new@example.com".to_string()),
            );
            let _update_result2 = user_update2.update(updates2).await?;
            let second_update_duration = start.elapsed();

            let result = PerformanceResult::new(
                "更新操作".to_string(),
                second_update_duration,
                first_update_duration,
            );

            println!("  ✅ 首次更新: {:?}", first_update_duration);
            println!("  ✅ 第二次更新: {:?}", second_update_duration);
            println!("  📈 性能变化: {:.2}x", result.improvement_ratio);

            self.results.push(result);
        } else {
            println!("  ⚠️ 未找到测试用户,跳过更新测试");
        }

        Ok(())
    }

    /// 显示测试结果汇总
    fn display_results(&self) {
        println!("\n📊 ==================== SQLite缓存绕过测试结果汇总 ====================");
        println!(
            "{:<25} {:<15} {:<20} {:<10} {:<10}",
            "操作类型", "使用缓存(ms)", "强制跳过缓存(ms)", "提升倍数", "缓存命中率"
        );
        println!("{}", "-".repeat(80));

        let mut total_improvement = 0.0;
        let mut count = 0;

        for result in &self.results {
            let cache_hit_str = if let Some(hit_rate) = result.cache_hit_rate {
                format!("{:.1}%", hit_rate)
            } else {
                "N/A".to_string()
            };

            println!(
                "{:<25} {:<15.3} {:<20.3} {:<10.2} {:<10}",
                result.operation,
                result.with_cache.as_millis(),
                result.without_cache.as_millis(),
                result.improvement_ratio,
                cache_hit_str
            );

            total_improvement += result.improvement_ratio;
            count += 1;
        }

        println!("{}", "-".repeat(80));

        if count > 0 {
            let avg_improvement = total_improvement / count as f64;
            println!("📈 平均性能提升: {:.2}x", avg_improvement);

            if avg_improvement > 1.5 {
                println!("🎉 缓存显著提升了数据库操作性能!");
            } else if avg_improvement > 1.1 {
                println!("✅ 缓存适度提升了数据库操作性能。");
            } else {
                println!("⚠️ 缓存对性能提升有限,可能需要调整缓存策略。");
            }
        }

        println!("\n💡 性能优化建议:");
        println!("   • 对于频繁查询的数据,缓存能显著提升性能");
        println!("   • 重复查询场景下,缓存命中率越高,性能提升越明显");
        println!("   • 写操作(创建、更新)的性能提升相对有限");
        println!("   • 可根据实际业务场景调整缓存 TTL 和容量配置");

        println!("\n🔧 缓存配置信息:");
        println!("   • 缓存策略: LRU");
        println!("   • L1 缓存容量: 1000 条记录");
        println!("   • L1 缓存内存限制: 64 MB");
        println!("   • L2 缓存容量: 512 MB 磁盘空间");
        println!("   • L2 缓存目录: ./test_cache");
        println!("   • 默认 TTL: 30 分钟");
        println!("   • 最大 TTL: 2 小时");
        println!("   • 压缩算法: ZSTD");
    }
}

/// 清理测试文件
async fn cleanup_test_files() {
    // 清理测试数据库文件
    let test_files = [
        "./test_data/cache_performance_cached.db",
        "./test_data/cache_performance_non_cached.db",
    ];

    for file_path in &test_files {
        if std::path::Path::new(file_path).exists() {
            if let Err(e) = tokio::fs::remove_file(file_path).await {
                eprintln!("⚠️  清理文件 {} 失败: {}", file_path, e);
            } else {
                println!("🗑️  已清理文件: {}", file_path);
            }
        }
    }

    // 清理缓存目录
    let cache_dir = "./test_cache";
    if std::path::Path::new(cache_dir).exists() {
        if let Err(e) = tokio::fs::remove_dir_all(cache_dir).await {
            eprintln!("⚠️  清理缓存目录 {} 失败: {}", cache_dir, e);
        } else {
            println!("🗑️  已清理缓存目录: {}", cache_dir);
        }
    }

    // 尝试清理测试目录(如果为空)
    if let Err(_) = tokio::fs::remove_dir("./test_data").await {
        // 目录不为空或不存在,忽略错误
    }

    println!("🧹 清理测试文件完成");
}

#[tokio::main]
async fn main() -> QuickDbResult<()> {
    println!("🚀 RatQuickDB SQLite缓存绕过测试");
    println!("=====================================\n");

    // 清理之前的测试文件
    cleanup_test_files().await;

    // 创建并运行测试
    let mut test = CacheBypassTest::new().await?;
    test.run_all_tests().await?;

    // 显示测试结果
    test.display_results();

    // 清理测试文件
    cleanup_test_files().await;

    // 关闭连接池
    shutdown().await?;

    println!("\n🎯 测试完成!感谢使用 RatQuickDB SQLite缓存绕过功能。");

    Ok(())
}