-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc59c35
commit 0c82562
Showing
6 changed files
with
145 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
local log = require("fzfx.log") | ||
|
||
--- @class Profiler | ||
--- @field name string | ||
--- @field start_at {secs:integer,ms:integer} | ||
local Profiler = {} | ||
|
||
local default_millis_formatter = "%d.%03d s" | ||
local default_micros_formatter = "%d.%06d s" | ||
|
||
--- @param name string | ||
--- @return Profiler | ||
function Profiler:new(name) | ||
local secs, ms = vim.loop.gettimeofday() | ||
local o = { | ||
name = name, | ||
start_at = { | ||
secs = secs, | ||
ms = ms, | ||
}, | ||
} | ||
log.debug( | ||
"%s start at: " .. default_millis_formatter, | ||
name, | ||
secs, | ||
math.floor(ms / 1000) | ||
) | ||
setmetatable(o, self) | ||
self.__index = self | ||
return o | ||
end | ||
|
||
--- @param message string? | ||
--- @return integer | ||
function Profiler:elapsed_millis(message) | ||
local now_secs, now_ms = vim.loop.gettimeofday() | ||
local used_ms = (now_secs * 1000 + math.floor(now_ms / 1000)) | ||
- (self.start_at.secs * 1000 + math.floor(self.start_at.ms / 1000)) | ||
log.debug( | ||
"%s%s running at: " .. default_millis_formatter .. ", used: %d millis", | ||
self.name, | ||
(type(message) == "string" and string.len(message) > 0) | ||
and string.format("(%s)", message) | ||
or "", | ||
now_secs, | ||
math.floor(now_ms / 1000), | ||
used_ms | ||
) | ||
return used_ms | ||
end | ||
|
||
--- @param message string? | ||
--- @return integer | ||
function Profiler:elapsed_micros(message) | ||
local now_secs, now_ms = vim.loop.gettimeofday() | ||
local used_ms = (now_secs * 1000000 + now_ms) | ||
- (self.start_at.secs * 1000000 + self.start_at.ms) | ||
log.debug( | ||
"%s%s running at: " .. default_micros_formatter .. ", used: %d micros", | ||
self.name, | ||
(type(message) == "string" and string.len(message) > 0) | ||
and string.format("(%s)", message) | ||
or "", | ||
now_secs, | ||
now_ms, | ||
used_ms | ||
) | ||
return used_ms | ||
end | ||
|
||
local M = { | ||
Profiler = Profiler, | ||
} | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
local cwd = vim.fn.getcwd() | ||
|
||
describe("profiler", function() | ||
local assert_eq = assert.is_equal | ||
local assert_true = assert.is_true | ||
local assert_false = assert.is_false | ||
|
||
before_each(function() | ||
vim.api.nvim_command("cd " .. cwd) | ||
end) | ||
|
||
local Profiler = require("fzfx.profiler").Profiler | ||
describe("[Profiler]", function() | ||
it("creates", function() | ||
local p = Profiler:new("test1") | ||
print(string.format("profiler1:%s\n", vim.inspect(p))) | ||
assert_true(p.start_at.secs > 0) | ||
assert_true(p.start_at.ms >= 0 and p.start_at.ms < 1000000) | ||
end) | ||
it("elapsed", function() | ||
local p = Profiler:new("test2") | ||
print(string.format("profiler2:%s\n", vim.inspect(p))) | ||
assert_true(p.start_at.secs > 0) | ||
assert_true(p.start_at.ms >= 0 and p.start_at.ms < 1000000) | ||
assert_true(p:elapsed_millis() >= 0 and p:elapsed_millis() < 100) | ||
assert_true(p:elapsed_micros() >= 0 and p:elapsed_micros() < 100000) | ||
end) | ||
end) | ||
end) |