Packagecom.gskinner.utils
Classpublic class PerformanceTest

This class represents a first attempt at creating a simple, but powerful performance testing harness for ActionScript 3. Its most important feature is testing and reporting on simple to write test suite classes. This is useful both for testing language features (ex. which loop structure runs fastest, what are the performance advantages of Vectors over Arrays), and for creating frameworks for testing the performance of projects as they are developed.

PerformanceTest instances maintain a queue, and will run multiple tests in order. Each method that is tested will be run asynchronously (iterations of the method test are run synchronously) unless .synchronous is set to true. The garbage collector is run in between each method test to provide better isolated results.
Note that it is generally good practice to wait a few seconds after your SWF loads to run performance tests. This ensures that the Flash player's start up and loading operations don't interfere with the test results.



Public Properties
 PropertyDefined by
  logger : Object
Specifies an object to handle logging results as they are generated.
PerformanceTest
  out : Function
Specifies a function to handle the text output from the default logging.
PerformanceTest
  paused : Boolean
Pauses or resumes the performance test queue.
PerformanceTest
  synchronous : Boolean = false
When synchronous is set to false (default) each method is tested in a separate frame to prevent the tests from freezing the UI or timing out.
PerformanceTest
Public Methods
 MethodDefined by
  
PerformanceTest
  
[static] This is a convenience function to allow you to access a single instance of PerformanceTest globally, so that you have a single test queue.
PerformanceTest
  
testFunction(testFunction:Function, iterations:uint = 1, name:String = "Function", description:String = null):void
Allows you to test the performance of a single function.
PerformanceTest
  
testRender(displayObject:DisplayObject, bounds:Rectangle = null, iterations:uint = 1, name:String = "Render", description:String = null):void
This method allows you to test the time it takes to render a complex display object.
PerformanceTest
  
testSuite(testSuite:Object, methods:Array = null, iterations:uint = 0, name:String = null, description:String = null):void
Tests a suite of methods.
PerformanceTest
  
unitTestFunction(targetTime:uint, testFunction:Function, iterations:uint = 1):Boolean
Runs a test function in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime.
PerformanceTest
  
unitTestRender(targetTime:uint, displayObject:DisplayObject, bounds:Rectangle = null, iterations:uint = 1):Boolean
Runs a test render in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime.
PerformanceTest
  
unitTestSuite(targetTime:uint, testSuite:Object, methods:Array = null, iterations:uint = 0):Boolean
Runs a test suite in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime.
PerformanceTest
Property detail
loggerproperty
public var logger:Object

Specifies an object to handle logging results as they are generated. This allows you to bypass the default text logging output, in order to store, chart or display the output differently. The logger object must expose 4 methods:

logBegin(name:String,description:String,iterations:uint)
Called when a new test begins.

logError(name:String,details:Error)
Called if an error occurs while testing a method. The details parameter will be null if the method was not found in the test suite, or will error object that was generated if an error occured while running the method.

logMethod(name:String, time:uint, iterations:uint, details:
Called after a method was tested successfully. The time parameter will be passed the total time for all iterations, calculate average with time/iterations. Currently, the the details parameter is only passed a value for "tare" methods - the number of times the tare method was run before returning consistent timing.

logEnd(name:String) Called when a test ends.

outproperty 
public var out:Function

Specifies a function to handle the text output from the default logging. You could use this to write the default output to a file, or display it in a text field. For example setting myPerformanceTest.out = trace; will cause the log output to be traced.

pausedproperty 
paused:Boolean  [read-write]

Pauses or resumes the performance test queue.

Implementation
    public function get paused():Boolean
    public function set paused(value:Boolean):void
synchronousproperty 
public var synchronous:Boolean = false

When synchronous is set to false (default) each method is tested in a separate frame to prevent the tests from freezing the UI or timing out. Setting synchronous to true forces tests to run immediately when added, and all within the same frame.

Constructor detail
PerformanceTest()constructor
public function PerformanceTest()
Method detail
getInstance()method
public static function getInstance():PerformanceTest

This is a convenience function to allow you to access a single instance of PerformanceTest globally, so that you have a single test queue.

Returns
PerformanceTest
testFunction()method 
public function testFunction(testFunction:Function, iterations:uint = 1, name:String = "Function", description:String = null):void

Allows you to test the performance of a single function. Handy for testing functions on the timeline.

Parameters
testFunction:Function — The function to test.
 
iterations:uint (default = 1) — The number of times to run the function. More iterations will take longer to run, but will result in a more consistent result.
 
name:String (default = "Function") — The name to use when logging this test.
 
description:String (default = null) — The description to use when logging this test.
testRender()method 
public function testRender(displayObject:DisplayObject, bounds:Rectangle = null, iterations:uint = 1, name:String = "Render", description:String = null):void

This method allows you to test the time it takes to render a complex display object. This is a largely untested feature in this version of PerformanceSuite.

Parameters
displayObject:DisplayObject — A DisplayObject to test rendering times for. For example you could test the render time of a display object with complex vectors or filters.
 
bounds:Rectangle (default = null) — Specifies the area of the display object to render. For example, you might want to limit the render to the area that would be visible on the stage at runtime. If bounds is not specified, it will use the bounds of the display object.
 
iterations:uint (default = 1) — The number of times to run the render. More iterations will take longer to run, but will result in a more consistent result.
 
name:String (default = "Render") — The name to use when logging this test.
 
description:String (default = null) — The description to use when logging this test.
testSuite()method 
public function testSuite(testSuite:Object, methods:Array = null, iterations:uint = 0, name:String = null, description:String = null):void

Tests a suite of methods. The suite can be any class instance with public methods. The suite object can optionally expose name, description, and methods properties that will be used if the corresponding parameters are not specified. The suite can also expose a tare method (see below for info).

A test suite should group similar tests together (ex. testing different loop structures), and each test method should run for a significant amount of time (because testing methods that run for only a few ms is unreliable). You can use a loop inside of your test methods to make simple operations run longer.

Similar to unit testing, you can write test suites alongside your main project files, and have the test suite methods call methods in your project to test them. In this way you can create an evolving performance testing framework without having to modify your project source code.

See the samples for more information on writing test suites.

Tare methods
If a test suite exposes a public method called tare, it will be run repeatedly (up to 6 times) at the beginning of the suite until it returns a consistent timing result. That time will then be subtracted from the results of all other tests. This is useful for accounting for "infrastructure costs".

For example, if you have a suite of tests to test mathematical operations, and every test has a loop to repeat the operation 100000 times (to get measureable results), you could write a tare method that contains an empty loop that repeats 100000 times, to eliminate the time required to run the loop from your results.

Parameters
testSuite:Object — The test suite instance to test.
 
methods:Array (default = null) — An array of method names to test. If null, the testSuite will be introspected, and all of its public methods will be tested (except those whose names begin with an underscore).
 
iterations:uint (default = 0) — The number of times to run each method. More iterations will take longer to run, but will result in a more consistent result.
 
name:String (default = null) — The name to use when logging this test.
 
description:String (default = null) — The description to use when logging this test.
unitTestFunction()method 
public function unitTestFunction(targetTime:uint, testFunction:Function, iterations:uint = 1):Boolean

Runs a test function in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime. This is useful for developing unit tests for performance. For instance, the following example would fail the unit test if myFunction took longer than 100ms to run on average per iteration:
assertTrue(PerformanceTest.getInstance().unitTestFunction(100, myFunction));

Parameters
targetTime:uint — The target time in milliseconds for this test. If the test takes longer to run, it will return false indicating the test was failed.
 
testFunction:Function — The function to test.
 
iterations:uint (default = 1) — The number of times to run the function. More iterations will take longer to run, but will result in a more consistent result.

Returns
Boolean
unitTestRender()method 
public function unitTestRender(targetTime:uint, displayObject:DisplayObject, bounds:Rectangle = null, iterations:uint = 1):Boolean

Runs a test render in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime. This is useful for developing unit tests for performance. For instance, the following example would fail the unit test if mySprite took longer than 100ms to render on average per iteration:
assertTrue(PerformanceTest.getInstance().unitTestRender(100, mySprite));

Parameters
targetTime:uint — The target time in milliseconds for this test. If the test takes longer to run, it will return false indicating the test was failed
 
displayObject:DisplayObject — A DisplayObject to test rendering times for. For example you could test the render time of a display object with complex vectors or filters.
 
bounds:Rectangle (default = null) — Specifies the area of the display object to render. For example, you might want to limit the render to the area that would be visible on the stage at runtime. If bounds is not specified, it will use the bounds of the display object.
 
iterations:uint (default = 1) — The number of times to run the render. More iterations will take longer to run, but will result in a more consistent result.

Returns
Boolean
unitTestSuite()method 
public function unitTestSuite(targetTime:uint, testSuite:Object, methods:Array = null, iterations:uint = 0):Boolean

Runs a test suite in synchronous mode and returns false if the average time (accounting for number of iterations) is greater than the specified targetTime. This is useful for developing unit tests for performance. For instance, the following example would fail the unit test if mySuite took longer than 100ms to run on average per iteration:
assertTrue(PerformanceTest.getInstance().unitTestSuite(100, mySuite));

Parameters
targetTime:uint — The target time in milliseconds for this test. If the test takes longer to run, it will return false indicating the test was failed
 
testSuite:Object — The test suite instance to test.
 
methods:Array (default = null) — An array of method names to test. If null, the testSuite will be introspected, and all of its public methods will be tested (except those whose names begin with an underscore).
 
iterations:uint (default = 0) — The number of times to run each method. More iterations will take longer to run, but will result in a more consistent result.

Returns
Boolean