diff --git a/.emmyrc.json b/.emmyrc.json index 5b81d3f..c4cd027 100644 --- a/.emmyrc.json +++ b/.emmyrc.json @@ -32,7 +32,8 @@ "vector", "Ensure", "string", - "string_utf8" + "string_utf8", + "Profiler" ], "globalsRegex": [] }, diff --git a/mods/ENVIRONMENT/mapGenerator/src/asyncInit.lua b/mods/ENVIRONMENT/mapGenerator/src/asyncInit.lua index 62d5ab6..1b47241 100644 --- a/mods/ENVIRONMENT/mapGenerator/src/asyncInit.lua +++ b/mods/ENVIRONMENT/mapGenerator/src/asyncInit.lua @@ -17,6 +17,9 @@ dofile(core.get_modpath('smc__system__logger')..'/init.lua') -- Math dofile(core.get_modpath('smc__system__math')..'/init.lua') +-- Profile +dofile(core.get_modpath('smc__system__profiler')..'/init.lua') + -- Require dofile(core.get_modpath('smc__system__require')..'/init.lua') diff --git a/mods/SYSTEM/Profiler/init.lua b/mods/SYSTEM/Profiler/init.lua new file mode 100644 index 0000000..fcb6801 --- /dev/null +++ b/mods/SYSTEM/Profiler/init.lua @@ -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 diff --git a/mods/SYSTEM/Profiler/mod.conf b/mods/SYSTEM/Profiler/mod.conf new file mode 100644 index 0000000..c5da40e --- /dev/null +++ b/mods/SYSTEM/Profiler/mod.conf @@ -0,0 +1,2 @@ +name = smc__system__profiler +depends = smc__system__class, smc__system__table \ No newline at end of file diff --git a/mods/SYSTEM/SystemLoader/mod.conf b/mods/SYSTEM/SystemLoader/mod.conf index d6c65f9..1fc0588 100644 --- a/mods/SYSTEM/SystemLoader/mod.conf +++ b/mods/SYSTEM/SystemLoader/mod.conf @@ -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