From db32047232a2959146345b0c7e675a815bd78bd1 Mon Sep 17 00:00:00 2001 From: JINNOUCHI Yasushi Date: Sun, 25 Aug 2024 20:31:15 +0900 Subject: [PATCH] feat: use lazy.nvim to measure times if usable (#249) --- lua/frecency/database.lua | 10 +++++----- lua/frecency/database/table.lua | 6 +++--- lua/frecency/finder.lua | 12 +++++------- lua/frecency/klass.lua | 10 +++++----- lua/frecency/tests/frecency_spec.lua | 7 ------- lua/frecency/timer.lua | 28 +++++++++++++--------------- 6 files changed, 31 insertions(+), 42 deletions(-) diff --git a/lua/frecency/database.lua b/lua/frecency/database.lua index e6ff339..f9d2916 100644 --- a/lua/frecency/database.lua +++ b/lua/frecency/database.lua @@ -1,6 +1,6 @@ local Table = require "frecency.database.table" local FileLock = require "frecency.file_lock" -local Timer = require "frecency.timer" +local timer = require "frecency.timer" local config = require "frecency.config" local fs = require "frecency.fs" local watcher = require "frecency.watcher" @@ -186,7 +186,7 @@ end ---@async ---@return nil function Database:load() - local timer = Timer.new "load()" + timer.track "load() start" local err, data = self:file_lock():with(function(target) local err, stat = async.uv.fs_stat(target) if err then @@ -205,13 +205,13 @@ function Database:load() assert(not err, err) local tbl = vim.F.npcall(loadstring(data or "")) self.tbl:set(tbl) - timer:finish() + timer.track "load() finish" end ---@async ---@return nil function Database:save() - local timer = Timer.new "save()" + timer.track "save() start" local err = self:file_lock():with(function(target) self:raw_save(self.tbl:raw(), target) local err, stat = async.uv.fs_stat(target) @@ -220,7 +220,7 @@ function Database:save() return nil end) assert(not err, err) - timer:finish() + timer.track "save() finish" end ---@async diff --git a/lua/frecency/database/table.lua b/lua/frecency/database/table.lua index a7e41d2..5fbdf14 100644 --- a/lua/frecency/database/table.lua +++ b/lua/frecency/database/table.lua @@ -1,4 +1,4 @@ -local Timer = require "frecency.timer" +local timer = require "frecency.timer" local lazy_require = require "frecency.lazy_require" local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] @@ -48,13 +48,13 @@ end ---@async ---@return nil function Table:wait_ready() - local timer = Timer.new "wait_ready()" + timer.track "wait_ready() start" local t = 0.2 while not rawget(self, "is_ready") do async.util.sleep(t) t = t * 2 end - timer:finish() + timer.track "wait_ready() finish" end return Table diff --git a/lua/frecency/finder.lua b/lua/frecency/finder.lua index d6df044..e405e10 100644 --- a/lua/frecency/finder.lua +++ b/lua/frecency/finder.lua @@ -1,9 +1,9 @@ -local Timer = require "frecency.timer" local config = require "frecency.config" local fs = require "frecency.fs" local os_util = require "frecency.os_util" local recency = require "frecency.recency" local log = require "frecency.log" +local timer = require "frecency.timer" local lazy_require = require "frecency.lazy_require" local Job = lazy_require "plenary.job" --[[@as FrecencyPlenaryJob]] local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] @@ -257,21 +257,19 @@ end ---@return FrecencyFile[] function Finder:get_results(workspace, epoch) 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) - timer_fetch:finish() - local timer_results = Timer.new "making results" + timer.track "fetching finish" for _, file in ipairs(files) do file.score = file.ages and recency.calculate(file.count, file.ages) or 0 file.ages = nil end - timer_results:finish() + timer.track "making results" - local timer_sort = Timer.new "sorting" table.sort(files, function(a, b) return a.score > b.score or (a.score == b.score and a.path > b.path) end) - timer_sort:finish() + timer.track "sorting finish" return files end diff --git a/lua/frecency/klass.lua b/lua/frecency/klass.lua index 9120b6f..e633357 100644 --- a/lua/frecency/klass.lua +++ b/lua/frecency/klass.lua @@ -1,10 +1,10 @@ local Database = require "frecency.database" local Picker = require "frecency.picker" -local Timer = require "frecency.timer" local config = require "frecency.config" local fs = require "frecency.fs" local log = require "frecency.log" local recency = require "frecency.recency" +local timer = require "frecency.timer" local wait = require "frecency.wait" local lazy_require = require "frecency.lazy_require" local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]] @@ -27,13 +27,13 @@ end function Frecency:setup() ---@async local function init() - local timer = Timer.new "init()" + timer.track "init() start" self.database:start() self:assert_db_entries() if config.auto_validate then self:validate_database() end - timer:finish() + timer.track "init() finish" end local is_async = not not coroutine.running() @@ -48,7 +48,7 @@ end ---@param opts? FrecencyPickerOptions ---@return nil function Frecency:start(opts) - local timer = Timer.new "start()" + timer.track "start() start" log.debug "Frecency:start" opts = opts or {} if opts.cwd then @@ -64,7 +64,7 @@ function Frecency:start(opts) initial_workspace_tag = opts.workspace, }) self.picker:start(vim.tbl_extend("force", config.get(), opts)) - timer:finish() + timer.track "start() finish" end ---This can be calledBy `require("telescope").extensions.frecency.complete`. diff --git a/lua/frecency/tests/frecency_spec.lua b/lua/frecency/tests/frecency_spec.lua index 31128b3..5bdb144 100644 --- a/lua/frecency/tests/frecency_spec.lua +++ b/lua/frecency/tests/frecency_spec.lua @@ -2,7 +2,6 @@ -- https://github.com/nvim-lua/plenary.nvim/blob/663246936325062427597964d81d30eaa42ab1e4/lua/plenary/test_harness.lua#L86-L86 vim.opt.runtimepath:append(vim.env.TELESCOPE_PATH) -local Timer = require "frecency.timer" local util = require "frecency.tests.util" local log = require "plenary.log" @@ -184,7 +183,6 @@ describe("frecency", function() register(file, make_epoch "2023-07-29T00:00:00+09:00") log.new({}, true) end - local timer = Timer.new "all results" local results = vim.tbl_map(function(result) result.timestamps = nil return result @@ -192,11 +190,6 @@ describe("frecency", function() table.sort(results, function(a, b) return a.path < b.path 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() assert.are.same(expected, results) diff --git a/lua/frecency/timer.lua b/lua/frecency/timer.lua index ac39ae9..be3509f 100644 --- a/lua/frecency/timer.lua +++ b/lua/frecency/timer.lua @@ -1,26 +1,24 @@ local config = require "frecency.config" local log = require "frecency.log" -local uv = vim.uv or vim.loop ---@class FrecencyTimer ----@field elapsed number ----@field start integer ----@field title string -local Timer = {} - ----@param title string ----@return FrecencyTimer -Timer.new = function(title) - return setmetatable({ start = uv.hrtime(), title = title }, { __index = Timer }) -end +---@field has_lazy boolean? +local M = {} +---@param event string ---@return nil -function Timer:finish() +function M.track(event) if not config.debug then 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 - self.elapsed = (uv.hrtime() - self.start) / 1000000000 - log.debug(("[%s] takes %.3f seconds"):format(self.title, self.elapsed)) end -return Timer +return M