Something went wrong. Try again.
Neovim Statusline
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274local M = {}
local uv = vim.uv
local function percentile(sorted, fraction) local position = 1 + (#sorted - 1) * fraction local lower = math.floor(position) local upper = math.ceil(position) if lower == upper then return sorted[lower] end
local weight = position - lower return sorted[lower] * (1 - weight) + sorted[upper] * weightend
function M.summarize(values) assert(#values > 0, "cannot summarize an empty sample")
local sorted = vim.deepcopy(values) table.sort(sorted)
local total = 0 for _, value in ipairs(values) do total = total + value end local mean = total / #values
local squared_difference = 0 for _, value in ipairs(values) do local difference = value - mean squared_difference = squared_difference + difference * difference end
return { min = sorted[1], max = sorted[#sorted], mean = mean, median = percentile(sorted, 0.5), p95 = percentile(sorted, 0.95), p99 = percentile(sorted, 0.99), stddev = math.sqrt(squared_difference / #values), }end
local sink
function M.measure(fn, options) local iterations = assert(options.iterations) local samples = assert(options.samples) local warmup = assert(options.warmup) local memory_iterations = options.memory_iterations or 0
collectgarbage("collect") for _ = 1, warmup do sink = fn() end
local timings = {} for sample = 1, samples do local started = uv.hrtime() for _ = 1, iterations do sink = fn() end timings[sample] = (uv.hrtime() - started) / iterations end
local lua_heap_growth_per_op if memory_iterations > 0 then collectgarbage("collect") local before = collectgarbage("count") * 1024 collectgarbage("stop")
local ok, err = xpcall(function() for _ = 1, memory_iterations do sink = fn() end end, debug.traceback)
local after = collectgarbage("count") * 1024 collectgarbage("restart") collectgarbage("collect") if not ok then error(err) end
lua_heap_growth_per_op = math.max(0, after - before) / memory_iterations end
local statistics = M.summarize(timings) statistics.ops_per_second = statistics.median == 0 and math.huge or 1e9 / statistics.median
return { iterations = iterations, samples = samples, warmup = warmup, unit = "ns/op", samples_ns_per_op = timings, stats = { min_ns = statistics.min, max_ns = statistics.max, mean_ns = statistics.mean, median_ns = statistics.median, p95_ns = statistics.p95, p99_ns = statistics.p99, stddev_ns = statistics.stddev, ops_per_second = statistics.ops_per_second, }, memory = lua_heap_growth_per_op and { method = "Lua heap growth while garbage collection is paused", iterations = memory_iterations, bytes_per_op = lua_heap_growth_per_op, } or nil, }end
function M.positive_integer(name, default) local raw = vim.env[name] if raw == nil or raw == "" then return default end
local value = tonumber(raw) if not value or value < 1 or value ~= math.floor(value) then error(("%s must be a positive integer (got %q)"):format(name, raw)) end return valueend
function M.env_truthy(name) local value = vim.env[name] return value == "1" or value == "true" or value == "yes" or value == "on"end
function M.parse_scales(name, defaults) local raw = vim.env[name] if raw == nil or raw == "" then return defaults end
local scales = {} local seen = {} for part in raw:gmatch("[^,]+") do local value = tonumber(vim.trim(part)) if not value or value < 1 or value ~= math.floor(value) then error(("%s must be a comma-separated list of positive integers (got %q)"):format(name, raw)) end if not seen[value] then scales[#scales + 1] = value seen[value] = true end end
if #scales == 0 then error(name .. " must contain at least one scale") end table.sort(scales) return scalesend
local function run_command(command) local ok, process = pcall(vim.system, command, { text = true }) if not ok then return nil end return process:wait()end
local function revision(root) local jj = run_command({ "jj", "log", "-R", root, "-r", "@", "--no-graph", "-T", "commit_id.short(12)", }) if jj and jj.code == 0 then return vim.trim(jj.stdout), "jj" end
local git = run_command({ "git", "-C", root, "rev-parse", "--short=12", "HEAD" }) if git and git.code == 0 then return vim.trim(git.stdout), "git" end
return "unknown", "unavailable"end
function M.metadata(root, configuration) local version = vim.version() local uname = uv.os_uname() local cpus = uv.cpu_info() local commit, revision_source = revision(root)
return { generated_at_utc = os.date("!%Y-%m-%dT%H:%M:%SZ"), repository_root = root, revision = commit, revision_source = revision_source, neovim = ("%d.%d.%d"):format(version.major, version.minor, version.patch), lua = jit and jit.version or _VERSION, platform = { sysname = uname.sysname, release = uname.release, machine = uname.machine, }, cpu = { model = cpus[1] and cpus[1].model or "unknown", logical_cores = #cpus, }, configuration = configuration, }end
function M.format_duration(nanoseconds) local sign = nanoseconds < 0 and "-" or "" nanoseconds = math.abs(nanoseconds)
if nanoseconds < 1e3 then return sign .. ("%.1f ns"):format(nanoseconds) elseif nanoseconds < 1e6 then return sign .. ("%.2f us"):format(nanoseconds / 1e3) elseif nanoseconds < 1e9 then return sign .. ("%.2f ms"):format(nanoseconds / 1e6) end return sign .. ("%.2f s"):format(nanoseconds / 1e9)end
function M.format_bytes(bytes) if bytes < 1024 then return ("%.1f B"):format(bytes) elseif bytes < 1024 * 1024 then return ("%.2f KiB"):format(bytes / 1024) end return ("%.2f MiB"):format(bytes / (1024 * 1024))end
function M.format_rate(rate) if rate >= 1e6 then return ("%.2f M/s"):format(rate / 1e6) elseif rate >= 1e3 then return ("%.2f K/s"):format(rate / 1e3) end return ("%.1f /s"):format(rate)end
function M.write_report(report, human, output_env) local format = vim.env.STATUSLINE_BENCH_FORMAT or "human" if format ~= "human" and format ~= "json" then error("STATUSLINE_BENCH_FORMAT must be 'human' or 'json'") end
local rendered = format == "json" and vim.json.encode(report) or human vim.api.nvim_out_write(rendered .. (rendered:sub(-1) == "\n" and "" or "\n"))
local output = vim.env[output_env] if output and output ~= "" then local parent = vim.fn.fnamemodify(output, ":h") if parent ~= "." then vim.fn.mkdir(parent, "p") end local result = vim.fn.writefile(vim.split(rendered, "\n", { plain = true }), output) if result ~= 0 then error("failed to write benchmark report to " .. output) end endend
return M