- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2022 01:04 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2022 02:36 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2022 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2022 02:36 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 01:41 AM
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"));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2024 11:16 AM
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)