feat(ui): added options to sort/filter profiling data

This commit is contained in:
Folke Lemaitre 2022-12-23 10:43:22 +01:00
parent fde5feea6d
commit 7dfb9c1f5c
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 75 additions and 5 deletions

View File

@ -5,6 +5,13 @@ local Config = require("lazy.core.config")
---@class LazyViewState
---@field mode string
---@field plugin? string
local default_state = {
mode = "home",
profile = {
threshold = 0,
sort_time_taken = true,
},
}
---@class LazyView
---@field buf number
@ -69,7 +76,7 @@ function M.create(opts)
opts = opts or {}
local self = setmetatable({}, { __index = M })
self.state = { mode = "home" }
self.state = vim.deepcopy(default_state)
self:mount()
@ -96,6 +103,33 @@ function M.create(opts)
end
end)
self:on_key("<C-s>", function()
if self.state.mode == "profile" then
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
self:update()
end
end)
self:on_key("<C-f>", function()
if self.state.mode == "profile" then
vim.ui.input({
prompt = "Enter time threshold in ms, like 0.5",
default = tostring(self.state.profile.threshold),
}, function(input)
if not input then
return
end
local num = input == "" and 0 or tonumber(input)
if not num then
Util.error("Please input a number")
else
self.state.profile.threshold = num
self:update()
end
end)
end
end)
self:setup_hover()
self:setup_modes()
return self

View File

@ -144,6 +144,7 @@ end
function M:help()
self:append("Help", "LazyH2"):nl():nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
self:append("Most properties can be hovered with ")
@ -459,6 +460,18 @@ end
function M:profile()
self:append("Profile", "LazyH2"):nl():nl()
self
:append("You can press ")
:append("<C-s>", "LazySpecial")
:append(" to change sorting between chronological order & time taken.")
:nl()
self
:append("Press ")
:append("<C-f>", "LazySpecial")
:append(" to filter profiling entries that took more time than a given threshold")
:nl()
self:nl()
local symbols = {
"",
"",
@ -466,21 +479,44 @@ function M:profile()
"",
}
---@param a LazyProfile
---@param b LazyProfile
local function sort(a, b)
return a.time > b.time
end
---@param entry LazyProfile
local function get_children(entry)
---@type LazyProfile[]
local children = entry
if self.view.state.profile.sort_time_taken then
children = {}
for _, child in ipairs(entry) do
children[#children + 1] = child
end
table.sort(children, sort)
end
return children
end
---@param entry LazyProfile
local function _profile(entry, depth)
if entry.time / 1e6 < self.view.state.profile.threshold then
return
end
local data = type(entry.data) == "string" and { source = entry.data } or entry.data
data.time = entry.time
local symbol = symbols[depth] or symbols[#symbols]
self:append((" "):rep(depth)):append(" " .. symbol, "LazySpecial"):append(" ")
self:append((" "):rep(depth)):append(symbol, "LazySpecial"):append(" ")
self:reason(data, { time_right = true })
self:nl()
for _, child in ipairs(entry) do
for _, child in ipairs(get_children(entry)) do
_profile(child, depth + 1)
end
end
for _, entry in ipairs(Util._profiles[1]) do
for _, entry in ipairs(get_children(Util._profiles[1])) do
_profile(entry, 1)
end
end