一键导入
benchmarking
Measures Lua performance optimizations in Helm.spoon. Use when optimizing code, comparing implementations, or measuring execution time.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Measures Lua performance optimizations in Helm.spoon. Use when optimizing code, comparing implementations, or measuring execution time.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Creates Hammerspoon Spoon plugins following official conventions. Use when building, scaffolding, or packaging a Hammerspoon Spoon.
Hammerspoon macOS automation API reference. Use when writing Lua scripts for Hammerspoon, looking up hs.* modules, or automating macOS with keyboard shortcuts, window management, or system events.
| name | benchmarking |
| description | Measures Lua performance optimizations in Helm.spoon. Use when optimizing code, comparing implementations, or measuring execution time. |
Use the benchmark.lua library to measure and compare performance of Lua implementations.
local benchmark = require("benchmark")
-- Compare two implementations
local results = benchmark.compare({
baseline = function() original_code() end,
optimized = function() improved_code() end,
}, { iterations = 100 })
print(benchmark.report(results))
Measure a single function's performance.
Options:
iterations (default: 100) - Number of timed runswarmup (default: 10) - Warmup iterations before timingsetup - Function returning data passed to fnteardown - Called after each iterationReturns: { avg, median, min, max, p95 } in milliseconds
Compare multiple implementations.
Arguments:
implementations - Table mapping names to functionsbaseline and optimized keys for automatic speedup calculationReturns: Stats for each implementation plus speedup ratio
Compare and print results. Same as compare but outputs to console.
Calculate statistics from raw timing array.
Format milliseconds with appropriate units (ns/μs/ms/s).
Generate formatted report string from comparison results.
Write benchmark test first:
-- spec/my_feature_benchmark_spec.lua
describe("my feature performance", function()
it("optimized version is faster", function()
local results = benchmark.compare({
baseline = function() old_implementation(test_data) end,
optimized = function() new_implementation(test_data) end,
}, { iterations = 100 })
assert.is_true(results.speedup > 1.5, "Expected 1.5x speedup")
end)
end)
Run benchmark to establish baseline:
busted spec/my_feature_benchmark_spec.lua
Implement optimization
Re-run benchmark to verify improvement
-- Problem: frame() is called O(n log n) times during sort
local function sortOriginal(windows)
table.sort(windows, function(a, b)
return a:frame().x < b:frame().x
end)
return windows
end
-- Solution: Cache frames before sorting (O(n) calls)
local function sortOptimized(windows)
local cached = {}
for i, win in ipairs(windows) do
cached[i] = { win = win, x = win:frame().x }
end
table.sort(cached, function(a, b) return a.x < b.x end)
for i, entry in ipairs(cached) do cached[i] = entry.win end
return cached
end
-- Benchmark
benchmark.run({
baseline = function() sortOriginal(windows) end,
optimized = function() sortOptimized(windows) end,
}, {
iterations = 1000,
setup = function() return generateTestWindows(20) end,
})
For real-world testing with actual Hammerspoon APIs (windows, screens, etc.), update the benchmark hotkey in ~/.hammerspoon/init.lua:
hs.hotkey.bind({ "cmd", "ctrl" }, "b", function()
local benchmark = require("benchmark")
-- Replace with your current benchmark code
benchmark.run({
baseline = function() ... end,
optimized = function() ... end,
}, { iterations = 100, name = "My Optimization" })
end)
After updating, reload Hammerspoon (cmd+ctrl+r) and press cmd+ctrl+b to run. Results appear in the Hammerspoon console.
setup to create fresh test data for each iterationiterations = more stable resultswarmup to avoid JIT/cache cold-start effectsbaseline/optimized for automatic speedup