[Scripting] How to measure the execution time of a server-side code?

Community Alums
Not applicable

Hi everyone,

In a script includes, I want to measure the execution time of a piece of code.
I use this method :

var start = new GlideDateTime().getNumericValue();
var result = this.myFunction();
var executionTime = this.calculateExecutionTime(start, new GlideDateTime().getNumericValue(), true);


/**
* Funtion to calculate duration between two timestamps
* @param {GlideDateTime} start DateTime object which holds value of Starting timestamp
* @param {GlideDateTime} end DateTime Object which holds value of ending timestamp
* @param {boolean} displayInSeconds Boolean variable which decides whether to return the difference in Seconds (if true) or Minutes and seconds
* @return {string} the time difference in seconds if "displayInSeconds" is "true" else returns difference in Minutes and seconds.
*/
calculateExecutionTime: function (start, end, displayInSeconds) {
	var startTime = start;
	var endTime = end;
	var difference;
	if (!displayInSeconds) {
		difference = gs.dateDiff(startTime, endTime, true);
		return Math.floor(difference / 60) + " minutes, " + Math.ceil(difference % 60) + " seconds";
	} else {

		difference = end - start;
		difference = difference + "";
		if (difference.length > 3)
			return difference.substr(0, difference.length - 3) + " " + difference.substr(difference.length - 3) + "ms";
		else if (difference == '0')
			return "N/A";
		else
			return difference + "ms";
	}
},


Do you know of a simpler and cleaner method to do this?

Thanks in advance

Chris

 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can directly use GlideDateTime subtract method

something like this

var start = new GlideDateTime();
var result = this.myFunction();
var end = new GlideDateTime();

var dur = GlideDateTime.subtract(start, end);
gs.info("Duration is" + dur.getDisplayValue());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Pedro Grilo1
Mega Sage

Hi!

 

I believe you can use the subtract method and get the difference between the two GlideDateTime.

Something like the below will give you the difference, in ms, between start and end:

var start = new GlideDateTime('2022-05-30 00:00:00');
var end = new GlideDateTime();
GlideDateTime.subtract(start, end).getNumericValue();

 

Hope it helps!

Pedro

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can directly use GlideDateTime subtract method

something like this

var start = new GlideDateTime();
var result = this.myFunction();
var end = new GlideDateTime();

var dur = GlideDateTime.subtract(start, end);
gs.info("Duration is" + dur.getDisplayValue());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

Thanks Ankur for the proposition.

I'm using the getByFormat() method for display milliseconds...

var start = new GlideDateTime();
var result = this.myFunction();
var end = new GlideDateTime();

var dur = GlideDateTime.subtract(start, end);
gs.info("Duration is" + dur.getByFormat("HH:mm:ss.SSS"));

Mwatkins
ServiceNow Employee
ServiceNow Employee

I have used GlideStopWatch for years. It isn't a documented API as far as I know, but it is used in some out-of-box Script Includes like "DCCalendarRestEndPoint".

 

var sw = new GlideStopWatch();
gs.info(sw); // outputs 0:00:00.000
gs.info(sw.getTime()); // outputs 0 (milliseconds)