Nov 19, 2008

Checking Performance Issue in Servoy

By the help of Debugging, we can make our code bug free, but it can't help us for checking performance issue which may creep into our code and Servoy does't offer any such tool to check or monitor these issues, such as which Servoy method called, and how many time it takes to finish the code etc.

Yes, a simple checking can be done by adding a timestamp at the method start and end.

Here is a nice solution for your above issue, suggested by Greg Pierce of Agile Tortoise. It requires no changes to your original code to track the timestamps, etc.

function profile_global_method()
{
var orig = arguments[0]; //Method name to test the performance
var newName = orig + '_orig';

if(typeof globals[newName] == 'function')
globals[orig] = globals[newName];
globals[newName] = globals[orig];

var pStart;
var before = function() { pStart = new Date(); };
var after = function() { application.output(orig + ":" + (new Date() - pStart)); };

var repl = function() {
switch(typeof before)
{
case 'string' : eval(before); break;
case 'function' : before(); break;
}
var result = globals[newName].apply(this, arguments);
switch(typeof after)
{
case 'string' : eval(after); break;
case 'function' : after(); break;
}
return result;
}
globals[orig] = repl;
}


You can profile your global method for performance testing by using the profile_global_method() and passing the method name as an argument. Now, you can call your method from somewhere else, typically in an "enter profiler mode" method somewhere that will call it repeated for the different methods you want to watch. It aliases the original global method in the runtime and replaces it with a version that tracks profiling ticks and outputs them to the console. No further modifications to your original code are required. Now, every time the method is called you can able to get the time consumed by the method like “methodName:[ticks]” in milliseconds. No, you can track the execution time into the db or keep counting the number of times the particular methods are called.

to Greg Pierce of Agile Tortoise for the same.

Here is an improved version which, now, can able to figure out the start, end and count of the method.

function profile_global_method()
{
var orig = arguments[0];
var newName = orig + '_orig';

if(typeof globals[newName] == 'function') globals[orig] = globals[newName];
globals[newName] = globals[orig];

if(typeof $profileData == 'undefined') $profileData = { };
if(typeof $profileData[orig] == 'undefined') $profileData[orig] = {count:0, lastStart:null, lastEnd:null, ticks:0};

var before = function() {
$profileData[orig].count = $profileData[orig].count + 1;
$profileData[orig].lastStart = new Date();
application.output("START | globals." + orig + " | count: " + $profileData[orig].count);
};
var after = function() {
$profileData[orig].lastEnd = new Date();
$profileData[orig].ticks = $profileData[orig].lastEnd - $profileData[orig].lastStart;
application.output('END | globals.' + orig + " | count: " + $profileData[orig].count + ", ticks: " + $profileData[orig].ticks);
};

var repl = function() {
before();
var result = globals[newName].apply(this, arguments);
after();
return result;
}
globals[orig] = repl;
}


Now, the console output will be like ...

START | method1 | count: 6
START | method1 | count: 7
END | method1 | count: 7, ticks: 10
END | method2 | count: 6, ticks: 50

The original post is at http://www.servoy.com/forum/viewtopic.php?f=22&t=11579.

0 comments: