开发者

measure various phases of code execution

Is there a way to measure the average time it takes my code to run and each frame to render for my Flex app? More specifically, I know how to use getTimer() but I'm not sure about which events I should listen to in order to do this. I was reading this post and am not sure how you'd figure out how long the actual rendering 开发者_运维问答took (it would seem like it may be the time between the RENDER event fires and the next ENTER_FRAME event fires, but I'm not sure). Also, not exactly sure where the user code happens, or whether I should care about EXIT_FRAME and FRAME_CONSTRUCTED.

Any help much appreciated!

EDIT ----

here's a snippet of code showing the main events for each repetition of the second frame in a super simple flex app. What I'm trying to figure out is whether there is a clear relationship between the "user code" and "rendering" parts of the classic Flex racetrack and the intervals between the four main signals that I'm tracing from.

The code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            import flash.utils.getTimer;

            public var t:Timer;
            protected function button1_clickHandler(event:MouseEvent):void
            {
                t = new Timer(40, 50);
                t.addEventListener(TimerEvent.TIMER, handleTimeTick);
                t.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
                addEventListener(Event.RENDER, application1_renderHandler);
                addEventListener(Event.ENTER_FRAME, application1_enterFrameHandler);
                addEventListener(Event.EXIT_FRAME, application1_exitFrameHandler);
                addEventListener(Event.FRAME_CONSTRUCTED, application1_frameConstructedHandler);
                t.start();
            }

            protected function handleTimeTick(e:TimerEvent):void
            {
                shape.x += 5;
            }

            protected function timerDone(e:TimerEvent):void
            {
                t.stop();
                t.removeEventListener(TimerEvent.TIMER, handleTimeTick);
                t.removeEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
                removeEventListener(Event.RENDER, application1_renderHandler);
                removeEventListener(Event.ENTER_FRAME, application1_enterFrameHandler);
                removeEventListener(Event.EXIT_FRAME, application1_exitFrameHandler);
                removeEventListener(Event.FRAME_CONSTRUCTED, application1_frameConstructedHandler);
            }

            protected function application1_renderHandler(event:Event):void
            {
                trace("render fire", getTimer());
            }

            protected function application1_enterFrameHandler(event:Event):void
            {
                trace("enter frame fire", getTimer());
            }

            protected function application1_exitFrameHandler(event:Event):void
            {
                trace("exit frame fire", getTimer());
            }

            protected function application1_frameConstructedHandler(event:Event):void
            {
                trace("frame constructed fire", getTimer());
            }

        ]]>
    </fx:Script>


    <s:Rect id="shape" x="0" y="0" height="20" width="20">
        <s:fill>
            <s:SolidColor color="0xff0000"/>
        </s:fill>
    </s:Rect>

    <s:Button x="10" y="100" click="button1_clickHandler(event)" label="go"/>
</s:Application>


Easy way to test out the time to takes for a "frame" to run. Here's a quick example. This is untested but you get the idea. You should also look at Grant Skinners talk about performance and framerates.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               xmlns:parsley="http://www.spicefactory.org/parsley"
               creationComplete="onCreationComplete()">
    <fx:Script> 
        <![CDATA[
            private var _timer:Timer = new Timer(1000);
            private var _previousTime:int;
            private var _avgTime:int;
            private var _times:Array = [];

            private function onCreationComplete():void
            {
                // Add event listener for enter frame
                this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
                // Listen to timer
                this._timer.addEventListener(TimerEvent.TIMER, onTimer);
                // start timer
                this._timer.start();
            }

            private function onEnterFrame(e:Event):void
            {
                var time:int = getTimer() - this._previousTime;
                trace("timer frame took "+ time +"ms");
                this._previousTime = getTimer();
                this._times.push(time);
            }   

            private function onTimer(e:Event):void
            {
                var total:int = 0;
                for(var i:uint = 0, len:uint = this._times.length; i < len ; i++)
                {
                    total += this._times[i];
                }
                this._avgTime = total/len;
                this._times = [];

                trace("Average frame time is "+ this._avgTime +"ms");
            }
        ]]>
    </fx:Script>
</s:Application>


Any Flex app is only two frames. The first frame is the startup/initialization of the framework and the second frame is your app.

The framerate is set on the Flex Application tag and the default is 24 frames per second. So, therefore it takes Flex 1/24 of a second to render the frames of your application.

However, it is entirely probably that your code takes more than one frame to execute, and I believe that is what you want to measure.

For some background reading, you should investigate the Flex Elastic Racetrack about how Flex divides each frame for different types of processing.

Then read up, if you haven't already, about the Flex Component LifeCycle. ( Flex 3 Version ) .

You already mentioned the getTimer() method. The gist is to use getTimer() at two points and compare the two values. What those two points are depends entirely on what you want to measure.

If you want to measure the time it takes a Flex component to go through the startup process, use getTimer() before you create it (AKA new Component() ) and then in a listener for that component's creationComplete event.

If you want to time the full application setup, you're best bet is to probably get the value in a preinitialize event handler of the main Application tag and on the applicationComplete handler of the same tag.

Does that help?


Ok, after reading plenty around the web I think this presentation gives the best info about how to measure elastic race track times (see for instance this piece of code from the presentation).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜