0 Favourites

How to derive time spent in a plugin's script

  • Hello! My plugin creation saga continues.... I would like to add feature that would return the time spent running the script inside a plugin. This would be available during normal runtime (rather than debug preview) so a dev can manage what they are doing with the plugin. The plugin is a behaviour and has a complex engine running in the background - ie there's more going on than the contents of the behinstProto.tick function and returning some expression values. I imagine that kahanTime will come to play, but I don't understand how to derive this value from it. Could someone please point me to a manual/tutorial or help, if you might know the answer?

    Thank you!!

  • A brute force way would be to just have a variable for the total time in the plugin space. aka here:

    /////////////////////////////////////
    	// Behavior type class
    	behaviorProto.Type = function(behavior, objtype)
    	{
    		this.behavior = behavior;
    		this.objtype = objtype;
    		this.runtime = behavior.runtime;
    		if (typeof this.behavior.totalTimeUsed == "undefined")
    			this.behavior.totalTimeUsed = 0;
    	};[/code:23ivohi0]
    
    Then you could just get the current time at the start of a function and right before it returns get the time again and add the difference to the total time.  You get the current high res time with performance.now().  The kahanTime gives you C2's "time" expression and is dt adjusted so it's not what you want.
    
    Here's a possible implementation:
    [code:23ivohi0]behinstProto.profileBegin = function ()
    {
    	this.ProfileStartTime = performance.now();
    };
    
    behinstProto.profileEnd = function ()
    {
    	this.behavior.totalTimeUsed += this.ProfileStartTime - performance.now();
    };
    
    behinstProto.tick = function ()
    {
    	this.profileBegin();
    	
    	if (!this.enabled)
    	{
    		this.profileEnd();
    		return;
    	}
    	// do stuff
    	this.profileEnd();
    };[/code:23ivohi0]
     
    Now adding lots of floating point numbers together will give rounding errors over time.  You can do this instead of a simple add to circumvent that if desired:
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • R0J0hound - thank you. I will give this a try, although I am not sure if it's what I think I need... Would it work in the case of a plugin like scirra's Physics - ie, is behinstProto.tick called before the rest of the plugin script models the world and, if so, how do I measure the end of the world's calculations? I guess I am struggling to visualise the relationship between the plugin script, the tick function and the c2.runtime (and, as I type, perhaps I need to dissect how that plugin's use of processing time is measured for the debugger). Cheers.

  • It should. You'd put the timing only in functions that either C2 calls:

    or any ACE functions. Helper functions only used by those other functions wouldn't need it. Also it wouldn't work if an action called another action inside the function.

    It sounds like it would be what your after since you want to know the total time used by a plugin. It is a bit tedious to do though.

  • It should. You'd put the timing only in functions that either C2 calls:

    or any ACE functions. Helper functions only used by those other functions wouldn't need it. Also it wouldn't work if an action called another action inside the function.

    It sounds like it would be what your after since you want to know the total time used by a plugin. It is a bit tedious to do though.

    Awesome, r0j0, I think that's exactly what I need. Thank you

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)