第 6 篇:「用数据说话」— 性能基准测试如何证明架构
开场:感觉不等于事实
到目前为止,我们讨论了很多次"快速"“性能好”。
但有一个问题:你怎么知道真的快?
感觉骗人。优化前"好像"卡,优化后"好像"快。直到你看到数据。
这一篇讲的是 LoopAgent 如何用科学的基准测试来证明架构的有效性。
基准测试的四个层级
层级 1: 快速验证(Quick)
npmrun benchmark:search -- quick规模:500 个符号的小项目
耗时:< 10 秒
用途:开发时快速反馈
预期结果:
┌─ LoopAgent Search Benchmark ───────────┐ │ Workspace size: 500 symbols │ │ Files: 50 │ │ │ │ 查询测试: │ │ ├─ Query: "get" │ │ │ ├─ SQLite: 1.2ms ✓ │ │ │ └─ Memory: 8ms │ │ ├─ Query: "user" │ │ │ ├─ SQLite: 0.8ms ✓ │ │ │ └─ Memory: 12ms │ │ └─ Query: "buildComponent" │ │ ├─ SQLite: 2.1ms ✓ │ │ └─ Memory: 15ms │ │ │ │ 平均延迟:SQLite 1.4ms, Memory 11.7ms│ │ 加速倍数: 8.4x ✓ │ └────────────────────────────────────────┘层级 2: 标准测试(Standard)- 推荐
npmrun benchmark:search -- standard规模:5K 符号的典型项目
耗时:30-60 秒
用途:CI/CD 中的常规测试
预期结果:
┌─ LoopAgent Search Benchmark ───────────┐ │ Workspace size: 5,243 symbols │ │ Files: 200 │ │ Database size: 12.4 MB │ │ │ │ ① 初始化 │ │ ├─ First startup: 4.2s │ │ ├─ Second startup (cached): 42ms │ │ └─ Database verification: 8ms │ │ │ │ ② 搜索性能(20 个常见查询) │ │ ├─ p50 (中位数): 6.8ms │ │ ├─ p95 (95%分位): 12.3ms │ │ ├─ p99 (99%分位): 18.5ms │ │ ├─ max (最大值): 34.2ms │ │ └─ Memory fallback: 45-52ms │ │ │ │ ③ 更新性能(模拟编辑) │ │ ├─ Single file update: 15ms │ │ ├─ 100 files batch: 1.2s │ │ └─ Incremental rebuild: 2.3s │ │ │ │ ④ 并发测试 (3 个搜索 + 1 个写) │ │ ├─ Conflict rate: 0% │ │ ├─ Read latency: 8.5ms (avg) │ │ ├─ Write latency: 18ms (avg) │ │ └─ Zero corruptions ✓ │ │ │ │ 性能评分:⭐⭐⭐⭐⭐ (5/5) │ │ 状态:PASS ✓ │ └────────────────────────────────────────┘层级 3: 完整测试(Full)
npmrun benchmark:search -- full规模:50K 符号的大型项目
耗时:5-10 分钟
用途:新版本发布前的验收测试
预期结果:
多个测试套件运行: ✓ Memory efficiency (内存占用是否在线性增长) ✓ GC pressure (垃圾回收是否过频繁) ✓ Index corruption resilience (破坏恢复能力) ✓ Concurrent access patterns (并发访问模式) ✓ Worst-case latency (最坏情况延迟)层级 4: 持续监控(Continuous)
# .github/workflows/benchmark-monitor.ymlname:Benchmark Monitoringon:[push]jobs:benchmark:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v3-run:npm run benchmark:search--standard-name:Store resultsrun:|mkdir -p benchmark-results cp benchmark-output.json benchmark-results/$(date +%s).json-name:Check regressionrun:|# 对比最近 10 次的结果 # 如果变慢 > 20% → 失败 npm run benchmark:compare基准测试的设计
测试用例集合
interfaceBenchmarkSuite{name:string;tests:BenchmarkTest[];}interfaceBenchmarkTest{name:string;operation:async()=>void;iterations?:number;expectedLatency?:number;// msdescription:string;}constBENCHMARK_SUITE:BenchmarkSuite={name:'LoopAgent Search Benchmarks',tests:[// ========== 查询延迟 =========={name:'Single word query',operation:async()=>{awaitsearch('get');},iterations:100,expectedLatency:10,// 预期 < 10msdescription:'简单单词搜索(最常见)',},{name:'Phrase query',operation:async()=>{awaitsearch('build component');},iterations:50,expectedLatency:12,description:'短语搜索',},{name:'Complex query',operation:async()=>{awaitsearch('fetch* user AND (auth OR permission)');},iterations:20,expectedLatency:15,description:'复杂布尔查询',},// ========== 初始化 =========={name:'First startup (build index)',operation:async()=>{awaitstore.initialize();},iterations:1,expectedLatency:21000,//