Files
hm 776b433eaa feat(quality): D 阶段工程质量 — 模型库 + fuzz_ccd + 性能回归 + 结构化日志 + API 版本化 + CI 增强
- D2: tests/data/models 按格式分目录模型库 + test_model_corpus 遍历测试(空库自动跳过)
- D3: tests/fuzz/fuzz_ccd(保守推进/扫掠球/GJK 一致性)+ Gitea CI Fuzz Smoketest
- D4/D6/D7: GitHub CI 新增 valgrind / sanitizer(ASan+UBSan) / coverage 三 job
- D8: tools/perf_regression.sh 性能回归脚本 + CI perf-regression job(退化>15% 报警)
- D10: include/vde/foundation/vde_log.h + src/foundation/vde_log.cpp 自研结构化日志
- D11: version.h 新增 VDE_API_VERSION_* / VDE_API_SINCE / VDE_DEPRECATED 宏
- 路线图 D 阶段状态同步
2026-08-09 08:09:22 +08:00

151 lines
5.0 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ═══════════════════════════════════════════════════════════
# VDE D8: 性能回归检测脚本
#
# 对每个 benchmark 测量基线时间,与上次基线比较。
# 若性能退化超过阈值(默认 15%)则返回非零退出码。
#
# 用法:
# tools/perf_regression.sh # 运行全部 benchmark 并对比
# tools/perf_regression.sh --update # 更新基线(首次或接受当前性能)
# tools/perf_regression.sh -t 0.30 # 自定义退化阈值 (30%)
#
# 基线存储: build/perf_baseline.json
# ═══════════════════════════════════════════════════════════
set -euo pipefail
BUILD_DIR="${BUILD_DIR:-build}"
THRESHOLD="${VDE_PERF_THRESHOLD:-0.15}"
UPDATE_BASELINE=false
while [[ $# -gt 0 ]]; do
case "$1" in
--update) UPDATE_BASELINE=true; shift ;;
-t) THRESHOLD="$2"; shift 2 ;;
*) echo "未知参数: $1"; exit 1 ;;
esac
done
BASELINE_FILE="$BUILD_DIR/perf_baseline.json"
if [[ ! -d "$BUILD_DIR" ]]; then
echo "错误: 构建目录 $BUILD_DIR 不存在。先运行 tools/build.sh"
exit 1
fi
# ── 选择可用的 benchmark ──────────────────────────────────────
BENCHES=()
for b in vde_bench_bvh vde_bench_delaunay vde_bench_boolean vde_bench_curves vde_bench_spatial; do
if [[ -x "$BUILD_DIR/bench/$b" || -x "$BUILD_DIR/bench/$b.exe" ]]; then
BENCHES+=("$b")
fi
done
if [[ ${#BENCHES[@]} -eq 0 ]]; then
echo "错误: 未找到任何 benchmark 可执行文件(需先构建 bench 目标)"
exit 1
fi
# ── 运行 benchmark 并解析 JSON 输出为 name<TAB>real_time ─────
measure() {
local bin="$1"
"$bin" --benchmark_format=json --benchmark_min_time=0.5s 2>/dev/null | python3 -c '
import json, sys
try:
data = json.load(sys.stdin)
except Exception:
sys.exit(0)
for b in data.get("benchmarks", []):
print(f"{b.get(\"name\", \"?\")}\t{b.get(\"real_time\", 0.0):.3f}")
' 2>/dev/null || true
}
# ── 收集当前测量结果:name<TAB>ms 输出到 stdout ────────────────
collect() {
for b in "${BENCHES[@]}"; do
local bin_path="$BUILD_DIR/bench/$b"
[[ -x "$bin_path" ]] || bin_path="$bin_path.exe"
echo "── 测量 $b ──" >&2
while IFS=$'\t' read -r name rt; do
[[ -z "$name" || -z "$rt" ]] && continue
echo "$b/$name $rt"
done < <(measure "$bin_path")
done
}
# ── 建立基线 ──────────────────────────────────────────────────
run_all() {
local tmp
tmp=$(mktemp)
collect | sort > "$tmp"
echo "── 生成 JSON 基线 ──"
python3 - "$tmp" "$BASELINE_FILE" <<'PYEOF'
import json, sys
tmp, out = sys.argv[1], sys.argv[2]
bench = {}
with open(tmp) as f:
for line in f:
name, rt = line.rstrip("\n").split("\t")
bench[name] = float(rt)
payload = {"generated": __import__("datetime").datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"benchmarks": bench}
with open(out, "w") as f:
json.dump(payload, f, indent=2)
print(f"基线已保存到 {out}({len(bench)} 项)")
PYEOF
rm -f "$tmp"
}
# ── 对比基线 ──────────────────────────────────────────────────
compare() {
if [[ ! -f "$BASELINE_FILE" ]]; then
echo "未找到基线文件,首次运行将直接建立基线"
run_all
return 0
fi
local tmp
tmp=$(mktemp)
collect | sort > "$tmp"
echo "── 与基线对比(阈值 $THRESHOLD)──"
python3 - "$tmp" "$BASELINE_FILE" "$THRESHOLD" <<'PYEOF'
import json, sys
cur_file, base_file, threshold = sys.argv[1], sys.argv[2], float(sys.argv[3])
with open(base_file) as f:
base = json.load(f)["benchmarks"]
cur = {}
with open(cur_file) as f:
for line in f:
name, rt = line.rstrip("\n").split("\t")
cur[name] = float(rt)
regressed = 0
for name in sorted(cur):
rt = cur[name]
if name in base and base[name] > 0:
ratio = (rt - base[name]) / base[name]
status = "REGRESSION" if ratio > threshold else "ok"
if ratio > threshold:
regressed += 1
print(f" {name:<45s} {base[name]:8.3f} -> {rt:8.3f} ({ratio*100:+6.1f}%) [{status}]")
else:
print(f" {name:<45s} (new) {rt:8.3f} ms")
if regressed > 0:
print(f"\n⚠ 检测到 {regressed} 项性能退化(阈值 {threshold})")
print(" 若为预期变更,使用 tools/perf_regression.sh --update 更新基线")
sys.exit(1)
print("\n✓ 全部通过,无性能退化")
PYEOF
rm -f "$tmp"
}
if [[ "$UPDATE_BASELINE" == "true" ]]; then
run_all
else
compare
fi