docs: 添加 Rustdoc 注释

This commit is contained in:
2026-08-05 21:46:47 +08:00
parent 7c8f19f61a
commit 263fd255b6
158 changed files with 1440 additions and 15 deletions
+6
View File
@@ -1,15 +1,18 @@
use crate::bench::metrics::MetricCollection;
/// 基准结果集合,持久化到 `benchmark_baseline.json` 用于后续回归对比。
pub struct Baseline {
pub entries: Vec<MetricCollection>,
}
impl Baseline {
/// 构造空的基线。
pub fn empty() -> Self {
Self { entries: Vec::new() }
}
}
/// 一次性能回退:当前指标相对基线的比值超过容差即视为回归。
pub struct Regression {
pub metric: String,
pub test_name: String,
@@ -19,6 +22,7 @@ pub struct Regression {
pub tolerance: f64,
}
/// 从 `benchmark_baseline.json` 加载基线;文件不存在时返回空基线。
pub fn load_baseline() -> Result<Baseline, String> {
let json_path = "benchmark_baseline.json";
let data = match std::fs::read_to_string(json_path) {
@@ -33,6 +37,7 @@ pub fn load_baseline() -> Result<Baseline, String> {
Ok(Baseline { entries })
}
/// 保存当前结果到 `benchmark_baseline.json` 并生成 Markdown 表格。
pub fn save_baseline(results: &[MetricCollection]) -> Result<(), String> {
let json = serde_json::to_string_pretty(results).map_err(|e| format!("Failed to serialize baseline: {}", e))?;
std::fs::write("benchmark_baseline.json", &json)
@@ -42,6 +47,7 @@ pub fn save_baseline(results: &[MetricCollection]) -> Result<(), String> {
Ok(())
}
/// 对比当前结果与基线,返回所有超过各自容差(wall_time 10%、GC 类 50%、其它 20%)的回归项。
pub fn compare_baseline(current: &[MetricCollection]) -> Vec<Regression> {
let baseline = match load_baseline() {
Ok(b) => b,