Skip to content

Commit 08aec67

Browse files
feat: Add optional metrics (#4)
1 parent 4c3a9da commit 08aec67

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ use { -- pytrize {{{
5656
-- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'},
5757
-- uncomment if you want to lazy load but not use the commands
5858
-- module = 'pytrize',
59-
config = 'require("pytrize").setup()',
59+
config = function()
60+
require("pytrize").setup({
61+
-- metrics = true, -- uncomment to log timing info for jump and rename
62+
})
63+
end,
6064
} -- }}}
6165
```
6266

@@ -68,7 +72,9 @@ For example using [`lazy.nvim`](https://github.com/folke/lazy.nvim):
6872
version = '*',
6973
dependencies = { 'nvim-lua/plenary' },
7074
ft = 'python', -- Load only for python files
71-
opts = {},
75+
opts = {
76+
-- metrics = true, -- uncomment to log timing info for jump and rename
77+
},
7278
-- uncomment if you want to lazy load
7379
-- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'},
7480
} -- }}}
@@ -84,6 +90,7 @@ Requires [`plenary.nvim`](https://github.com/nvim-lua/plenary.nvim).
8490
{
8591
no_commands = false,
8692
highlight = 'LineNr',
93+
metrics = false,
8794
preferred_input = 'telescope',
8895
}
8996
```
@@ -92,6 +99,7 @@ where:
9299

93100
- `no_commands` can be set to `true` and the commands `Pytrize` etc won't be declared.
94101
- `highlight` defines the highlighting used for the virtual text.
102+
- `metrics` when set to `true`, logs timing information via `vim.notify` after each jump-to-fixture and rename operation. Useful for understanding performance in large projects. The jump reports total time and index-build time; the rename reports total, grep, scoping (fixture resolution), and apply time.
95103
- `preferred_input` which method to query input to prefer (if it's installed), see the [Input](#input)-section below.
96104

97105
## Details

lua/pytrize/jump/fixture.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local open_file = require('pytrize.jump.util').open_file
55
local paths = require('pytrize.paths')
66
local ts_utils = require('pytrize.ts')
77

8+
local function hrtime() return (vim.uv or vim.loop).hrtime() end
9+
local function ms(t) return string.format('%.1fms', t / 1e6) end
10+
811
M.to_declaration = function()
912
local fixture = vim.fn.expand('<cword>')
1013
if fixture == '' then
@@ -18,7 +21,10 @@ M.to_declaration = function()
1821
return
1922
end
2023

24+
local t0 = hrtime()
2125
local fixtures = ts_utils.build_fixture_index(filepath, root_dir)
26+
local t_index = hrtime()
27+
2228
local location = fixtures[fixture]
2329
if location == nil then
2430
warn(string.format('fixture "%s" not found', fixture))
@@ -27,6 +33,15 @@ M.to_declaration = function()
2733

2834
open_file(location.file)
2935
vim.api.nvim_win_set_cursor(0, {location.linenr, 0})
36+
37+
if require('pytrize.settings').settings.metrics then
38+
local total = hrtime() - t0
39+
local index = t_index - t0
40+
vim.notify(string.format(
41+
'Pytrize jump: total=%s index=%s',
42+
ms(total), ms(index)
43+
), vim.log.levels.INFO)
44+
end
3045
end
3146

3247
return M

lua/pytrize/rename.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local warn = require('pytrize.warn').warn
55
local paths = require('pytrize.paths')
66
local ts_utils = require('pytrize.ts')
77

8+
local function hrtime() return (vim.uv or vim.loop).hrtime() end
9+
local function ms(t) return string.format('%.1fms', t / 1e6) end
10+
811
local function get_fixture_name()
912
return vim.fn.expand('<cword>')
1013
end
@@ -222,12 +225,16 @@ local function rename(old_name, new_name)
222225
return
223226
end
224227

228+
local t0 = hrtime()
229+
225230
local py_files = find_python_files(root_dir, old_name)
226231
if #py_files == 0 then
227232
warn(string.format('No Python files contain "%s"', old_name))
228233
return
229234
end
230235

236+
local t_grep = hrtime()
237+
231238
-- First pass: determine which files to process. Only rename in files where
232239
-- the fixture resolves to the current file (not shadowed by a closer definition).
233240
local files_to_process = {}
@@ -243,6 +250,8 @@ local function rename(old_name, new_name)
243250
end
244251
end
245252

253+
local t_scope = hrtime()
254+
246255
local total_replacements = 0
247256
local files_changed = 0
248257

@@ -277,13 +286,22 @@ local function rename(old_name, new_name)
277286
end
278287
end
279288

289+
local t_end = hrtime()
290+
280291
if total_replacements == 0 then
281292
warn(string.format('No fixture references found for "%s"', old_name))
282293
else
283-
vim.notify(string.format(
294+
local msg = string.format(
284295
'Pytrize: Renamed "%s" -> "%s" in %d file(s) (%d occurrence(s))',
285296
old_name, new_name, files_changed, total_replacements
286-
), vim.log.levels.INFO)
297+
)
298+
if require('pytrize.settings').settings.metrics then
299+
msg = msg .. string.format(
300+
'\n total=%s grep=%s scoping=%s apply=%s',
301+
ms(t_end - t0), ms(t_grep - t0), ms(t_scope - t_grep), ms(t_end - t_scope)
302+
)
303+
end
304+
vim.notify(msg, vim.log.levels.INFO)
287305
end
288306
end
289307

lua/pytrize/settings.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local warn = require('pytrize.warn').warn
66
M.settings = {
77
no_commands = false,
88
highlight = 'LineNr',
9+
metrics = false,
910
-- preferred_input = 'telescope',
1011
}
1112

0 commit comments

Comments
 (0)