Appearance
性能优化
Git Doctor 的性能优化策略和基准测试结果。
性能基准
按仓库规模
| 仓库规模 | 提交数 | 完整诊断 | 体积分析 | 热点分析 |
|---|---|---|---|---|
| 小型 | < 1,000 | < 1s | < 0.5s | < 2s |
| 中型 | 1,000-10,000 | < 5s | < 1s | < 5s |
| 大型 | > 10,000 | < 30s | < 5s | < 20s |
各功能耗时
| 功能 | 平均耗时 | 阈值 | 内存占用 |
|---|---|---|---|
| 松散对象解析 (100个) | 45ms | 500ms | 2.3MB |
| 获取所有提交 (1000个) | 320ms | 5000ms | 15.2MB |
| 仓库体积分析 | 180ms | 3000ms | 8.5MB |
| 提交质量分析 | 450ms | 3000ms | 12.1MB |
| 分支健康度分析 | 85ms | 2000ms | 3.2MB |
| 敏感信息扫描 | 520ms | 3000ms | 18.7MB |
| 代码热点分析 | 1250ms | 10000ms | 25.4MB |
优化策略
1. 并行处理
四大分析器并行执行:
typescript
const [sizeResult, commitResult, branchResult, securityResult] =
await Promise.all([
analyzeSizeHealth(gitDir),
analyzeCommitQuality(gitDir),
analyzeBranchHealth(gitDir),
scanSecurity(gitDir)
])效果:总耗时减少约 58%
2. 增量扫描
支持 since 参数限制扫描范围:
typescript
// 只分析最近 30 天
const commits = await getCommitsInRange(gitDir, since)配置:
json
{
"gitDoctor.hotspot.timeRange": 30
}效果:大仓库热点分析可从 20s 降至 3s
3. 排除模式
过滤无关文件:
typescript
const excludePatterns = [
'**/node_modules/**',
'**/dist/**',
'**/*.lock'
]
function shouldExclude(filePath: string): boolean {
return excludePatterns.some(pattern => {
const regex = new RegExp(pattern.replace(/\*/g, '.*'))
return regex.test(filePath)
})
}效果:减少约 40% 的文件遍历
4. 对象缓存
缓存已解析的 Git 对象:
typescript
const objectCache = new Map<string, GitObject>()
function parseLooseObjectCached(
gitDir: string,
sha: string
): GitObject | null {
if (objectCache.has(sha)) {
return objectCache.get(sha)!
}
const obj = parseLooseObject(gitDir, sha)
if (obj) {
objectCache.set(sha, obj)
}
return obj
}5. 流式 AI 响应
减少用户等待感知:
typescript
// SSE 流式响应
for await (const chunk of streamResponse) {
yield chunk // 立即返回每个片段
}效果:首字符延迟 < 500ms
内存优化
内存使用基准
| 场景 | 峰值内存 |
|---|---|
| 小型仓库 | ~50MB |
| 中型仓库 | ~150MB |
| 大型仓库 | ~500MB |
优化建议
大仓库增量分析
json{ "gitDoctor.hotspot.timeRange": 30 }CI 环境限制内存
bashnode --max-old-space-size=1024 dist/cli.js analyze及时释放缓存
typescriptfunction clearCache() { objectCache.clear() }
基准测试代码
typescript
import { BenchmarkRunner } from './benchmark/performance'
async function runBenchmarks(gitDir: string) {
const runner = new BenchmarkRunner('Git Doctor Benchmarks')
await runner.add('解析松散对象', async () => {
for (const obj of walkLooseObjects(gitDir)) {
parseLooseObject(gitDir, obj.sha)
}
}, { iterations: 3, threshold: 500 })
await runner.add('获取所有提交', async () => {
await getAllCommits(gitDir)
}, { threshold: 5000 })
console.log(runner.generateReport())
}CI 性能监控
GitHub Actions
yaml
name: Performance Benchmark
on: [push]
jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run Benchmarks
run: |
npm ci
npm run benchmark > benchmark.md
- name: Check Thresholds
run: |
if grep -q "❌ 超时" benchmark.md; then
echo "Performance regression!"
exit 1
fi
- name: Upload Report
uses: actions/upload-artifact@v3
with:
name: benchmark
path: benchmark.md性能回归检测
typescript
// 比较两次运行结果
function detectRegression(
baseline: BenchmarkResult[],
current: BenchmarkResult[]
): string[] {
const regressions: string[] = []
for (const curr of current) {
const base = baseline.find(b => b.name === curr.name)
if (base && curr.duration > base.duration * 1.2) {
regressions.push(
`${curr.name}: ${base.duration}ms → ${curr.duration}ms (+${((curr.duration / base.duration - 1) * 100).toFixed(1)}%)`
)
}
}
return regressions
}最佳实践
1. 合理配置时间范围
json
{
// 日常开发:30天
"gitDoctor.hotspot.timeRange": 30,
// 季度回顾:90天
"gitDoctor.hotspot.timeRange": 90
}2. 排除无关目录
json
{
"gitDoctor.hotspot.excludePatterns": [
"**/node_modules/**",
"**/vendor/**",
"**/dist/**",
"**/*.min.js",
"**/*.map"
]
}3. CI 中限制分析范围
bash
# 只检查增量变更
git-doctor analyze --since="1 week ago"4. 定期运行完整分析
完整分析资源消耗大,建议:
- 每周/每迭代运行一次
- 使用定时任务而非每次提交触发