How to derive time spent in a plugin's script

For developers using the Construct 2 Javascript SDK

Post » Mon Feb 06, 2017 6:13 pm

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 big fan of JavaScript.
B
76
S
20
G
76
Posts: 2,287
Reputation: 47,558

Post » Mon Feb 06, 2017 7:23 pm

A brute force way would be to just have a variable for the total time in the plugin space. aka here:
Code: Select all
/////////////////////////////////////
   // 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;
   };



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.

https://developer.mozilla.org/en-US/doc ... rmance/now

Here's a possible implementation:
Code: Select all
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();
};


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:
https://en.wikipedia.org/wiki/Kahan_summation_algorithm
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Mon Feb 06, 2017 7:33 pm

@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.
A big fan of JavaScript.
B
76
S
20
G
76
Posts: 2,287
Reputation: 47,558

Post » Mon Feb 06, 2017 7:51 pm

It should. You'd put the timing only in functions that either C2 calls:
viewtopic.php?f=154&t=123265&p=876933&hilit=tick2#p876933
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.
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Mon Feb 06, 2017 7:57 pm

R0J0hound wrote:It should. You'd put the timing only in functions that either C2 calls:
viewtopic.php?f=154&t=123265&p=876933&hilit=tick2#p876933
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 :)
A big fan of JavaScript.
B
76
S
20
G
76
Posts: 2,287
Reputation: 47,558


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests