ChromeTrace library


This is a library to allow us to use the Chrome Tracing utility for apps script profiling. I've implemented a subset of what can be profiled, but will implement additional capabilities if there is enough interest. The VBA version is here - Chrome Tracing class for VBA

Getting started

You'll need the ChromeTrace library which you can get on github or see below for the script project details.  The code for the following tests is also available here on github.

https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec?list=cChromeTrace


Drive API

It uses the Drive API - specifically my Drive JSON API for apps script - to create the Chrome Tracing file on drive. This means that you need to enable the Drive API from the Advanced services menu and the Cloud console. 

Examples

Let's get straight down to an example. We'd like to see how long it takes to execute this snippet

    var LOOPSIZE = 1000;
    var text = "abcdefghijklmnop";
    var key = "xyz";

      for (var i = 0 ; i < LOOPSIZE ; i++) {
        Utilities.computeHmacSha256Signature(text, key);
      }

Create a trace object
  // create tracing object - need to have enabled drive advanced service to get a properly scoped token
  var trace = new cChromeTrace.ChromeTrace().setAccessToken(ScriptApp.getOAuthToken());

Start tracing
trace.begin('hmac256');

Run the code
 for (var i = 0 ; i < LOOPSIZE ; i++) {
   Utilities.computeHmacSha256Signature(text, key);
 }
Stop tracing
trace.end('hmac256');

Dump the results to drive in the given folder
trace.dump("/Published Scripts/tracing")

Load to Chrome tracer and see the viz

And click on the bar and see the data

Multiple traces and nesting

Now let's compare a couple of different traces

Here's the code that generated that
  // create tracing object - need to have enabled drive advanced service to get a properly scoped token
  var trace = new cChromeTrace.ChromeTrace().setAccessToken(ScriptApp.getOAuthToken());
  
  var text = "abcdefghijklmnop";
  var key = "xyz";
  
  // add a couple of events
  trace.begin('hmac');
  
    var LOOPSIZE = 1000;
  
    trace.begin('hmac256');
    
      for (var i = 0 ; i < LOOPSIZE ; i++) {
        Utilities.computeHmacSha256Signature(text, key);
      }

    trace.end('hmac256');
    
    trace.begin ('hmac512');
    
      for (var i = 0 ; i < LOOPSIZE ; i++) {
        Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, text, key);
      }
      
    trace.end('hmac512');

  trace.end('hmac');

  trace.dump("/Published Scripts/tracing");

Counters

That's timing - now here's how to show values over time. This time we'll do the same thing, but also show a couple of values for each loop iteration. What we're looking at here are the two counts with each of the values as well as the timings for each section.


Picking a point in time, we can look at those values at that point

Here's the code for this

function myFunction() {
  
  // -- always leave this comment somewhere .. its a hack to provoke an authorization dialog ... Drive.Files.copy(resource, fileId)
  
  // create tracing object - need to have enabled drive advanced service to get a properly scoped token
  var trace = new cChromeTrace.ChromeTrace().setAccessToken(ScriptApp.getOAuthToken());
  
  var text = "abcdefghijklmnop";
  var key = "xyz";
  
  // add a couple of events
  trace.begin('hmac');
  
    var LOOPSIZE = 1000;
  
    trace.begin('hmac256');
    
      for (var i = 0 ; i < LOOPSIZE ; i++) {
        Utilities.computeHmacSha256Signature(text, key);
        trace.counter ("count256", {args:{count:i,random:Math.random()*LOOPSIZE}});
      }
      
      
    trace.end('hmac256');
    
    trace.begin ('hmac512');
    
      for (var i = 0 ; i < LOOPSIZE ; i++) {
        Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, text, key);
        trace.counter ("count512", {args:{count:i,random:Math.random()*LOOPSIZE}});
      }
      
    trace.end('hmac512');

  trace.end('hmac');

  trace.dump("/Published Scripts/tracing");
  
  
}
The Chrome tracing utility is rich with options. I won't cover them all here - just try it and play with it. It's very very good.

Notes on access token

Although I'm not using the Apps Script Drive service, but rather the generic Drive API, the simplest way to get a properly scoped access token is to pretend that we're going to use the Drive service. To do that
  • add the Drive Api advanced service
  • activate it on the cloud console
  • add this line somewhere in your script - it works as just a commen
// -- always leave this comment somewhere .. its a hack to provoke an authorization dialog ... Drive.Files.copy(resource, fileId)

If you do those things, then you'll get an appropriate authentication dialog that creates the needed scope - then your script access token can be passed over to the library when you create the ChromeTrace object. Alternatively you can use my cEzyOauth2 library, but that's a lot more complicated.

  var trace = new cChromeTrace.ChromeTrace().setAccessToken(ScriptApp.getOAuthToken());

Loading trace file to Chrome

Covered in tracing

For help and more information join our community,  follow the blog,  follow me on twitter, or follow me on g+. If you have any additional Chrome tracing types activated, please let me know.
Comments