From 570fe0d21a1829629dcb018153a5e5576b1a503f Mon Sep 17 00:00:00 2001 From: Ruben Garcia Date: Tue, 17 Feb 2026 22:07:54 +0100 Subject: [PATCH] feat: Add optional metrics --- README.md | 12 ++++++++++-- lua/pytrize/jump/fixture.lua | 15 +++++++++++++++ lua/pytrize/rename.lua | 22 ++++++++++++++++++++-- lua/pytrize/settings.lua | 1 + 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 428c807..d04f647 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,11 @@ use { -- pytrize {{{ -- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'}, -- uncomment if you want to lazy load but not use the commands -- module = 'pytrize', - config = 'require("pytrize").setup()', + config = function() + require("pytrize").setup({ + -- metrics = true, -- uncomment to log timing info for jump and rename + }) + end, } -- }}} ``` @@ -68,7 +72,9 @@ For example using [`lazy.nvim`](https://github.com/folke/lazy.nvim): version = '*', dependencies = { 'nvim-lua/plenary' }, ft = 'python', -- Load only for python files - opts = {}, + opts = { + -- metrics = true, -- uncomment to log timing info for jump and rename + }, -- uncomment if you want to lazy load -- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'}, } -- }}} @@ -84,6 +90,7 @@ Requires [`plenary.nvim`](https://github.com/nvim-lua/plenary.nvim). { no_commands = false, highlight = 'LineNr', + metrics = false, preferred_input = 'telescope', } ``` @@ -92,6 +99,7 @@ where: - `no_commands` can be set to `true` and the commands `Pytrize` etc won't be declared. - `highlight` defines the highlighting used for the virtual text. +- `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. - `preferred_input` which method to query input to prefer (if it's installed), see the [Input](#input)-section below. ## Details diff --git a/lua/pytrize/jump/fixture.lua b/lua/pytrize/jump/fixture.lua index febcf1d..83f37cc 100644 --- a/lua/pytrize/jump/fixture.lua +++ b/lua/pytrize/jump/fixture.lua @@ -5,6 +5,9 @@ local open_file = require('pytrize.jump.util').open_file local paths = require('pytrize.paths') local ts_utils = require('pytrize.ts') +local function hrtime() return (vim.uv or vim.loop).hrtime() end +local function ms(t) return string.format('%.1fms', t / 1e6) end + M.to_declaration = function() local fixture = vim.fn.expand('') if fixture == '' then @@ -18,7 +21,10 @@ M.to_declaration = function() return end + local t0 = hrtime() local fixtures = ts_utils.build_fixture_index(filepath, root_dir) + local t_index = hrtime() + local location = fixtures[fixture] if location == nil then warn(string.format('fixture "%s" not found', fixture)) @@ -27,6 +33,15 @@ M.to_declaration = function() open_file(location.file) vim.api.nvim_win_set_cursor(0, {location.linenr, 0}) + + if require('pytrize.settings').settings.metrics then + local total = hrtime() - t0 + local index = t_index - t0 + vim.notify(string.format( + 'Pytrize jump: total=%s index=%s', + ms(total), ms(index) + ), vim.log.levels.INFO) + end end return M diff --git a/lua/pytrize/rename.lua b/lua/pytrize/rename.lua index b7296e4..5d378ec 100644 --- a/lua/pytrize/rename.lua +++ b/lua/pytrize/rename.lua @@ -5,6 +5,9 @@ local warn = require('pytrize.warn').warn local paths = require('pytrize.paths') local ts_utils = require('pytrize.ts') +local function hrtime() return (vim.uv or vim.loop).hrtime() end +local function ms(t) return string.format('%.1fms', t / 1e6) end + local function get_fixture_name() return vim.fn.expand('') end @@ -222,12 +225,16 @@ local function rename(old_name, new_name) return end + local t0 = hrtime() + local py_files = find_python_files(root_dir, old_name) if #py_files == 0 then warn(string.format('No Python files contain "%s"', old_name)) return end + local t_grep = hrtime() + -- First pass: determine which files to process. Only rename in files where -- the fixture resolves to the current file (not shadowed by a closer definition). local files_to_process = {} @@ -243,6 +250,8 @@ local function rename(old_name, new_name) end end + local t_scope = hrtime() + local total_replacements = 0 local files_changed = 0 @@ -277,13 +286,22 @@ local function rename(old_name, new_name) end end + local t_end = hrtime() + if total_replacements == 0 then warn(string.format('No fixture references found for "%s"', old_name)) else - vim.notify(string.format( + local msg = string.format( 'Pytrize: Renamed "%s" -> "%s" in %d file(s) (%d occurrence(s))', old_name, new_name, files_changed, total_replacements - ), vim.log.levels.INFO) + ) + if require('pytrize.settings').settings.metrics then + msg = msg .. string.format( + '\n total=%s grep=%s scoping=%s apply=%s', + ms(t_end - t0), ms(t_grep - t0), ms(t_scope - t_grep), ms(t_end - t_scope) + ) + end + vim.notify(msg, vim.log.levels.INFO) end end diff --git a/lua/pytrize/settings.lua b/lua/pytrize/settings.lua index 8f2c191..5395aad 100644 --- a/lua/pytrize/settings.lua +++ b/lua/pytrize/settings.lua @@ -6,6 +6,7 @@ local warn = require('pytrize.warn').warn M.settings = { no_commands = false, highlight = 'LineNr', + metrics = false, -- preferred_input = 'telescope', }