Profiler: create class and base functional. Close #36

This commit is contained in:
Van
2026-03-12 20:10:55 +03:00
parent c009493484
commit e359c4be09
5 changed files with 63 additions and 2 deletions

View 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

View File

@@ -0,0 +1,2 @@
name = smc__system__profiler
depends = smc__system__class, smc__system__table

View File

@@ -1,3 +1,3 @@
name = smc__system__system_loader
description = Include this mod in your `depends` to ensure all necessary Libs are loaded for your mod
depends = smc__system__ensure, smc__system__math, smc__system__mod, smc__system__require, smc__system__table, smc__system__string, smc__system__utf8, smc__system__class, smc__system__helper_lib
depends = smc__system__ensure, smc__system__math, smc__system__mod, smc__system__require, smc__system__table, smc__system__string, smc__system__utf8, smc__system__class, smc__system__helper_lib, smc__system__profiler