Advanced profiling: Rogue Use Cases

Identifying rogue cases - (by that I mean a combination of circumstances that cause a normally well behaved procedure to take forever), is very simple using these profiling tools. For the sake of this example, I'm assuming you have read about profiling, downloaded and added profiling using the autoprofiler, played around with it and come up with a section of code that has a wide variance of execution times, and you want to understand which iterations are taking a long time.

Identifying rogues

Lets look at an example
    Dim cTimer As cpProfiler.cProcTimer
    ' some loop
        cpProfiler.apProfiler_1.Start "Proc:mloop", "(Module1) Proc:mloop"   'apProfiler_1 :apr:slc:0.2 on 7/21/2010 at 1:36:26 PM
        Set cTimer = cpProfiler.apProfiler_1.Timers("Proc:mloop")
    '     code A to be investigated
          Debug.Assert cTimer.soFar < 0.5

    '     code B to be investigated
          Debug.Assert cTimer.soFar < 0.5

        cpProfiler.apProfiler_1.Finish "Proc:mloop"    'apProfiler_1 :apr:slc:0.2 on 7/21/2010 at 1:3
    ' end of loop

Whats happening

We have identified that sometimes the section of code between our start and finish timers takes a long time to execute. We want to halt execution and debug when it happens. 

First set up a cproctimer class to use.

 Dim cTimer As cpProfiler.cProcTimer

... assign the one being monitored

Set cTimer = cpProfiler.apProfiler_1.Timers("Proc:mloop")

... this will halt execution when the time spent on any particular iteration is more than .5 second

Debug.Assert cTimer.soFar < 0.5

Note that when you break execution, the timer continues so every time you look at cTimer.soFar it will have increased. If this matters, pause the timer with ctimer.pause before breaking code execution.

Another useful one would be number of iterations, as below where execution will break when the number of times we have been in this loop is 5000 or more.

Debug.Assert cTimer.Iterations < 5000


Clearly this gives a powerful tool to break execution programatically if certain activities take longer than expected. See here for  more on the cproctimer class.

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'ReillyAmazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.

For help and more information join our forumfollow the blogfollow me on twitter