AS 3 Performance Monitor

Here’s a simple performance monitor you can add to your flash projects. You simply pass in a display object and an optional boolean. The boolean enables the auto start, by default it is set to true. You will notice the implicit setters and getters which gives you a bit of control of the monitor display. You can stop and start the monitor if need be. You will also notice when you hit a memory leak, it will warn and abort the monitor with messaging. Enjoy!
/***********************************
* MGonzalez
* www.stheory.com
*
*
* Usage:
* add the performance monitor to any displayObject
* example :
* var spr:Sprite = new Sprite()
* addChild(spr);
* var pDisplay = new PerformanceDisplay(spr);
*
***********************************/

package {
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.utils.Timer;
  import flash.events.TimerEvent;
  import flash.events.Event;
  import flash.utils.getTimer;
  import flash.system.System;

  public class PerformanceDisplay extends Sprite {

    private var _container:*;
    private var _memField:TextField;
    private var _fpsField:TextField;
    private var _tFieldColor:uint = 0xFFFFFF;
    private var _tFieldFontSize:uint = 10;
    private var _timer:Timer;
    private var _timerDuration:Number = 1000;
    private var _tempFps:Number = 0;
    private var _fps:Number = 0;
    private var _ms:int;
    private var _time:int;
    private var _tFormat:TextFormat;
    private var _warningMemory:uint = 1000*1000*500;
    private var _potentialCrashMemory:uint = 1000*1000*625;

    /////////////////// SETTERS & GETTERS ////////////////////////
    public function set TimerDur(inNum:Number):void {
      _timerDuration = inNum;
    }
    public function get TimerDur():Number {
      return _timerDuration;
    }
    public function set TextColor(inNum:uint):void {
      _tFieldColor = inNum;
      formatTextField();
    }
    public function get TextColor():uint {
      return _tFieldColor;
    }
    public function set FontSize(inNum:uint):void {
      _tFieldFontSize = inNum;
      formatTextField();
    }
    public function get FontSize():uint {
      return _tFieldFontSize;
    }
    /////////////////////////////////////////////////////////////////

    public function PerformanceDisplay(inDisplayObject:*,autoStart:Boolean = true) {
      _container = inDisplayObject;
      createMonitorFields();
      if (autoStart) {
        startMonitor();
      }
    }
    /*
    Method: startMonitor
    Parameters:
    Returns:
    */

    public function startMonitor():void {
      _timer = new Timer(_timerDuration);
      _timer.addEventListener(TimerEvent.TIMER, outputMemory);
      addEventListener(Event.ENTER_FRAME,outputFPS);
      _timer.start();
      _ms = getTimer();
      _fps=0;
      _tempFps = 0;
    }
    /*
    Method: stopMonitor
    Parameters:
    Returns:
    */

    public function stopMonitor():void {
      _timer.stop();
      _fps = 0;
      _tempFps = 0;
      _timer.removeEventListener(TimerEvent.TIMER, outputMemory);
      _timer = null;
      removeEventListener(Event.ENTER_FRAME,outputFPS);

    }
    /*
    Method: createMonitorFields
    Parameters:
    Returns:
    */

    private function createMonitorFields():void {
      _memField = new TextField();
      _memField.autoSize = TextFieldAutoSize.LEFT;
      _memField.selectable = false;
      _memField.text = "Mem : ";
      _fpsField = new TextField();
      _fpsField.autoSize = TextFieldAutoSize.LEFT;
      _fpsField.selectable = false;
      _fpsField.text ="Fps : " ;
      _container.addChild(_memField);
      _container.addChild(_fpsField);
      formatTextField();
    }
    /*
    Method: formatTextField
    Parameters:
    Returns:
    */

    private function formatTextField():void {
      _tFormat = new TextFormat();
      _tFormat.color = _tFieldColor;
      _tFormat.size = _tFieldFontSize;
      _memField.setTextFormat(_tFormat);
      _fpsField.setTextFormat(_tFormat);
      _fpsField.y = _memField.textHeight+5;

    }
    /*
    Method: outputMemory
    Parameters:
    Returns:
    */

    private function outputMemory(event:TimerEvent):void {
      var memory:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + ' Mb';
      _memField.text = "Mem : " + memory;
      _memField.setTextFormat(_tFormat);
      checkMemory();

    }
    /*
    Method: outputFPS
    Parameters:
    event:Event
    Returns:
    */

    private function outputFPS(event:Event):void {
      _time = getTimer()-1000;
      if (_time>_ms) {
        _fps=_tempFps+(_tempFps*.001*(_time-_ms));
        _ms=getTimer();
        _tempFps=0;
      } else {
        _tempFps++;
      }
      _fpsField.text = "FPS : " + Math.floor(_fps).toString();
      _fpsField.setTextFormat(_tFormat);
    }
    /*
    Method: checkMemory
    Parameters:
    Returns:
    */

    private function checkMemory():void {
      if (System.totalMemory > _warningMemory) {
        _memField.text = "WARNING!";
      } else if (System.totalMemory > _potentialCrashMemory) {
        _memField.text = "CRASHED!";
        stopMonitor();
      }
      _memField.setTextFormat(_tFormat);
    }

  }
}

Download Class
PerformanceDisplay.as (267)

One thought on “AS 3 Performance Monitor

  • November 11, 2011 at 8:18 am
    Permalink

    Thanks for this – really useful!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: