Article
· May 13 2m read

Using %System.Monitor.LineByLine together with %SYS.MONLBL to analyze your code

I'm sure most of you are familiar with utility %SYS.MONLBL that is crucial when analysing code performance bottlenecks. It allows you to select a number of routines that you want to monitor at runtime and also specify what process(es) you want to watch. BUT, what if you do not know exactly, what process would execute your code? This is true with many web based (CSP/REST) applications today. You want to minimize the resource utilization on your production system that needs analysis. So, how about doing a small tweak?

1. Define an INC file with these macros:

#define START(%level) try { ##continue 
s zzroutine=$p($view(-1,-3),"^",6) ##continue 
if ##class(%Monitor.System.LineByLine).IsActive(zzroutine)=0 { ##continue $$$ThrowOnError(##class(%Monitor.System.LineByLine).Start($lfs(zzroutine),$lfs(##class(%Monitor.System.LineByLine).GetMetrics(%level)),$lb($job))) ##continue } ##continue 
h 1 ##continue 
} catch (e) { ##continue
 d BACK^%ETN ##continue 
} 

#define PAUSE s sc=##class(%Monitor.System.LineByLine).Pause()

2. in your class, or routine, identify a place you want to monitor and put these two macros to start collecting data and pausing collection, like in this example:

$$$START(2)   // use 1 or 2 here

// code of your application here

$$$PAUSE

Whenever you run this code (method of a class or routine) this would try to start %SYS.MONLBL for this particular class or routine and process.

Once your code is finished, you can simply go to terminal in the namespace where your application runs and call d ^%SYS.MONLBL. As START macro started monitor, it is still running and you can easily collect your performance data, optionally together with source code. Do not forget to stop monitor, once you have collected your data!

If you need to collect data for more routines, you can easily modify  the START macro to allow for manual entry of routines list to analyze.

Hope you find this little tweak useful!

Discussion (1)2
Log in or sign up to continue

Hi Daniel 😊

can you elaborate "$p($view(-1,-3),"^",6)"? 😉

Your code gives the impression that to implement this solution requires knowledge that we "simple humans" don't have.

Fortunately this is not the case, instead of the cryptic, obscure, arcane and undocumented $p($view(-1,-3),"^",6) the simple $ZNAME special variable can be used. 😃