ExecutionTime

The ExecutionTime module provides a simple mechanism for measuring the execution time of code blocks and flagging times which exceed a hard-coded threshold (20ms for development environments and 50ms otherwise). It leverages the ContextManager module to accomplish this in an ergonomic way.

Example

The following code is an example of how to use this module.

local MyModule = select(2, ...).MyModule
local ExecutionTime = MyModule:From("LibTSMUtil"):Include("Util.ExecutionTime")

for _ in ExecutionTime.WithMeasurement("Count to 1M") do
   local x = 0
   while x < 1000000 do
      x = x + 1
   end
end
-- Warning log: Count to 1M took 0.12345s

API

class Util.ExecutionTime: LibTSMModule
staticmethod CheckElapsed(
    elapsedTime: number,
    labelFormatString: string,
    ...: any
)

Checks the elapsed time and warns if it’s too long.

Parameters:
  • elapsedTime (number) – The elapsed time

  • labelFormatString (string) – The label format string to use for logging any warnings

  • ... (any) – Additional arguments for the label format string

staticmethod WithMeasurement(labelFormatString: string, ...: any): function, table, any

Returns an iterator which executes exactly once and measures the time taken within the loop body, warning if it’s too long.

Parameters:
  • labelFormatString (string) – The label format string to use for logging any warnings

  • ... (any) – Additional arguments for the label format string

staticmethod WithMeasurementAndRaisedLogStackLevel(
    raiseStackLevel: number,
    labelFormatString: string,
    ...: any
): function, table, any

Returns an iterator which executes exactly once and measures the time taken within the loop body, warning if it’s too long.

Parameters:
  • raiseStackLevel (number) – The amount to raise the stack level before logging

  • labelFormatString (string) – The label format string to use for logging any warnings

  • ... (any) – Additional arguments for the label format string