Profiler: create class and base functional. Close #36
This commit is contained in:
55
mods/SYSTEM/Profiler/init.lua
Normal file
55
mods/SYSTEM/Profiler/init.lua
Normal file
@@ -0,0 +1,55 @@
|
||||
local getInstance = Class.getInstance
|
||||
local getUsTime = core.get_us_time
|
||||
|
||||
---@alias Profiler.TimeUnits 'sec'|'us'
|
||||
|
||||
---@class Profiler
|
||||
---@field name string
|
||||
---@field startTime number
|
||||
---@field endTime number
|
||||
---@field deltaTime number
|
||||
Profiler = {
|
||||
timers = {}
|
||||
}
|
||||
|
||||
---@param name string?
|
||||
---@return Profiler
|
||||
function Profiler:new(name)
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name or 'NoName'
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Marks the time when the timer starts.
|
||||
function Profiler:startTimer()
|
||||
self.startTime = getUsTime()
|
||||
self.deltaTime = nil
|
||||
self.endTime = nil
|
||||
end
|
||||
|
||||
---Marks the end time of the timer.
|
||||
function Profiler:endTimer()
|
||||
self.endTime = getUsTime()
|
||||
|
||||
self.deltaTime = self.endTime - self.startTime
|
||||
end
|
||||
|
||||
---Returns the time difference between the start and stop of the timer in seconds (`sec`) or microseconds (`us`).
|
||||
---@param units Profiler.TimeUnits? Units of time measurement. Default 'us'.
|
||||
function Profiler:getDeltaTime(units)
|
||||
if units == nil then
|
||||
units = 'us'
|
||||
end
|
||||
|
||||
if units == 'us' then
|
||||
return self.deltaTime
|
||||
elseif units == 'sec' then
|
||||
return self.deltaTime/1000000
|
||||
end
|
||||
end
|
||||
|
||||
---Displays the time difference in the chat using `core.log`.
|
||||
function Profiler:logDeltaTime()
|
||||
core.log(('DeltaTime[%s]: %.3f'):format(self:getDeltaTime()))
|
||||
end
|
||||
Reference in New Issue
Block a user