Foundation.TimeSlotProfiler

Records time spent on repetitive operations (such as code in an update method, as example) and compiles aggregate statistics. The stats presented here are not guaranteed to be massively accurate, but are accurate enough to identify time-consuming code blocks.

To time a single-shot code block, use the native console.time() methods.


Example

// Create a logger instance (which buffers)
var profiler = new TimeSlotProfiler();

// Create a slot for a distinct code block
var randomCodeSlot = profiler.createSlot("Random Code");

// Start recording time
profiler.enterSlot(randomCodeSlot);

// Code to time here

// Stop recording time
profiler.leaveSlot(randomCodeSlot);

// Print stats on random code to the console
profiler.dumpSlotStats(randomCodeSlot);

To add profiling to custom types, the following code structure will be useful (as used in all profiled Foundation types)

// Extending a base type
class MyClass extends Foundation.Actor {

  // Slot number for this type's update method
  public static updateProfilerSlot: number = null;

  // Override the update method
  public update(): boolean {
    if (super.update()) {

      // Profiler start (change MyClass as appropriate)
      if (Game.instance.profiler){
        if (MyClass.updateProfilerSlot === null) {
          MyClass.updateProfilerSlot = Game.instance.profiler.createSlot('MyClass Update');
        }
        Game.instance.profiler.enterSlot(MyClass.updateProfilerSlot);
      }

      // ----------------------------
      // Your update code here
      // ----------------------------

      // Profiler stop (change MyClass as appropriate)
      if (Game.instance.profiler){
        Game.instance.profiler.leaveSlot(MyClass.updateProfilerSlot);
      }

      return true;
    } else {
      return false;
    }
  }
}

...

// Dump stats on all slots in the game profiler (including for MyClass)
// Include percentage of game loop consumed
game.profiler.dumpAllSlotStats(Game.loopProfilerSlot);

// Or just for MyClass. Include percentage of game loop consumed
game.profiler.dumpSlotStats(MyClass.updateProfilerSlot, Game.loopProfilerSlot);

Example output from a single slot dump:

Slot (1): GameTimeTracker Update
  Avg (ms): 0.005759320175475493
  Min (ms): 0
  Max (ms): 0.25499999999999545
  0.2120% of slot: Game Loop
  0.0346% of 60fps

Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/debugging/_digest.ts'
  • '/foundation/core/debugging/TimeSlotProfiler.ts'

Extends

This type does not extend a base type.

Implements

This type does not implement any interfaces