con un clic
benchmark
本地效能測試工具,用於測量程式碼執行時間、記憶體使用、N+1 查詢偵測等。支援 Ruby、JavaScript、Python。
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Menú
本地效能測試工具,用於測量程式碼執行時間、記憶體使用、N+1 查詢偵測等。支援 Ruby、JavaScript、Python。
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Basado en la clasificación ocupacional SOC
在 4 個 Rails 專案(sf_project / jv_project / core_web / e_trading)跑 local dev 指令——console、server、rspec、migrate、rake、bundle。這些指令一律「進 Tilt 起的 web container」跑,不在 host 直接跑。Use when 要對這些 rails 專案開 console、跑 rspec、跑 migration / rake / bundle,或 local server(sf.local / jv.local / core.local / et.local)連不上要排查時。
資料遷移工具,從 Production 安全地遷移資料到 Local/Staging 環境。自動清理敏感資料,硬編碼禁止反向遷移。
Archive Claude Code telemetry logs to dated files (YYYY-MM-DD~YYYY-MM-DD.json). Use when user wants to archive, rotate, or save telemetry logs.
Generates optimized prompts for any AI tool. Use when writing, fixing, improving, or adapting a prompt for LLM, Cursor, Midjourney, image AI, video AI, coding agents, or any other AI tool.
BA 報告撰寫指引。在撰寫、審查或修改 BA 報告(-ba.md)時使用。確保 BA 報告使用純業務語言,不包含任何技術術語。
AWS EC2 operations including checking service status, viewing logs, server health monitoring, and production/staging environment management. Use when user asks to check/view/查看 production/staging servers, service status/狀態/狀況, logs/日誌, or perform remote operations like deployment and database backup.
| name | benchmark |
| description | 本地效能測試工具,用於測量程式碼執行時間、記憶體使用、N+1 查詢偵測等。支援 Ruby、JavaScript、Python。 |
| version | 1.0.0 |
本地效能測試和分析工具。
量測先於優化 - 先確認效能瓶頸,再進行針對性優化
| 類型 | 用途 | 工具 |
|---|---|---|
| 執行時間 | 測量程式碼執行速度 | Benchmark, benchmark-ips |
| 記憶體使用 | 測量記憶體消耗 | memory_profiler |
| N+1 偵測 | 找出 N+1 查詢問題 | bullet |
| HTTP 負載 | API 壓力測試 | ab, wrk |
| 資料庫查詢 | SQL 效能分析 | EXPLAIN ANALYZE |
# 執行時間測量
require 'benchmark'
result = Benchmark.measure do
# 要測試的程式碼
1000.times { User.find(1) }
end
puts result
# user system total real
# 0.123456 0.012345 0.135801 ( 0.140000)
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("方法 A") { method_a }
x.report("方法 B") { method_b }
x.compare!
end
require 'memory_profiler'
report = MemoryProfiler.report do
# 要分析的程式碼
large_array = (1..100000).map { |i| "string #{i}" }
end
report.pretty_print
# 使用 bullet gem(需在 Gemfile 中添加)
# config/environments/development.rb
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
# 或手動檢查
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
puts "SQL (#{event.duration.round(1)}ms): #{event.payload[:sql]}"
end
# 然後執行要測試的程式碼
projects = Project.all
projects.each { |p| puts p.invoices.count } # N+1!
console.time('operation');
// 要測試的程式碼
for (let i = 0; i < 1000; i++) {
someFunction();
}
console.timeEnd('operation');
// operation: 123.456ms
const { performance } = require('perf_hooks');
const start = performance.now();
// 要測試的程式碼
await someAsyncOperation();
const end = performance.now();
console.log(`執行時間: ${end - start} ms`);
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
suite
.add('方法 A', function() {
methodA();
})
.add('方法 B', function() {
methodB();
})
.on('complete', function() {
console.log('最快的是: ' + this.filter('fastest').map('name'));
})
.run();
# 基本測試:100 個請求,10 個併發
ab -n 100 -c 10 http://localhost:3000/api/projects
# 帶認證的測試
ab -n 100 -c 10 -H "Authorization: Bearer TOKEN" http://localhost:3000/api/projects
# POST 請求
ab -n 100 -c 10 -p data.json -T 'application/json' http://localhost:3000/api/projects
輸出解讀:
Requests per second: 150.00 [#/sec] (mean) # 每秒處理請求數
Time per request: 66.67 [ms] (mean) # 平均回應時間
Time per request: 6.67 [ms] (mean, across all concurrent requests)
Percentage of the requests served within a certain time (ms)
50% 60 # 50% 的請求在 60ms 內完成
90% 100 # 90% 的請求在 100ms 內完成
99% 200 # 99% 的請求在 200ms 內完成
# 基本測試
wrk -t12 -c400 -d30s http://localhost:3000/api/projects
# 帶 Lua 腳本
wrk -t12 -c400 -d30s -s post.lua http://localhost:3000/api/projects
EXPLAIN ANALYZE
SELECT * FROM invoices
WHERE project_id = 123
AND status = 'pending';
輸出解讀:
Seq Scan on invoices (cost=0.00..1000.00 rows=100 width=200) (actual time=0.020..10.000 rows=100 loops=1)
Filter: ((project_id = 123) AND (status = 'pending'))
Rows Removed by Filter: 9900
Planning Time: 0.100 ms
Execution Time: 10.200 ms
關注點:
Seq Scan vs Index Scan:Seq Scan 表示全表掃描actual time:實際執行時間Rows Removed by Filter:被過濾掉的行數# 在 Rails console
Invoice.where(project_id: 123, status: 'pending').explain
# 或
ActiveRecord::Base.connection.execute("EXPLAIN ANALYZE SELECT * FROM invoices WHERE project_id = 123")
# benchmark_test.rb
require 'benchmark'
require 'benchmark/ips'
require 'memory_profiler'
puts "=== 執行時間測試 ==="
Benchmark.bm(20) do |x|
x.report("原始方法:") { original_method }
x.report("優化方法:") { optimized_method }
end
puts "\n=== IPS 比較 ==="
Benchmark.ips do |x|
x.report("原始方法") { original_method }
x.report("優化方法") { optimized_method }
x.compare!
end
puts "\n=== 記憶體使用 ==="
report = MemoryProfiler.report { original_method }
puts "總分配: #{report.total_allocated_memsize} bytes"
puts "總保留: #{report.total_retained_memsize} bytes"
執行:
# 路徑從 .claude/config/projects.yml 取得
cd {project_path} && bundle exec rails runner benchmark_test.rb
require 'benchmark/ips'
# 方法 A: 使用 each
def method_a(items)
result = []
items.each { |i| result << i * 2 }
result
end
# 方法 B: 使用 map
def method_b(items)
items.map { |i| i * 2 }
end
items = (1..10000).to_a
Benchmark.ips do |x|
x.report("each + push") { method_a(items) }
x.report("map") { method_b(items) }
x.compare!
end
# 開啟 SQL 日誌
ActiveRecord::Base.logger = Logger.new(STDOUT)
# 測試程式碼
projects = Project.all # 1 次查詢
projects.each do |p|
puts p.invoices.count # N 次查詢!
end
# 修正後
projects = Project.includes(:invoices).all # 2 次查詢
projects.each do |p|
puts p.invoices.size # 0 次額外查詢
end
# 測試單一 endpoint
ab -n 100 -c 1 http://localhost:3000/api/projects | grep -E "Time per request|Requests per second"
# 比較不同 endpoint
for endpoint in projects invoices users; do
echo "=== $endpoint ==="
ab -n 100 -c 10 http://localhost:3000/api/$endpoint 2>/dev/null | grep "Time per request.*mean\)"
done
測試完成後輸出:
┌──────────────────────────────────────────────────────┐
│ 📊 效能測試報告 │
├──────────────────────────────────────────────────────┤
│ 測試項目:Invoice 查詢優化 │
│ 測試環境:Local (MacBook Pro, 16GB RAM) │
│ │
│ 📈 執行時間比較: │
│ ┌────────────────┬──────────┬──────────┐ │
│ │ 方法 │ 時間 │ 改善 │ │
│ ├────────────────┼──────────┼──────────┤ │
│ │ 原始方法 │ 150ms │ - │ │
│ │ 優化方法 │ 45ms │ 70% ⬇️ │ │
│ └────────────────┴──────────┴──────────┘ │
│ │
│ 💾 記憶體使用: │
│ • 原始:50 MB │
│ • 優化:15 MB(-70%) │
│ │
│ 🔍 SQL 查詢: │
│ • 原始:101 次(N+1) │
│ • 優化:2 次(includes) │
│ │
│ 💡 建議:採用優化方法,效能提升 70% │
└──────────────────────────────────────────────────────┘
✅ 在 Local/Test 環境執行效能測試
✅ 使用 limit 限制測試資料量
✅ 避免在效能測試中修改資料
❌ 在 Production 執行負載測試
❌ 使用 Production 資料進行測試
❌ 長時間佔用資源的測試
效能測試在以下情況使用:
調查階段:找出效能瓶頸
測試階段:建立效能基準測試
開發階段:實作優化
驗收階段:確認效能改善