# Reed-Solomon 纠删码性能基准测试 本目录包含了比较不同 Reed-Solomon 实现性能的综合基准测试套件。 ## 📊 测试概述 ### 支持的实现模式 #### 🏛️ 纯 Erasure 模式(默认,推荐) - **稳定可靠**: 使用成熟的 reed-solomon-erasure 实现 - **广泛兼容**: 支持任意分片大小 - **内存高效**: 优化的内存使用模式 - **可预测性**: 性能对分片大小不敏感 - **使用场景**: 生产环境默认选择,适合大多数应用场景 #### 🎯 混合模式(`reed-solomon-simd` feature) - **自动优化**: 根据分片大小智能选择最优实现 - **SIMD + Erasure Fallback**: 大分片使用 SIMD 优化,小分片或 SIMD 失败时自动回退到 Erasure 实现 - **兼容性**: 支持所有分片大小和配置 - **性能**: 在各种场景下都能提供最佳性能 - **使用场景**: 需要最大化性能的场景,适合处理大量数据 **回退机制**: - ✅ 分片 ≥ 512 字节:优先使用 SIMD 优化 - 🔄 分片 < 512 字节或 SIMD 失败:自动回退到 Erasure 实现 - 📊 无缝切换,透明给用户 ### 测试维度 - **编码性能** - 数据编码成纠删码分片的速度 - **解码性能** - 从纠删码分片恢复原始数据的速度 - **分片大小敏感性** - 不同分片大小对性能的影响 - **纠删码配置** - 不同数据/奇偶分片比例的性能影响 - **混合模式回退** - SIMD 与 Erasure 回退机制的性能 - **并发性能** - 多线程环境下的性能表现 - **内存效率** - 内存使用模式和效率 - **错误恢复能力** - 不同丢失分片数量下的恢复性能 ## 🚀 快速开始 ### 运行快速测试 ```bash # 运行快速性能对比测试(默认混合模式) ./run_benchmarks.sh quick ``` ### 运行完整对比测试 ```bash # 运行详细的实现对比测试 ./run_benchmarks.sh comparison ``` ### 运行特定模式的测试 ```bash # 测试默认纯 erasure 模式(推荐) ./run_benchmarks.sh erasure # 测试混合模式(SIMD + Erasure fallback) ./run_benchmarks.sh hybrid ``` ## 📈 手动运行基准测试 ### 基本使用 ```bash # 运行所有基准测试(默认纯 erasure 模式) cargo bench # 运行特定的基准测试文件 cargo bench --bench erasure_benchmark cargo bench --bench comparison_benchmark ``` ### 对比不同实现模式 ```bash # 测试默认纯 erasure 模式 cargo bench --bench comparison_benchmark # 测试混合模式(SIMD + Erasure fallback) cargo bench --bench comparison_benchmark \ --features reed-solomon-simd # 保存基线进行对比 cargo bench --bench comparison_benchmark \ -- --save-baseline erasure_baseline # 与基线比较混合模式性能 cargo bench --bench comparison_benchmark \ --features reed-solomon-simd \ -- --baseline erasure_baseline ``` ### 过滤特定测试 ```bash # 只运行编码测试 cargo bench encode # 只运行解码测试 cargo bench decode # 只运行特定数据大小的测试 cargo bench 1MB # 只运行特定配置的测试 cargo bench "4+2" ``` ## 📊 查看结果 ### HTML 报告 基准测试结果会自动生成 HTML 报告: ```bash # 启动本地服务器查看报告 cd target/criterion python3 -m http.server 8080 # 在浏览器中访问 open http://localhost:8080/report/index.html ``` ### 命令行输出 基准测试会在终端显示: - 每秒操作数 (ops/sec) - 吞吐量 (MB/s) - 延迟统计 (平均值、标准差、百分位数) - 性能变化趋势 - 回退机制触发情况 ## 🔧 测试配置 ### 数据大小 - **小数据**: 1KB, 8KB - 测试小文件场景和回退机制 - **中等数据**: 64KB, 256KB - 测试常见文件大小 - **大数据**: 1MB, 4MB - 测试大文件处理和 SIMD 优化 - **超大数据**: 16MB+ - 测试高吞吐量场景 ### 纠删码配置 - **(4,2)** - 常用配置,33% 冗余 - **(6,3)** - 50% 冗余,平衡性能和可靠性 - **(8,4)** - 50% 冗余,更多并行度 - **(10,5)**, **(12,6)** - 高并行度配置 ### 分片大小 测试从 32 字节到 8KB 的不同分片大小,特别关注: - **回退临界点**: 512 字节 - 混合模式的 SIMD/Erasure 切换点 - **内存对齐**: 64, 128, 256 字节 - 内存对齐对性能的影响 - **Cache 友好**: 1KB, 2KB, 4KB - CPU 缓存友好的大小 ## 📝 解读测试结果 ### 性能指标 1. **吞吐量 (Throughput)** - 单位: MB/s 或 GB/s - 衡量数据处理速度 - 越高越好 2. **延迟 (Latency)** - 单位: 微秒 (μs) 或毫秒 (ms) - 衡量单次操作时间 - 越低越好 3. **CPU 效率** - 每 CPU 周期处理的字节数 - 反映算法效率 4. **回退频率** - 混合模式下 SIMD 到 Erasure 的回退次数 - 反映智能选择的效果 ### 预期结果 **纯 Erasure 模式(默认)**: - 性能稳定,对分片大小不敏感 - 兼容性最佳,支持所有配置 - 内存使用稳定可预测 **混合模式(`reed-solomon-simd` feature)**: - 大分片 (≥512B):接近纯 SIMD 性能 - 小分片 (<512B):自动回退到 Erasure,保证兼容性 - 整体:在各种场景下都有良好表现 **分片大小敏感性**: - 混合模式在 512B 附近可能有性能切换 - 纯 Erasure 模式对分片大小相对不敏感 **内存使用**: - 混合模式根据场景优化内存使用 - 纯 Erasure 模式内存使用更稳定 ## 🛠️ 自定义测试 ### 添加新的测试场景 编辑 `benches/erasure_benchmark.rs` 或 `benches/comparison_benchmark.rs`: ```rust // 添加新的测试配置 let configs = vec![ // 你的自定义配置 BenchConfig::new(10, 4, 2048 * 1024, 2048 * 1024), // 10+4, 2MB ]; ``` ### 调整测试参数 ```rust // 修改采样和测试时间 group.sample_size(20); // 样本数量 group.measurement_time(Duration::from_secs(10)); // 测试时间 ``` ### 测试回退机制 ```rust // 测试混合模式的回退行为 #[cfg(not(feature = "reed-solomon-erasure"))] { // 测试小分片是否正确回退 let small_data = vec![0u8; 256]; // 小于 512B,应该使用 Erasure let erasure = Erasure::new(4, 2, 256); let result = erasure.encode_data(&small_data); assert!(result.is_ok()); // 应该成功回退 } ``` ## 🐛 故障排除 ### 常见问题 1. **编译错误**: 确保安装了正确的依赖 ```bash cargo update cargo build --all-features ``` 2. **性能异常**: 检查是否在正确的模式下运行 ```bash # 检查当前配置 cargo bench --bench comparison_benchmark -- --help ``` 3. **回退过于频繁**: 调整 SIMD 临界点 ```rust // 在代码中可以调整这个值 const SIMD_MIN_SHARD_SIZE: usize = 512; ``` 4. **测试时间过长**: 调整测试参数 ```bash # 使用更短的测试时间 cargo bench -- --quick ``` ### 性能分析 使用 `perf` 等工具进行更详细的性能分析: ```bash # 分析 CPU 使用情况 cargo bench --bench comparison_benchmark & perf record -p $(pgrep -f comparison_benchmark) perf report ``` ### 调试回退机制 ```bash # 启用详细日志查看回退情况 RUST_LOG=warn cargo bench --bench comparison_benchmark ``` ## 🤝 贡献 欢迎提交新的基准测试场景或优化建议: 1. Fork 项目 2. 创建特性分支: `git checkout -b feature/new-benchmark` 3. 添加测试用例 4. 提交更改: `git commit -m 'Add new benchmark for XYZ'` 5. 推送到分支: `git push origin feature/new-benchmark` 6. 创建 Pull Request ## 📚 参考资料 - [reed-solomon-erasure crate](https://crates.io/crates/reed-solomon-erasure) - [reed-solomon-simd crate](https://crates.io/crates/reed-solomon-simd) - [Criterion.rs 基准测试框架](https://bheisler.github.io/criterion.rs/book/) - [Reed-Solomon 纠删码原理](https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction) --- 💡 **提示**: - 推荐使用默认的混合模式,它能在各种场景下自动选择最优实现 - 基准测试结果可能因硬件、操作系统和编译器版本而异 - 建议在目标部署环境中运行测试以获得最准确的性能数据