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

OlaN
Giga Sage
Giga Sage

Hi,

For the stopwatch-solution, you could go look at this thread.

For the other part, counting and storing an average value, I guess the only way is to store the execution in a record in a table.

GeoffreyOptumOp
Tera Expert

Thanks @OlaN I did reference that article and I ended up using:

var msDuration = GlideDateTime.subtract(startTime, endTime).getNumericValue();

and I stored the value in the Session, across business rule iterations.

I had to add a piece of code somewhere, also, to RESET the session variable to 0 at an appropriate time.

 

Would anyone happen to know if using a session variable is much faster (or slower) to execute than using a table?  Thanks. 

I believe a session variable is faster to use than storing it in a record, because the session variables does not require that you read/write from the database, it's stored in memory.

So your solution should depend on your requirement, if you need to store and see the values over time, you should save it in the database.
Session variables are lost whenever the session ends, then all your data on average execution time is also lost.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Geoffrey,

ServiceNow has OCTimer to time server-side script. It is possible to time multi-thread scripts using OCTimer.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_OCTimerAPI