feat: use lazy.nvim to measure times if usable (#249)

This commit is contained in:
JINNOUCHI Yasushi 2024-08-25 20:31:15 +09:00 committed by GitHub
parent c140e6ff9c
commit db32047232
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 42 deletions

View File

@ -1,6 +1,6 @@
local Table = require "frecency.database.table" local Table = require "frecency.database.table"
local FileLock = require "frecency.file_lock" local FileLock = require "frecency.file_lock"
local Timer = require "frecency.timer" local timer = require "frecency.timer"
local config = require "frecency.config" local config = require "frecency.config"
local fs = require "frecency.fs" local fs = require "frecency.fs"
local watcher = require "frecency.watcher" local watcher = require "frecency.watcher"
@ -186,7 +186,7 @@ end
---@async ---@async
---@return nil ---@return nil
function Database:load() function Database:load()
local timer = Timer.new "load()" timer.track "load() start"
local err, data = self:file_lock():with(function(target) local err, data = self:file_lock():with(function(target)
local err, stat = async.uv.fs_stat(target) local err, stat = async.uv.fs_stat(target)
if err then if err then
@ -205,13 +205,13 @@ function Database:load()
assert(not err, err) assert(not err, err)
local tbl = vim.F.npcall(loadstring(data or "")) local tbl = vim.F.npcall(loadstring(data or ""))
self.tbl:set(tbl) self.tbl:set(tbl)
timer:finish() timer.track "load() finish"
end end
---@async ---@async
---@return nil ---@return nil
function Database:save() function Database:save()
local timer = Timer.new "save()" timer.track "save() start"
local err = self:file_lock():with(function(target) local err = self:file_lock():with(function(target)
self:raw_save(self.tbl:raw(), target) self:raw_save(self.tbl:raw(), target)
local err, stat = async.uv.fs_stat(target) local err, stat = async.uv.fs_stat(target)
@ -220,7 +220,7 @@ function Database:save()
return nil return nil
end) end)
assert(not err, err) assert(not err, err)
timer:finish() timer.track "save() finish"
end end
---@async ---@async

View File

@ -1,4 +1,4 @@
local Timer = require "frecency.timer" local timer = require "frecency.timer"
local lazy_require = require "frecency.lazy_require" local lazy_require = require "frecency.lazy_require"
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
@ -48,13 +48,13 @@ end
---@async ---@async
---@return nil ---@return nil
function Table:wait_ready() function Table:wait_ready()
local timer = Timer.new "wait_ready()" timer.track "wait_ready() start"
local t = 0.2 local t = 0.2
while not rawget(self, "is_ready") do while not rawget(self, "is_ready") do
async.util.sleep(t) async.util.sleep(t)
t = t * 2 t = t * 2
end end
timer:finish() timer.track "wait_ready() finish"
end end
return Table return Table

View File

@ -1,9 +1,9 @@
local Timer = require "frecency.timer"
local config = require "frecency.config" local config = require "frecency.config"
local fs = require "frecency.fs" local fs = require "frecency.fs"
local os_util = require "frecency.os_util" local os_util = require "frecency.os_util"
local recency = require "frecency.recency" local recency = require "frecency.recency"
local log = require "frecency.log" local log = require "frecency.log"
local timer = require "frecency.timer"
local lazy_require = require "frecency.lazy_require" local lazy_require = require "frecency.lazy_require"
local Job = lazy_require "plenary.job" --[[@as FrecencyPlenaryJob]] local Job = lazy_require "plenary.job" --[[@as FrecencyPlenaryJob]]
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
@ -257,21 +257,19 @@ end
---@return FrecencyFile[] ---@return FrecencyFile[]
function Finder:get_results(workspace, epoch) function Finder:get_results(workspace, epoch)
log.debug { workspace = workspace or "NONE" } log.debug { workspace = workspace or "NONE" }
local timer_fetch = Timer.new "fetching entries" timer.track "fetching start"
local files = self.database:get_entries(workspace, epoch) local files = self.database:get_entries(workspace, epoch)
timer_fetch:finish() timer.track "fetching finish"
local timer_results = Timer.new "making results"
for _, file in ipairs(files) do for _, file in ipairs(files) do
file.score = file.ages and recency.calculate(file.count, file.ages) or 0 file.score = file.ages and recency.calculate(file.count, file.ages) or 0
file.ages = nil file.ages = nil
end end
timer_results:finish() timer.track "making results"
local timer_sort = Timer.new "sorting"
table.sort(files, function(a, b) table.sort(files, function(a, b)
return a.score > b.score or (a.score == b.score and a.path > b.path) return a.score > b.score or (a.score == b.score and a.path > b.path)
end) end)
timer_sort:finish() timer.track "sorting finish"
return files return files
end end

View File

@ -1,10 +1,10 @@
local Database = require "frecency.database" local Database = require "frecency.database"
local Picker = require "frecency.picker" local Picker = require "frecency.picker"
local Timer = require "frecency.timer"
local config = require "frecency.config" local config = require "frecency.config"
local fs = require "frecency.fs" local fs = require "frecency.fs"
local log = require "frecency.log" local log = require "frecency.log"
local recency = require "frecency.recency" local recency = require "frecency.recency"
local timer = require "frecency.timer"
local wait = require "frecency.wait" local wait = require "frecency.wait"
local lazy_require = require "frecency.lazy_require" local lazy_require = require "frecency.lazy_require"
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
@ -27,13 +27,13 @@ end
function Frecency:setup() function Frecency:setup()
---@async ---@async
local function init() local function init()
local timer = Timer.new "init()" timer.track "init() start"
self.database:start() self.database:start()
self:assert_db_entries() self:assert_db_entries()
if config.auto_validate then if config.auto_validate then
self:validate_database() self:validate_database()
end end
timer:finish() timer.track "init() finish"
end end
local is_async = not not coroutine.running() local is_async = not not coroutine.running()
@ -48,7 +48,7 @@ end
---@param opts? FrecencyPickerOptions ---@param opts? FrecencyPickerOptions
---@return nil ---@return nil
function Frecency:start(opts) function Frecency:start(opts)
local timer = Timer.new "start()" timer.track "start() start"
log.debug "Frecency:start" log.debug "Frecency:start"
opts = opts or {} opts = opts or {}
if opts.cwd then if opts.cwd then
@ -64,7 +64,7 @@ function Frecency:start(opts)
initial_workspace_tag = opts.workspace, initial_workspace_tag = opts.workspace,
}) })
self.picker:start(vim.tbl_extend("force", config.get(), opts)) self.picker:start(vim.tbl_extend("force", config.get(), opts))
timer:finish() timer.track "start() finish"
end end
---This can be calledBy `require("telescope").extensions.frecency.complete`. ---This can be calledBy `require("telescope").extensions.frecency.complete`.

View File

@ -2,7 +2,6 @@
-- https://github.com/nvim-lua/plenary.nvim/blob/663246936325062427597964d81d30eaa42ab1e4/lua/plenary/test_harness.lua#L86-L86 -- https://github.com/nvim-lua/plenary.nvim/blob/663246936325062427597964d81d30eaa42ab1e4/lua/plenary/test_harness.lua#L86-L86
vim.opt.runtimepath:append(vim.env.TELESCOPE_PATH) vim.opt.runtimepath:append(vim.env.TELESCOPE_PATH)
local Timer = require "frecency.timer"
local util = require "frecency.tests.util" local util = require "frecency.tests.util"
local log = require "plenary.log" local log = require "plenary.log"
@ -184,7 +183,6 @@ describe("frecency", function()
register(file, make_epoch "2023-07-29T00:00:00+09:00") register(file, make_epoch "2023-07-29T00:00:00+09:00")
log.new({}, true) log.new({}, true)
end end
local timer = Timer.new "all results"
local results = vim.tbl_map(function(result) local results = vim.tbl_map(function(result)
result.timestamps = nil result.timestamps = nil
return result return result
@ -192,11 +190,6 @@ describe("frecency", function()
table.sort(results, function(a, b) table.sort(results, function(a, b)
return a.path < b.path return a.path < b.path
end) end)
timer:finish()
it("returns appropriate latency (<1.0 second)", function()
assert.are.is_true(timer.elapsed < 1.0)
end)
it("returns valid response", function() it("returns valid response", function()
assert.are.same(expected, results) assert.are.same(expected, results)

View File

@ -1,26 +1,24 @@
local config = require "frecency.config" local config = require "frecency.config"
local log = require "frecency.log" local log = require "frecency.log"
local uv = vim.uv or vim.loop
---@class FrecencyTimer ---@class FrecencyTimer
---@field elapsed number ---@field has_lazy boolean?
---@field start integer local M = {}
---@field title string
local Timer = {}
---@param title string
---@return FrecencyTimer
Timer.new = function(title)
return setmetatable({ start = uv.hrtime(), title = title }, { __index = Timer })
end
---@param event string
---@return nil ---@return nil
function Timer:finish() function M.track(event)
if not config.debug then if not config.debug then
return return
elseif M.has_lazy == nil then
M.has_lazy = (pcall(require, "lazy.stats"))
if not M.has_lazy then
log.debug "frecency.timer needs lazy.nvim"
end
end
if M.has_lazy then
require("lazy.stats").track("[telescope-frecency] " .. event)
end end
self.elapsed = (uv.hrtime() - self.start) / 1000000000
log.debug(("[%s] takes %.3f seconds"):format(self.title, self.elapsed))
end end
return Timer return M