Global Stopwatch ?

GeoffreyOptumOp
Tera Expert

Intelligent ServiceNow Folks,

I need to figure out how long certain chunks of server side code are taking to run.  Those chunks of code might include database operations, REST operations, file operations, etc.  Arbitrary code.

I would also like to keep track of how many times those chunks of code run.  So the output I'm looking for is something like this:

Code Chunk X has been run 15,784 times.  Total run time: 154 seconds.  Average run time: 9.75 milliseconds.

So I want a "Stopwatch" and a "Counter", that I can send to a Log File or Info Message whenever I want.  What is a smart way to establish this?  Examples / Links appreciated.  Love you guys.  Thank you.

8 REPLIES 8

@Hitoshi Ozawa 

The OC Timer is very cool!  I did not know that existed!  Thanks for pointing me to it.

Do you know if it can be used in this situation?...

I have 500 records in a table.  The user is deleting them all.  For each one, a "Before Delete" business rule gets triggered, and it calls a function, "myFunction()", to do some work.

I need to time "myFunction()".  But where do I instantiate the OC Timer so that it does not get reset  / reinitialized for every invocation of "Before Delete"?

Do you follow?  Thanks.

Hi,

There can be several timers running at the same time.

Set a different name to in "start(<name>)" and "stop(<name>)". Maybe set the name to sys_id of the record to delete?

@Hitoshi Ozawa 

Hmm.  But that will be extremely cumbersome, because then I will end up with 500 log entries with 500 different values that I have to add up manually.  Right?

It sounds like the OC Timer can't really do what I want it to do.  Unless there is a way to instantiate an object from 1 business rule, and then USE that object in another business rule.  Is there??

Mwatkins
ServiceNow Employee
ServiceNow Employee

I use GlideStopWatch.

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