Skip to content
Mar 2 2010 / alecmce

Introducing RacetrackStats v.0.0.1

Introduction

RacetrackStats is an in-Flash performance profiler that gives real-time feedback about how the code it is contained within is performing. It can be found at GitHub:

http://github.com/alecmce/RacetrackStats

RacetrackStats is inspired by and seeks to move forward from Mr Doob‘s Stats package (http://code.google.com/p/mrdoob/wiki/stats)

In Action

Below is the first alpha of RacetrackStats in action.

source: http://github.com/alecmce/RacetrackStats/blob/master/test/demo/RacetrackStatsDemo.as

The SWF comprises three small animations (from left to right):

  • A mixture of computation and rendering: A simple BitmapData-driven particle system attempting to model smoke using 100000 pixels and a heavy glow filter and blur filter;
  • Heavy computation: A prime-factorisation algorithm, which computes 24 * 100 prime factor decompositions per frame. As it continues to run, the numbers get larger which leads to heavier calculation cycles;
  • Extremely heavy rendering: A cycadelic circle pattern that has hundreds of semi-transparent circular sprites moving around and overlapping with each other.

The ‘Elastic Racetrack’

RacetrackStats attempts to go beyond Mr Doob’s Stats by measuring how the elastic racetrack is being stretched. The Elastic Racetrack was a phrase coined to describe how the original AVM1 Flash Player segmented each second into frames and attempted to balance the requirements of code, refreshing the screen and doing work like garbage collection. The AVM2 player’s model is sufficiently similar, that the name remains appropriate.

Code Execution

It does this by hooking into the Event.ENTER_FRAME and Event.RENDER events to record when portions of the code start and end. By recording when the ENTER_FRAME starts and when the RENDER starts, the intervening time is taken to be code execution.

Pre-Render Code Execution

Commonly this part of the code cycle is almost unused. If you exploit stage.invalidate and Event.RENDER in order for your classes to execute code after the main code execution has completed, then it will be captured here, between the top-priority Event.RENDER and the bottom-priority Event.RENDER calls.

Rendering

A problem is, the time between the RENDER ending and the ENTER_FRAME starting is used to draw the screen but if time is left over in which the Flash Player has nothing to do, it will idle during this time also. To counteract this, RacetrackStats attempts to throttle the FlashPlayer so that the application is always running as fast as it can, minimising, or even eliminating that idle time. I call this the “render” portion – the “Event.RENDER” event is badly named – screen rendering happens after the RENDER event has finished.

Work To Do

As soon as you add weight like this to an application, the speed at which it runs changes. By measuring the speed, you change the speed. It is the classic scientific paradox. It is mitigated by shrinking down the weight of the stats package as far as possible. At the moment, functionality rather than size is the chief concern, though I have tried to minimize the package by avoiding embedding fonts, and so forth.

I would love to hear what you think about the package, whether positive or negative. I’d also really appreciate it if you use it in your applications. If you have any problems, questions, queries or comments, please leave a comment below, or open an issue on GitHub.

  • http://play.blog2t.net/ Og2t

    Nice one Alec, I’ve been looking for Mr Doob’s stats alternative as well, not because I don’t like it (I still use it) but rather the way the graphs are presented.
    I just read your “TOTHINKABOUT” in GitHub project notes and reminded myself of writing a class that handles that “magic keyword” functionality. Please have a look if you’re interested http://gist.github.com/319402

    • http://alecmce.com Alec McEachran

      Thanks Tomek, I’ve had a quick read-through of your code. It’s exactly what I meant; I will use that as a guide for my own implementation. Thanks.

  • http://twitter.com/miketwoface the.twoface

    Thanks for this tool, it can be useful in future. But unfortunately my current project is AS2 one. So maybe you know some tool for AS2?

    • http://alecmce.com Alec McEachran

      I am really sorry Mike, I am not aware of any stats packages for AS2. Good luck with your search. I hope you come back and have another look at this once you’re working in AS3.

  • http://play.blog2t.net/ Og2t

    Nice one Alec, I've been looking for Mr Doob's stats alternative as well, not because I don't like it (I still use it) but rather the way the graphs are presented.
    I just read your “TOTHINKABOUT” in GitHub project notes and reminded myself of writing a class that handles that “magic keyword” functionality. Please have a look if you're interested http://gist.github.com/319402

  • http://twitter.com/miketwoface the.twoface

    Thanks for this tool, it can be useful in future. But unfortunately my current project is AS2 one. So maybe you know some tool for AS2?

  • http://jpauclait.net/ jpauclair

    This is really cool!
    Can you make a version of it for the PreLoadSWF mm.cfg feature?
    http://tinyurl.com/y8zl8rz
    If you don’t I will! :D
    You can grab my project and change my little profiler with your nice one :)

    • http://alecmce.com Alec McEachran

      Hi JP,

      First of all, thanks! It’s early days for the project, but I wanted people to look at it because they would spot flaws and potential extensions…

      …and you’ve just done exactly that. It’s a great idea. I will look back over your blog post and see what I can use. Or you could fork my project, if you like :)

      The only problem I can foresee is that your debugger requires a debug swf. One of the nice things about these inline profilers is that they allow you to bundle them into release code and check that nothing significant changes between debug and inline. Still, perhaps it is worthwhile having two flavours of stats monitors? One for debug, the other for release.

      • http://jpauclair.net/ jpauclair

        I’m not sure I understand correctly, the debug.SWF code used by PreloadSWF command is loaded at run time into the debug or release version of any other SWF running. there is no code modification to do with the project your profiling! ex, you could go and profile youtube with your profiler without youtube even knowing it!
        I’ll make something… maybee tonight. I really like what you did!

        • http://alecmce.com Alec McEachran

          Ah, I misunderstood. Even better. I look forward to seeing what you come up with!

  • http://jpauclait.net/ jpauclair

    This is really cool!
    Can you make a version of it for the PreLoadSWF mm.cfg feature?
    http://tinyurl.com/y8zl8rz
    If you don't I will! :D
    You can grab my project and change my little profiler with your nice one :)

  • http://alecmce.com alecmce

    Hi JP,

    First of all, thanks! It's early days for the project, but I wanted people to look at it because they would spot flaws and potential extensions…

    …and you've just done exactly that. It's a great idea. I will look back over your blog post and see what I can use. Feel free to fork the project and use my base to do it yourself, if you like :)

  • http://alecmce.com alecmce

    Thanks Tomek, I've had a quick read-through of your code. It's exactly what I meant; I will use that as a guide for my own implementation. Thanks.

  • http://alecmce.com alecmce

    I am really sorry Mike, I am not aware of any stats packages for AS2. Good luck with your search. I hope you come back and have another look at this once you're working in AS3.

  • http://jpauclair.net/ jpauclair

    I'm not sure I understand correctly, the debug.SWF code used by PreloadSWF command is loaded at run time into the debug or release version of any other SWF running. there is no code modification to do with the project your profiling! ex, you could go and profile youtube with your profiler without youtube even knowing it!
    I'll make something… maybee tonight. I really like what you did!

  • http://alecmce.com alecmce

    Ah, I misunderstood. Even better. I look forward to seeing what you come up with!

  • http://alecmce.com Alec McEachran

    I’ve opened up a few bugs on the basis of this demo, here:

    http://github.com/alecmce/RacetrackStats/issues

    If you have any others, please go there and add them.

  • http://alecmce.com alecmce

    I've opened up a few bugs on the basis of this demo, here:

    http://github.com/alecmce/RacetrackStats/issues

    If you have any others, please go there and add them.