测试

MynaDB 采用全面的多层测试策略,灵感来自 InfluxDB、TimescaleDB、ClickHouse 和 QuestDB 等业界领先的时序数据库。这确保了数据完整性、查询正确性和性能可靠性。

🛡 生产就绪的测试

MynaDB 包含 200+ 测试,涵盖单元测试、集成测试、基于属性的测试、模糊测试和性能基准测试。

测试概览

测试类别 框架 覆盖范围 用途
单元测试 Rust 内置 146+ 测试 核心模块功能
正确性测试 Rust 测试 50+ 边界情况 查询结果准确性
属性测试 proptest 20+ 属性 不变式验证
模糊测试 cargo-fuzz / libfuzzer 3 个目标 输入健壮性
基准测试 Criterion 50+ 基准 性能回归检测
集成测试 Bash 脚本 22 个脚本 端到端验证

单元测试

单元测试验证各个模块的功能。MynaDB 有 146+ 单元测试覆盖核心组件:

存储层

  • Line Protocol 解析
  • Parquet 读/写
  • WAL 操作
  • 数据缓存

查询引擎

  • 时间桶函数
  • first_value / last_value
  • 填充操作
  • 查询缓存

后台任务

  • 压缩合并策略
  • 数据保留策略
  • 元数据管理
  • 文件操作
bash
# Run all unit tests
cargo test

# Run tests for a specific module
cargo test --lib cache
cargo test --lib compactor
cargo test --lib wal
cargo test --lib line_protocol

# Run tests with output
cargo test -- --nocapture

正确性测试

正确性测试验证查询结果与预期值一致。这些测试覆盖聚合、边界情况和数据完整性:

聚合准确性

测试验证 SQL 聚合返回数学上正确的结果:

边界情况

类别 测试用例
浮点数 零值、负数、科学计数法、精度限制、NaN/Infinity 处理
整数 大值(i64 最大值)、负整数、零
字符串 空字符串、Unicode、特殊字符、引号、转义序列
时间戳 Epoch 边界、遥远未来日期、毫秒/纳秒精度
标签 多标签(10+)、数值、连字符、下划线
查询 空结果、单行、LIMIT/OFFSET、ORDER BY、GROUP BY 与 HAVING
bash
# Run correctness tests
cargo test --test correctness

# Run specific correctness test
cargo test --test correctness test_floating_point_precision

基于属性的测试

基于属性的测试使用 proptest 生成随机输入并验证不变式在所有情况下都成立。

解析器属性

rust
proptest! {
    // Line protocol roundtrip: parse → serialize → parse = same result
    #[test]
    fn line_protocol_roundtrip(
        measurement in "[a-z]+",
        tag_key in "[a-z]+",
        tag_value in "[a-z0-9]+",
        field_key in "[a-z]+",
        field_value in any::<f64>().prop_filter("finite", |v| v.is_finite()),
    ) {
        let line = format!("{},{}={} {}={}",
            measurement, tag_key, tag_value, field_key, field_value);
        let parsed = parse_line_protocol(&line)?;
        // Verify parsed values match inputs
    }

    // Tag order independence
    #[test]
    fn tag_order_independent(tags in prop::collection::vec("[a-z]+", 2..5)) {
        // Tags in any order should produce equivalent results
    }
}

查询属性

bash
# Run property tests
cargo test --test property

# Run with more test cases (slower but more thorough)
PROPTEST_CASES=1000 cargo test --test property

模糊测试

模糊测试使用 cargo-fuzz 和 libFuzzer 来发现解析器中的边界情况和潜在崩溃。

模糊测试目标

目标 说明 测试的输入
fuzz_line_protocol InfluxDB Line Protocol 解析器 格式错误的行、特殊字符、边界情况
fuzz_time_interval 持续时间字符串解析器(1h、5m、30s) 无效格式、边界值、溢出
fuzz_field_value 字段值解析器(浮点数、整数、字符串、布尔值) 类型混淆、转义序列、引号
bash
# Install cargo-fuzz (requires nightly)
cargo +nightly install cargo-fuzz

# Run fuzz tests (runs indefinitely, Ctrl+C to stop)
cargo +nightly fuzz run fuzz_line_protocol

# Run for a specific duration
cargo +nightly fuzz run fuzz_line_protocol -- -max_total_time=60

# List all fuzz targets
cargo +nightly fuzz list
💡 模糊测试最佳实践

长时间运行模糊测试(数小时或过夜)以发现罕见的边界情况。崩溃会自动保存到 fuzz/artifacts/ 以便复现。

性能基准测试

性能基准测试使用 Criterion 进行统计分析和回归检测。

基准测试类别

📝

Line Protocol

  • 单行解析
  • 批量解析(100-10000 行)
  • 复杂行(多标签/字段)
  • 并行解析吞吐量
  • 错误路径性能
🔎

查询执行

  • 简单 SELECT 查询
  • 聚合(SUM、AVG、COUNT)
  • 过滤谓词
  • GROUP BY 操作
  • 时间范围查询
🕓

时间函数

  • TimeInterval 解析
  • time_bucket 计算
  • 填充选项解析
  • 边界条件
  • 大数据集处理

边界情况基准

基准测试包含边界情况,以确保所有输入下的一致性能:

bash
# Run all benchmarks
cargo bench

# Run specific benchmark suite
cargo bench --bench line_protocol_bench
cargo bench --bench query_bench
cargo bench --bench time_functions_bench

# Generate HTML report
cargo bench -- --save-baseline main
# Open target/criterion/report/index.html

基准测试输出

Criterion 提供详细的统计分析:

line_protocol/parse_single_line
                        time:   [1.2341 µs 1.2456 µs 1.2589 µs]
                        change: [-2.1234% -0.8765% +0.4321%] (p = 0.19 > 0.05)
                        No change in performance detected.

line_protocol/batch_1000_lines
                        time:   [1.1234 ms 1.1456 ms 1.1789 ms]
                        thrpt:  [848.23 Klines/s 872.89 Klines/s 890.12 Klines/s]

集成测试

集成测试使用 bash 脚本与运行中的 MynaDB 实例交互,验证端到端功能。

测试脚本

脚本 类别 说明
test_write.sh 写入路径 Catalog.schema.table 结构
test_schemas.sh Schema Schema 和表列表 API
test_partitions.sh 分区 分区管理
test_compaction.sh 压缩合并 基本分层压缩合并
test_compaction_multilevel.sh 压缩合并 多级 L0→L1→L2
test_auth.sh 认证 基本认证
test_mysql.sh 协议 MySQL 线协议
test_group_by_time.sh 查询 时间桶聚合
test_comprehensive.sh 端到端 完整集成测试
bash
# Run integration tests
cd scripts

# Run individual test
./test_write.sh
./test_compaction.sh

# Run comprehensive test suite
./test_comprehensive.sh

混沌测试

混沌测试模拟故障场景以验证数据持久性和恢复能力:

bash
# Run chaos tests
./scripts/test_chaos.sh

# Test WAL recovery
./scripts/test_wal_recovery.sh

运行所有测试

bash
# Quick validation (unit + correctness + property tests)
cargo test

# Full test suite
cargo test --all-targets

# Include benchmarks (compile only, don't run)
cargo test --all-targets --benches

# Run benchmarks
cargo bench

# Fuzz testing (requires nightly)
cargo +nightly fuzz run fuzz_line_protocol -- -max_total_time=60

# Integration tests
./scripts/test_comprehensive.sh

阶段 A:数据完整性测试

数据完整性测试验证 MynaDB 在整个数据生命周期中正确处理数据:写入、刷新、压缩合并、恢复和查询。这些测试确保在所有条件下实现零数据丢失和精确值保留。

🔒 29 项数据完整性测试

全面覆盖写入/读取往返、WAL 恢复、压缩合并保持和数据一致性。

写入/读取往返测试(11 项测试)

这些测试验证写入 MynaDB 的数据可以以精确精度读回:

测试 说明 验证方式
test_roundtrip_basic_float 浮点字段值(23.456、19.876 等) SUM 与预期值匹配,容差 <0.001
test_roundtrip_integer_fields 整数字段值(100i、250i、75i) 精确 SUM 匹配
test_roundtrip_boolean_fields 布尔字段值(true/false) true 值计数保留
test_roundtrip_string_fields 带引号的字符串字段值 行数保留
test_roundtrip_mixed_types 单次写入中的浮点 + 整数 + 布尔 + 字符串 所有字段类型保留
test_roundtrip_large_batch 单次写入 1000 行 COUNT=1000,SUM 匹配
test_roundtrip_special_characters 带 /api/v1、连字符、下划线的标签 所有行可查询
test_roundtrip_many_tags 每个数据点 7 个标签 所有标签保留
test_roundtrip_many_fields 每个数据点 10 个字段 所有字段值保留
test_roundtrip_precision_floats 科学计数法(1e-15、1e15) 极端值保留
test_roundtrip_timestamp_preservation 显式纳秒时间戳 时间排序保留

WAL 恢复测试(4 项测试)

WAL 恢复测试验证崩溃后未刷新的数据永不丢失。这些测试模拟崩溃并验证完整的数据恢复。

测试 场景 验证方式
test_wal_recovery_basic 写入 3 行 → 崩溃 → 重启 所有 3 行以精确值恢复
test_wal_recovery_multiple_writes 5 次独立写入操作 → 崩溃 所有 5 次写入恢复
test_wal_recovery_after_partial_flush 刷新批次 1 → 写入批次 2 → 崩溃 已刷新和未刷新的数据都存在
test_wal_recovery_large_batch 写入 500 行 → 崩溃 → 重启 COUNT=500,SUM 精确匹配

压缩合并保持测试(5 项测试)

压缩合并测试验证 LSM-tree 压缩合并过程永不丢失或损坏数据。数据通过多次刷新写入,然后进行压缩合并,在前后进行验证。

测试 场景 验证方式
test_compaction_preserves_count 4 批次 x 100 行 → 压缩合并 压缩前后 COUNT=400
test_compaction_preserves_values 3 批次中的已知值 SUM、MIN、MAX 不变
test_compaction_preserves_all_field_types 浮点、整数、布尔、字符串字段 压缩合并后所有类型保留
test_compaction_preserves_tags 3 主机 x 3 区域 x 3 批次 标签过滤查询返回相同结果
test_compaction_multilevel 10 批次触发 L0→L1→L2 所有级别中 COUNT=500,SUM 不变

运行数据完整性测试

bash
# Run all data integrity tests (requires sequential execution)
cargo test --test data_integrity -- --test-threads=1

# Run only roundtrip tests
cargo test --test data_integrity test_roundtrip -- --test-threads=1

# Run only WAL recovery tests
cargo test --test data_integrity test_wal -- --test-threads=1

# Run only compaction tests
cargo test --test data_integrity test_compaction -- --test-threads=1

# Run with verbose output
cargo test --test data_integrity -- --test-threads=1 --nocapture
💡 重要:顺序执行

数据完整性测试必须使用 --test-threads=1 运行,因为每个测试会启动自己的 MynaDB 服务器实例。并行执行会导致端口冲突。

阶段 B:并发与压力测试

并发测试验证 MynaDB 在并发负载下维持数据一致性。这些测试模拟真实场景,包括多个写入者、读取者和后台操作同时进行。

⚡ 13 项并发测试

多线程写入、操作期间的读取一致性,以及 8000+ 写入/秒的压力测试。

并发写入测试(4 项测试)

这些测试验证多线程同时写入时不会丢失任何写入:

测试 场景 验证方式
test_concurrent_writes_single_table 4 线程 x 100 次写入同一表 COUNT=400,无错误
test_concurrent_writes_multiple_tables 4 线程 x 50 次写入不同表 每个表恰好有 50 行
test_concurrent_writes_no_duplicates 4 线程使用唯一 ID COUNT(DISTINCT id) = 总写入数
test_concurrent_writes_with_flush 3 个写入者 + 1 个刷新者并发运行 刷新期间无写入丢失

读取一致性测试(3 项测试)

这些测试验证后台操作期间读取永不看到不一致或损坏的数据:

测试 场景 验证方式
test_read_during_write 写入 100 行期间持续读取 计数永不低于初始值
test_read_during_compaction L0→L1 压缩合并期间查询 全程计数保持不变
test_query_isolation 批量写入期间的聚合查询 无查询失败或结果损坏

压力测试(3 项测试)

这些测试将 MynaDB 推向极限,验证在极端负载下的行为:

测试 场景 性能
test_stress_high_throughput_writes 8 线程 x 250 次写入 8000+ 写入/秒
test_stress_mixed_read_write 2 个写入者 + 4 个读取者 无不一致读取
test_stress_burst_writes 5 次突发,每次 200 次写入 所有突发保留

数据一致性测试(3 项测试)

这些测试验证并发修改期间的聚合一致性:

测试 场景 验证方式
test_consistency_aggregation_during_writes 插入期间的 SUM 查询 总和永不减少
test_consistency_count_monotonic 插入期间的 COUNT 查询 计数单调非递减
test_no_data_loss_under_load 6 线程 + 定期刷新 每个已写入的 ID 都出现在最终结果中

运行并发测试

bash
# Run all concurrency tests (requires sequential execution)
cargo test --test concurrency -- --test-threads=1

# Run only concurrent write tests
cargo test --test concurrency test_concurrent -- --test-threads=1

# Run only stress tests
cargo test --test concurrency test_stress -- --test-threads=1

# Run only consistency tests
cargo test --test concurrency test_consistency -- --test-threads=1

# Run with verbose output to see throughput metrics
cargo test --test concurrency test_stress_high_throughput -- --test-threads=1 --nocapture

快速参考:运行测试

bash
# ═══════════════════════════════════════════════════════════════════
# COMPLETE TEST SUITE
# ═══════════════════════════════════════════════════════════════════

# Run all unit tests (fast, ~10 seconds)
cargo test

# Run all integration tests
cargo test --test data_integrity -- --test-threads=1
cargo test --test concurrency -- --test-threads=1
cargo test --test correctness -- --test-threads=1
cargo test --test property

# ═══════════════════════════════════════════════════════════════════
# PHASE A: DATA INTEGRITY
# ═══════════════════════════════════════════════════════════════════

# All data integrity tests (29 tests, ~4 minutes)
cargo test --test data_integrity -- --test-threads=1

# Roundtrip tests only (11 tests)
cargo test --test data_integrity test_roundtrip -- --test-threads=1

# WAL recovery tests only (4 tests)
cargo test --test data_integrity test_wal -- --test-threads=1

# Compaction tests only (5 tests)
cargo test --test data_integrity test_compaction -- --test-threads=1

# ═══════════════════════════════════════════════════════════════════
# PHASE B: CONCURRENCY & STRESS
# ═══════════════════════════════════════════════════════════════════

# All concurrency tests (13 tests, ~2 minutes)
cargo test --test concurrency -- --test-threads=1

# Concurrent write tests only
cargo test --test concurrency test_concurrent -- --test-threads=1

# Stress tests only (includes throughput metrics)
cargo test --test concurrency test_stress -- --test-threads=1 --nocapture

# ═══════════════════════════════════════════════════════════════════
# PERFORMANCE BENCHMARKS
# ═══════════════════════════════════════════════════════════════════

# Run all benchmarks
cargo bench

# Run specific benchmark
cargo bench --bench line_protocol_bench

# ═══════════════════════════════════════════════════════════════════
# FUZZ TESTING (requires nightly)
# ═══════════════════════════════════════════════════════════════════

# Run for 60 seconds
cargo +nightly fuzz run fuzz_line_protocol -- -max_total_time=60

CI/CD 集成

所有测试在 CI/CD 流水线中自动运行:

阶段 测试 触发时机
预提交 单元测试、正确性测试 每次提交
Pull Request 所有测试 + 基准测试(不检查回归) PR 创建/更新
合并到 main 完整套件 + 基准基线更新 合并
每晚 扩展模糊测试、压力测试 每天

下一步