AgentSkillsCN

Performance

优化、性能分析、缓存策略与性能预算指南

SKILL.md
--- frontmatter
name: Performance
description: Guidelines for optimization, profiling, caching strategies, and performance budgets

Performance Skill

When to use this skill

Use when optimizing existing code, implementing caching, setting up profiling, or when performance budgets are at risk.


Performance Principles

1. Measure first, optimize second

  • Never optimize without a benchmark showing the problem
  • Profile to find the actual bottleneck — intuition is often wrong
  • Set concrete, measurable budgets and track them

2. The fastest code is code that doesn't run

  • Cache expensive results
  • Avoid unnecessary computation
  • Lazy-load what isn't immediately needed
  • Short-circuit early when possible

3. Optimize for the common case

  • 80% of time is spent in 20% of code — find that 20%
  • Don't optimize cold paths at the expense of hot paths

Performance Budgets

Define and enforce these in CI:

MetricTargetMeasured by
App startup< ___msStartup trace/log
API response (p50)< ___msRequest metrics
API response (p95)< ___msRequest metrics
API response (p99)< ___msRequest metrics
Page load (LCP)< ___sLighthouse / Web Vitals
Time to interactive< ___sLighthouse
JS bundle size< ___KBBuild output
Memory usage (steady state)< ___MBRuntime metrics
Database query (p95)< ___msQuery metrics

Fill in your actual targets. Leave blank = no budget = no accountability.


Profiling Checklist

When investigating a performance issue:

  1. Reproduce consistently — create a repeatable test case
  2. Measure baseline — record current performance numbers
  3. Profile — use the appropriate tool for your stack:
    • CPU: profiler (Chrome DevTools / py-spy / pprof)
    • Memory: heap snapshot (Chrome / tracemalloc / pprof)
    • Network: request waterfall (DevTools Network tab)
    • Database: slow query log + EXPLAIN plans
  4. Identify the bottleneck — find the single biggest contributor
  5. Fix one thing — make one change, re-measure
  6. Verify improvement — compare against baseline
  7. Document — record what you changed and the before/after numbers

Caching Strategy

Cache layers

code
Client Cache → CDN → App Cache → Database Cache → Database
(fastest)                                        (slowest)

When to cache

ScenarioCache strategy
Static assets (images, CSS, JS)CDN + long-lived browser cache
API responses that rarely changeHTTP cache headers (ETag, Cache-Control)
Expensive computationsIn-memory cache with TTL
Database query resultsRedis/Memcached with invalidation
Session dataServer-side session store

Cache invalidation rules

  • Set TTL (time-to-live) — every cached item must expire
  • Invalidate on write — when data changes, invalidate related caches
  • Cache keys must be deterministic — same input = same cache key
  • Monitor hit rate — < 80% hit rate suggests cache configuration issues

Cache anti-patterns

  • ❌ Caching without TTL (stale data forever)
  • ❌ Caching user-specific data in shared cache
  • ❌ Cache stampede (many requests rebuilding cache simultaneously)
  • ❌ Caching errors (negative caching without short TTL)

Common Optimizations

Backend

TechniqueWhen to use
Database indexingSlow queries on specific columns
Connection poolingHigh-throughput database access
Batch operationsProcessing many items (use bulk insert/update)
Async processingNon-blocking operations (email, notifications)
PaginationLarge result sets
CompressionLarge response bodies (gzip/brotli)

Frontend

TechniqueWhen to use
Code splittingLarge JS bundles (lazy-load routes)
Image optimizationHeavy pages (WebP/AVIF, srcset, lazy-load)
Virtual scrollingLong lists (>100 items)
Debounce/throttleFrequent events (scroll, resize, search input)
Service workersOffline support, background sync
Critical CSSSlow first paint (inline above-fold CSS)

Load Testing

Before launching or major changes:

  1. Define load expectations — expected concurrent users, requests/sec
  2. Create realistic scenarios — simulate real user behavior, not just hits
  3. Test at 2x expected load — find the breaking point
  4. Monitor during test — CPU, memory, response times, error rates
  5. Document results — include in the exec plan validation evidence

PR Checklist for Performance Changes

  • Baseline measured before changes
  • Improvement measured after changes (with numbers)
  • No regression in other areas
  • Performance budgets still met
  • Cache invalidation tested
  • Load tested if applicable
  • Before/after numbers documented in PR