|
| 1 | +vim.fn.mkdir("tests/perf/.env", "p") |
| 2 | +local root = vim.fn.fnamemodify("./tests/perf/.env", ":p") |
| 3 | + |
| 4 | +for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do |
| 5 | + vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. name |
| 6 | +end |
| 7 | + |
| 8 | +vim.opt.runtimepath:prepend(vim.fn.fnamemodify(".", ":p")) |
| 9 | + |
| 10 | +---@module 'oil' |
| 11 | +---@type oil.SetupOpts |
| 12 | +local setup_opts = { |
| 13 | + -- columns = { "icon", "permissions", "size", "mtime" }, |
| 14 | +} |
| 15 | + |
| 16 | +local num_files = 100000 |
| 17 | + |
| 18 | +if not vim.uv.fs_stat(string.format("tests/perf/file %d.txt", num_files)) then |
| 19 | + vim.notify("Creating files") |
| 20 | + for i = 1, num_files, 1 do |
| 21 | + local filename = ("tests/perf/file %d.txt"):format(i) |
| 22 | + local fd = vim.uv.fs_open(filename, "a", 420) |
| 23 | + assert(fd) |
| 24 | + vim.uv.fs_close(fd) |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +local function wait_for_done(callback) |
| 29 | + vim.api.nvim_create_autocmd("User", { |
| 30 | + pattern = "OilEnter", |
| 31 | + once = true, |
| 32 | + callback = callback, |
| 33 | + }) |
| 34 | +end |
| 35 | + |
| 36 | +function _G.jit_profile() |
| 37 | + require("oil").setup(setup_opts) |
| 38 | + local outfile = "tests/perf/profile.txt" |
| 39 | + require("jit.p").start("3Fpli1s", outfile) |
| 40 | + local start = vim.uv.hrtime() |
| 41 | + require("oil").open("tests/perf") |
| 42 | + |
| 43 | + wait_for_done(function() |
| 44 | + local delta = vim.uv.hrtime() - start |
| 45 | + require("jit.p").stop() |
| 46 | + print("Elapsed:", delta / 1e6, "ms") |
| 47 | + vim.cmd.edit({ args = { outfile } }) |
| 48 | + end) |
| 49 | +end |
| 50 | + |
| 51 | +function _G.benchmark(iterations) |
| 52 | + require("oil").setup(setup_opts) |
| 53 | + local num_outliers = math.floor(0.1 * iterations) |
| 54 | + local times = {} |
| 55 | + |
| 56 | + local run_profile |
| 57 | + run_profile = function() |
| 58 | + -- Clear out state |
| 59 | + vim.cmd.enew() |
| 60 | + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do |
| 61 | + if vim.api.nvim_buf_is_valid(bufnr) and bufnr ~= vim.api.nvim_get_current_buf() then |
| 62 | + vim.api.nvim_buf_delete(bufnr, { force = true }) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + local start = vim.uv.hrtime() |
| 67 | + wait_for_done(function() |
| 68 | + local delta = vim.uv.hrtime() - start |
| 69 | + table.insert(times, delta / 1e6) |
| 70 | + if #times < iterations then |
| 71 | + vim.schedule(run_profile) |
| 72 | + else |
| 73 | + -- Remove the outliers |
| 74 | + table.sort(times) |
| 75 | + for _ = 1, num_outliers do |
| 76 | + table.remove(times, 1) |
| 77 | + table.remove(times) |
| 78 | + end |
| 79 | + |
| 80 | + local total = 0 |
| 81 | + for _, time in ipairs(times) do |
| 82 | + total = total + time |
| 83 | + end |
| 84 | + |
| 85 | + local lines = { |
| 86 | + table.concat( |
| 87 | + vim.tbl_map(function(t) |
| 88 | + return string.format("%dms", math.floor(t)) |
| 89 | + end, times), |
| 90 | + " " |
| 91 | + ), |
| 92 | + string.format("Average: %dms", math.floor(total / #times)), |
| 93 | + } |
| 94 | + vim.fn.writefile(lines, "tests/perf/benchmark.txt") |
| 95 | + vim.cmd.qall() |
| 96 | + end |
| 97 | + end) |
| 98 | + require("oil").open("tests/perf") |
| 99 | + end |
| 100 | + |
| 101 | + run_profile() |
| 102 | +end |
| 103 | + |
| 104 | +function _G.flame_profile() |
| 105 | + if not vim.uv.fs_stat("tests/perf/profile.nvim") then |
| 106 | + vim |
| 107 | + .system({ "git", "clone", "https://github.com/stevearc/profile.nvim", "tests/perf/profile.nvim" }) |
| 108 | + :wait() |
| 109 | + end |
| 110 | + vim.opt.runtimepath:prepend(vim.fn.fnamemodify("./tests/perf/profile.nvim", ":p")) |
| 111 | + local profile = require("profile") |
| 112 | + profile.instrument_autocmds() |
| 113 | + profile.instrument("oil*") |
| 114 | + |
| 115 | + require("oil").setup(setup_opts) |
| 116 | + profile.start() |
| 117 | + require("oil").open("tests/perf") |
| 118 | + wait_for_done(function() |
| 119 | + profile.stop("profile.json") |
| 120 | + vim.cmd.qall() |
| 121 | + end) |
| 122 | +end |
0 commit comments