In the search for a better way to implement tracing, Dirk Eismann pointed out that Sandeep Gupta wrote flash.trace.Trace, something I have never heard of.
What it’s about
Unlike the name suggests flash.trace.Trace has nothing to do with trace, the function. In short: with Trace it is possible to record which methods are called with what certain parameters. This would allow to implement profiling or, and this is interesting for logging, we could find out more about the stack.To me it sounds too good to be true, lets see.
Does it work?
It does work. In the flash debug player from version 9. The runtime player just ignores the setup and does quietly run his rounds. It is possible to set the level and the output target for those statements. The output can be either to a function or to a file. For both ways it is possible to define a level that limits the input passed through. Here is a table of the things that are passed down to the method for the different levels that can be set.
| Level | File | Line # | Class & Method | Arguments |
|---|---|---|---|---|
METHODS |
0 | Main/doSomething | ||
METHODS_WITH_ARGS |
0 | Main/doSomething | “a”,”b”,”c” | |
METHODS_AND_LINES |
C:\…\src;;Main.as | 226 | Main/doSomething | “a”,”b”,”c” |
METHODS_AND_LINES_WITH_ARGS |
C:\…\src;;Main.as | 226 | Main/doSomething |
The file and line number is passed empty if the .swf is compiled with the flag -debug.
Performance
To test the performance I assembled a little test suite that covers the basic performance needs. It can be downloaded here. On my local machine I get following results in Flash Player 10, they do not differ too much from Flash Player 9.
| Level | Call/s. | Compared to OFF |
|---|---|---|
OFF |
2.200.000 | 100% |
METHODS |
900.000 | ~41% |
METHODS_WITH_ARGS |
350.000 | ~16% |
METHODS_AND_LINES |
260.000 | ~12% |
METHODS_AND_LINES_WITH_ARGS |
180 tsd. | ~8% |
Curiously there is a performance difference between compiling to with or without -debug:
| Level | Call/s. | Compared to OFF | Compared to -debug |
|---|---|---|---|
OFF |
9.800.000 | 100% | ~445% |
METHODS |
1.900.000 | ~29% | ~210% |
METHODS_WITH_ARGS |
560.000 | ~5% | ~160% |
And just to be complete: Trace does not consume any performance in the release version of the player, even compiled with "-debug".
Suitable for Logging?
Unfortunately Trace does not offer a stack depth. That means it is not possible to know where the logging call was called from, making it impossible to actually
utilize it for logging. Ah… dead-end. However: You could implement a homegrown profiler if you wanted to.
I hope you enjoyed my trip into performance depths.
yours
Martin.