Skip to content

Commit

Permalink
chore: refactor benchmarking to use benchmark.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 22, 2024
1 parent 3fa3161 commit 5fa528f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ venv/
doc/tags
scripts/nvim_doc_tools
scripts/nvim-typecheck-action
tests/perf/
scripts/benchmark.nvim
perf/tmp/
profile.json
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,30 @@ fastlint: scripts/nvim_doc_tools venv

## profile: use LuaJIT profiler to profile the plugin
.PHONY: profile
profile:
nvim --clean -u tests/perf_harness.lua -c 'lua jit_profile()'
profile: scripts/benchmark.nvim
nvim --clean -u perf/bootstrap.lua -c 'lua jit_profile()'

## flame_profile: create a trace in the chrome profiler format
.PHONY: flame_profile
flame_profile:
nvim --clean -u tests/perf_harness.lua -c 'lua flame_profile()'
@echo "Visit https://ui.perfetto.dev/ and load the profile.json file"
flame_profile: scripts/benchmark.nvim
nvim --clean -u perf/bootstrap.lua -c 'lua flame_profile()'

## benchmark: benchmark performance opening directory with many files
.PHONY: benchmark
benchmark:
nvim --clean -u tests/perf_harness.lua -c 'lua benchmark(10)'
@cat tests/perf/benchmark.txt
benchmark: scripts/benchmark.nvim
nvim --clean -u perf/bootstrap.lua -c 'lua benchmark()'
@cat perf/tmp/benchmark.txt

scripts/nvim_doc_tools:
git clone https://github.com/stevearc/nvim_doc_tools scripts/nvim_doc_tools

scripts/nvim-typecheck-action:
git clone https://github.com/stevearc/nvim-typecheck-action scripts/nvim-typecheck-action

scripts/benchmark.nvim:
git clone https://github.com/stevearc/benchmark.nvim scripts/benchmark.nvim

## clean: reset the repository to a clean state
.PHONY: clean
clean:
rm -rf scripts/nvim_doc_tools scripts/nvim-typecheck-action venv .testenv tests/perf profile.json
rm -rf scripts/nvim_doc_tools scripts/nvim-typecheck-action venv .testenv perf/tmp profile.json
63 changes: 63 additions & 0 deletions perf/bootstrap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
vim.opt.runtimepath:prepend("scripts/benchmark.nvim")
vim.opt.runtimepath:prepend(".")

local bm = require("benchmark")
bm.sandbox()

---@module 'oil'
---@type oil.SetupOpts
local setup_opts = {
-- columns = { "icon", "permissions", "size", "mtime" },
}

local DIR_SIZE = tonumber(vim.env.DIR_SIZE) or 100000
local ITERATIONS = tonumber(vim.env.ITERATIONS) or 10
local WARM_UP = tonumber(vim.env.WARM_UP) or 1
local OUTLIERS = tonumber(vim.env.OUTLIERS) or math.floor(ITERATIONS / 10)
local TEST_DIR = "perf/tmp/test_" .. DIR_SIZE

vim.fn.mkdir(TEST_DIR, "p")
require("benchmark.files").create_files(TEST_DIR, "file %d.txt", DIR_SIZE)

function _G.jit_profile()
require("oil").setup(setup_opts)
local finish = bm.jit_profile({ filename = TEST_DIR .. "/profile.txt" })
bm.wait_for_user_event("OilEnter", function()
finish()
end)
require("oil").open(TEST_DIR)
end

function _G.flame_profile()
local start, stop = bm.flame_profile({
pattern = "oil*",
filename = "profile.json",
})
require("oil").setup(setup_opts)
start()
bm.wait_for_user_event("OilEnter", function()
stop(function()
vim.cmd.qall({ mods = { silent = true } })
end)
end)
require("oil").open(TEST_DIR)
end

function _G.benchmark()
require("oil").setup(setup_opts)
bm.run({ title = "oil.nvim", iterations = ITERATIONS, warm_up = WARM_UP }, function(callback)
bm.wait_for_user_event("OilEnter", callback)
require("oil").open(TEST_DIR)
end, function(times)
local avg = bm.avg(times, { trim_outliers = OUTLIERS })
local std_dev = bm.std_dev(times, { trim_outliers = OUTLIERS })
local lines = {
table.concat(vim.tbl_map(bm.format_time, times), " "),
string.format("Average: %s", bm.format_time(avg)),
string.format("Std deviation: %s", bm.format_time(std_dev)),
}

vim.fn.writefile(lines, "perf/tmp/benchmark.txt")
vim.cmd.qall({ mods = { silent = true } })
end)
end
122 changes: 0 additions & 122 deletions tests/perf_harness.lua

This file was deleted.

0 comments on commit 5fa528f

Please sign in to comment.