local source = debug.getinfo(1, "S").source:sub(2) local root = vim.fn.fnamemodify(source, ":p:h:h") local benchmark = dofile(vim.fs.joinpath(root, "benchmarks", "lib.lua")) local function main() local quick = benchmark.env_truthy("STATUSLINE_BENCH_QUICK") local samples = benchmark.positive_integer("STATUSLINE_BENCH_SAMPLES", quick and 3 or 15) local iterations = benchmark.positive_integer("STATUSLINE_BENCH_ITERATIONS", quick and 30 or 500) local warmup = benchmark.positive_integer("STATUSLINE_BENCH_WARMUP", quick and 5 or 100) local memory_iterations = benchmark.positive_integer( "STATUSLINE_BENCH_MEMORY_ITERATIONS", quick and 10 or 200 ) local scales = benchmark.parse_scales("STATUSLINE_BENCH_SCALES", quick and { 1, 10 } or { 1, 10, 100 }) local full_buffers = benchmark.positive_integer("STATUSLINE_BENCH_FULL_BUFFERS", quick and 5 or 25) local window_count = benchmark.positive_integer("STATUSLINE_BENCH_WINDOWS", quick and 2 or 4) local filter = vim.env.STATUSLINE_BENCH_FILTER local configuration = { quick = quick, samples = samples, iterations = iterations, warmup = warmup, memory_iterations = memory_iterations, diagnostic_buffer_scales = scales, full_scenario_buffers = full_buffers, simulated_redraw_windows = window_count, filter = filter or "", } local results = {} local function selected(name) return not filter or filter == "" or name:find(filter, 1, true) ~= nil end local function scaled(value, divisor) return math.max(1, math.floor(value / divisor)) end local function run_case(name, group, description, fn, options) if not selected(name) then return end options = options or {} local result = benchmark.measure(fn, { iterations = options.iterations or iterations, samples = options.samples or samples, warmup = options.warmup or warmup, memory_iterations = options.memory == false and 0 or options.memory_iterations or memory_iterations, }) result.name = name result.group = group result.description = description result.baseline = options.baseline results[#results + 1] = result end local statusline = require("statusline") local cursor = require("statusline.components.cursor") local cwd = require("statusline.components.cwd") local diagnostics = require("statusline.components.diagnostics") local filetype = require("statusline.components.filetype") local lsp = require("statusline.components.lsp") local macro = require("statusline.components.macro") local mode = require("statusline.components.mode") statusline.setup({ filetype_icons = { lua = "LUA" }, lsp_exclude = { "excluded-ls" }, }) local current_buffer = vim.api.nvim_get_current_buf() vim.api.nvim_set_option_value("filetype", "lua", { buf = current_buffer }) local static_statusline = "%#StatusModeNormal# NORMAL %*%=%l:%c" _G.statusline_benchmark_noop = function() return static_statusline end local plugin_expression = "%!v:lua.require('statusline').render()" local noop_expression = "%!v:lua.statusline_benchmark_noop()" local current_window = vim.api.nvim_get_current_win() run_case("baseline/direct-static", "baseline", "Return a prebuilt statusline string from Lua", function() return static_statusline end) run_case("baseline/eval-builtin", "baseline", "Evaluate a small built-in statusline", function() return vim.api.nvim_eval_statusline("%l:%c", { winid = current_window }) end) run_case("baseline/eval-lua-callback", "baseline", "Evaluate a statusline using a minimal Lua callback", function() return vim.api.nvim_eval_statusline(noop_expression, { winid = current_window }) end) run_case("component/cursor", "component", "Render the constant cursor component", cursor.render) run_case("component/mode", "component", "Resolve and render a known mode", function() return mode.render("n") end) run_case("component/cwd", "component", "Shorten and render the working directory", function() return cwd.render(root) end) run_case("component/macro-inactive", "component", "Query macro state when recording is inactive", macro.render) run_case("component/filetype-configured", "component", "Render a filetype with a configured icon", function() return filetype.render("lua") end) run_case("component/filetype-devicons", "component", "Render a filetype through devicons lookup", function() return filetype.render("markdown") end) run_case("component/lsp-empty", "component", "Query a buffer with no attached LSP clients", function() return lsp.render(current_buffer) end) run_case("component/diagnostics-empty", "component", "Scan the current empty buffer list for diagnostics", function() return diagnostics.count(root) end) local original_get_clients = vim.lsp.get_clients vim.lsp.get_clients = function() return { { name = "lua_ls" }, { name = "excluded-ls" }, { name = "null-ls" }, { name = "copilot" }, } end local ok, err = xpcall(function() run_case("component/lsp-four-clients", "component", "Filter and render four mocked LSP clients", function() return lsp.render(current_buffer) end) end, debug.traceback) vim.lsp.get_clients = original_get_clients if not ok then error(err) end local fixture_id = 0 local function with_diagnostic_fixture(buffer_count, diagnostics_per_buffer, outside_count, fn) fixture_id = fixture_id + 1 local temporary = vim.fn.tempname() local workspace = vim.fs.joinpath(temporary, "workspace") local outside = vim.fs.joinpath(temporary, "outside") vim.fn.mkdir(vim.fs.joinpath(workspace, "src"), "p") vim.fn.mkdir(outside, "p") local namespace = vim.api.nvim_create_namespace("statusline-benchmark-" .. fixture_id) local buffers = {} local function create_buffer(path, with_diagnostics) local buffer = vim.api.nvim_create_buf(false, true) vim.api.nvim_buf_set_name(buffer, path) buffers[#buffers + 1] = buffer if with_diagnostics > 0 then local entries = {} local severities = { vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN, vim.diagnostic.severity.INFO, vim.diagnostic.severity.HINT, } for index = 1, with_diagnostics do entries[index] = { lnum = index - 1, col = 0, message = "benchmark diagnostic " .. index, severity = severities[(index - 1) % #severities + 1], source = "statusline-benchmark", } end vim.diagnostic.set(namespace, buffer, entries) end end for index = 1, buffer_count do create_buffer(vim.fs.joinpath(workspace, "src", ("buffer-%04d.lua"):format(index)), diagnostics_per_buffer) end for index = 1, outside_count do create_buffer(vim.fs.joinpath(outside, ("buffer-%04d.lua"):format(index)), diagnostics_per_buffer) end local fixture_ok, result = xpcall(function() return fn(workspace, buffers) end, debug.traceback) for _, buffer in ipairs(buffers) do if vim.api.nvim_buf_is_valid(buffer) then vim.diagnostic.reset(namespace, buffer) vim.api.nvim_buf_delete(buffer, { force = true }) end end vim.fn.delete(temporary, "rf") if not fixture_ok then error(result) end return result end for _, scale in ipairs(scales) do with_diagnostic_fixture(scale, 0, 0, function(workspace) run_case( ("diagnostics/count-empty-%d-buffers"):format(scale), "diagnostics", ("Count diagnostics across %d named workspace buffers without diagnostics"):format(scale), function() return diagnostics.count(workspace) end ) end) with_diagnostic_fixture(scale, 4, 0, function(workspace) run_case( ("diagnostics/count-populated-%d-buffers"):format(scale), "diagnostics", ("Count four diagnostics in each of %d named workspace buffers"):format(scale), function() return diagnostics.count(workspace) end ) diagnostics.invalidate() diagnostics.render(workspace) run_case( ("diagnostics/render-warm-%d-buffers"):format(scale), "diagnostics", ("Reuse the cached rendered diagnostics for %d populated workspace buffers"):format(scale), function() return diagnostics.render(workspace) end ) run_case( ("diagnostics/render-invalidated-%d-buffers"):format(scale), "diagnostics", ("Invalidate and rebuild rendered diagnostics for %d populated workspace buffers"):format(scale), function() diagnostics.invalidate() return diagnostics.render(workspace) end ) diagnostics.invalidate() end) end local largest_scale = scales[#scales] with_diagnostic_fixture(largest_scale, 4, 0, function(workspace, buffers) run_case( ("diagnostics/count-cold-%d-buffers"):format(largest_scale), "diagnostics", "Invalidate cached paths before counting a populated workspace", function() for _, buffer in ipairs(buffers) do diagnostics.invalidate_buffer(buffer) end return diagnostics.count(workspace) end ) end) with_diagnostic_fixture(math.ceil(largest_scale / 2), 2, math.floor(largest_scale / 2), function(workspace) run_case( ("diagnostics/count-mixed-%d-buffers"):format(largest_scale), "diagnostics", "Scan an equally split mix of workspace and out-of-workspace buffers", function() return diagnostics.count(workspace) end ) end) run_case( "render/idle-direct", "render", "Call statusline.render() with one buffer, no diagnostics, and no LSP clients", statusline.render ) run_case( "render/idle-evaluated", "render", "Evaluate the installed statusline expression in Neovim", function() return vim.api.nvim_eval_statusline(plugin_expression, { winid = current_window }) end, { baseline = "baseline/eval-lua-callback" } ) local original_directory = vim.fn.getcwd(-1, 0) with_diagnostic_fixture(full_buffers, 4, math.floor(full_buffers / 4), function(workspace) vim.cmd("tcd " .. vim.fn.fnameescape(workspace)) local full_windows = { current_window } for _ = 2, window_count do vim.cmd("belowright split") full_windows[#full_windows + 1] = vim.api.nvim_get_current_win() end local saved_get_clients = vim.lsp.get_clients local saved_reg_recording = vim.fn.reg_recording vim.lsp.get_clients = function() return { { name = "lua_ls" }, { name = "excluded-ls" }, { name = "null-ls" }, { name = "copilot" }, } end vim.fn.reg_recording = function() return "q" end local full_ok, full_err = xpcall(function() run_case( "render/full-direct", "render", ("Render with %d workspace buffers, diagnostics, macro state, and four LSP clients"):format(full_buffers), statusline.render ) run_case( "render/full-evaluated", "render", "Evaluate the complete populated statusline through Neovim's statusline engine", function() return vim.api.nvim_eval_statusline(plugin_expression, { winid = current_window }) end, { baseline = "baseline/eval-lua-callback" } ) run_case( "baseline/eval-lua-callback-all-windows", "baseline", ("Evaluate the minimal Lua callback for %d visible windows"):format(window_count), function() local rendered for _, window in ipairs(full_windows) do rendered = vim.api.nvim_eval_statusline(noop_expression, { winid = window }) end return rendered end ) run_case( "render/full-all-windows-evaluated", "render", ("Simulate one populated statusline redraw across %d visible windows"):format(window_count), function() local rendered for _, window in ipairs(full_windows) do rendered = vim.api.nvim_eval_statusline(plugin_expression, { winid = window }) end return rendered end, { baseline = "baseline/eval-lua-callback-all-windows" } ) end, debug.traceback) vim.lsp.get_clients = saved_get_clients vim.fn.reg_recording = saved_reg_recording for index = #full_windows, 2, -1 do if vim.api.nvim_win_is_valid(full_windows[index]) then vim.api.nvim_win_close(full_windows[index], true) end end if vim.api.nvim_win_is_valid(current_window) then vim.api.nvim_set_current_win(current_window) end vim.cmd("tcd " .. vim.fn.fnameescape(original_directory)) if not full_ok then error(full_err) end end) run_case( "lifecycle/setup-default", "lifecycle", "Repeat the idempotent default setup path", function() return statusline.setup() end, { iterations = scaled(iterations, 10), warmup = scaled(warmup, 10), memory_iterations = scaled(memory_iterations, 10), } ) statusline.setup({ filetype_icons = { lua = "LUA" }, lsp_exclude = { "excluded-ls" }, }) local plugin_modules = { "statusline", "statusline.components.cursor", "statusline.components.cwd", "statusline.components.diagnostics", "statusline.components.filetype", "statusline.components.lsp", "statusline.components.macro", "statusline.components.mode", } run_case( "lifecycle/cold-require", "lifecycle", "Clear plugin modules and load all plugin Lua files from the filesystem cache", function() for _, module_name in ipairs(plugin_modules) do package.loaded[module_name] = nil end return require("statusline") end, { iterations = scaled(iterations, 20), warmup = scaled(warmup, 20), memory_iterations = scaled(memory_iterations, 20), } ) local by_name = {} for _, result in ipairs(results) do by_name[result.name] = result end for _, result in ipairs(results) do local baseline_result = result.baseline and by_name[result.baseline] if baseline_result then local baseline_median = baseline_result.stats.median_ns local median = result.stats.median_ns result.impact = { baseline = result.baseline, delta_ns = median - baseline_median, ratio = median / baseline_median, percent = (median - baseline_median) / baseline_median * 100, } end end local report = { schema_version = 1, kind = "statusline.nvim-runtime", metadata = benchmark.metadata(root, configuration), benchmarks = results, notes = { "Timings are batch averages; percentiles describe per-batch ns/op values.", "Evaluated cases include Neovim statusline parsing and Lua callback dispatch.", "Lua memory is heap growth with garbage collection paused, not total process allocation.", "LSP clients are mocked only in the explicitly populated component and full-render cases.", "Warm diagnostic-render cases reuse one formatted result; invalidated cases rebuild it every operation.", "The all-windows case reports the cost of one simulated redraw across every configured window.", }, } local lines = { "statusline.nvim runtime benchmarks", ("Neovim %s | %s | %s"):format( report.metadata.neovim, report.metadata.lua, report.metadata.cpu.model ), ("samples=%d iterations=%d warmup=%d scales=%s"):format( samples, iterations, warmup, table.concat(scales, ",") ), "", string.format("%-44s %11s %11s %11s %11s %11s %11s", "benchmark", "median", "p95", "min", "max", "rate", "heap/op"), string.rep("-", 121), } for _, result in ipairs(results) do local heap = result.memory and benchmark.format_bytes(result.memory.bytes_per_op) or "-" lines[#lines + 1] = string.format( "%-44s %11s %11s %11s %11s %11s %11s", result.name, benchmark.format_duration(result.stats.median_ns), benchmark.format_duration(result.stats.p95_ns), benchmark.format_duration(result.stats.min_ns), benchmark.format_duration(result.stats.max_ns), benchmark.format_rate(result.stats.ops_per_second), heap ) if result.impact then local delta_sign = result.impact.delta_ns >= 0 and "+" or "-" lines[#lines + 1] = (" vs %-39s %s%s (%.2fx, %+.1f%%)"):format( result.impact.baseline, delta_sign, benchmark.format_duration(math.abs(result.impact.delta_ns)), result.impact.ratio, result.impact.percent ) end end if #results == 0 then lines[#lines + 1] = "No benchmarks matched STATUSLINE_BENCH_FILTER." end lines[#lines + 1] = "" lines[#lines + 1] = "Percentiles are across batch averages. Re-run on an idle machine before comparing revisions." benchmark.write_report(report, table.concat(lines, "\n"), "STATUSLINE_BENCH_RUNTIME_OUTPUT") _G.statusline_benchmark_noop = nil end local ok, err = xpcall(main, debug.traceback) if not ok then vim.api.nvim_err_writeln(err) vim.cmd("cquit 1") else vim.cmd("qa!") end