ITADN
stevearc/profile.nvim
stevearc/profile.nvim · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

profile.nvim

一款用于 neovim 的 Lua 性能分析工具

警告: 这是一个通过直接修改所有 Lua 函数来实现的庞大临时解决方案。我创建它是为了个人使用,以便优化自己的插件,且不保证其稳定性、实用性,也不保证不会导致你的 neovim 崩溃。

快速开始

以下是一个默认配置,你可以将其复制/粘贴到你的 init.lua 文件的开头:

local should_profile = os.getenv("NVIM_PROFILE")
if should_profile then
  require("profile").instrument_autocmds()
  if should_profile:lower():match("^start") then
    require("profile").start("*")
  else
    require("profile").instrument("*")
  end
end

local function toggle_profile()
  local prof = require("profile")
  if prof.is_recording() then
    prof.stop()
    vim.ui.input({ prompt = "Save profile to:", completion = "file", default = "profile.json" }, function(filename)
      if filename then
        prof.export(filename)
        vim.notify(string.format("Wrote %s", filename))
      end
    end)
  else
    prof.start("*")
  end
end
vim.keymap.set("", "<f1>", toggle_profile)

如何使用它?
当您想要运行 Neovim 并对其性能进行分析时,先使用 NVIM_PROFILE=1 nvim 启动它。接着您可以按下 <F1> 开始生成性能分析文件,再次按下 <F1> 即可完成分析并将结果保存到文件中。如果您想分析 Neovim 的_启动_过程,则先执行 NVIM_PROFILE=start nvim,等 Neovim 加载完成后立即按下 <F1>

警告:性能分析文件可能会非常大!
这些性能分析文件会迅速变得极为庞大,且在导出之前会存储在内存中。这会导致 Neovim 的内存占用急剧上升,写入磁盘可能需要很长时间,甚至可能使 Neovim 崩溃,或是导致您使用的性能分析查看器崩溃。为避免这种情况,建议您进行_相对较短时间跨度_的性能分析,并尽可能针对您关心的特定模块进行分析。例如,与其使用 start("*"),不如使用 start("aerial*") 仅对我自己的 aerial.nvim 插件进行性能分析,这样会更好。

您可以在 chrome://tracing 中或通过 https://ui.perfetto.dev/ 查看这些性能分析文件。

如需了解该 API 的更多详细信息,请查看 profile.lua] 中的文档字符串。