15

I've written a Google App Script to pull in Google Analytics data into a Google Spreadsheet. It's a fairly long running script making multiple requests to the GA Reporting API, it also uses Google App's ScriptDB.

Is there a good way of profiling each step of the scripts performance so I can figure out what areas are taking the longest, so I can begin optimizing in certain areas?

Rubén
  • 24,097
  • 9
  • 55
  • 116
eywu
  • 2,454
  • 1
  • 20
  • 24

5 Answers5

15

As a complement to Fred's answer, define a variable start at the top of your script

var start = new Date().getTime();

then in a few Logger.log() placed a strategic points use

Logger.log(new Date().getTime()-start);

and you'll get a pretty good idea of what is going on...

cheesus
  • 11,077
  • 10
  • 64
  • 122
Serge insas
  • 42,040
  • 6
  • 89
  • 113
14

The Execution Transcript is very useful for this kind of thing.

You can also put Logger.log() statements all over the place that measure the time since the last log. That way you can find areas that take longer to execute.

I have a spreadsheet where I copy the log after an execution and then formulas + conditional formatting help me identify areas that are slow.

Fred
  • 1,051
  • 13
  • 21
  • please consider my answer as it is : a simple complement about the Logger method. Your solution using SS if of course more convenient although a bit more complex to set up. btw I voted it up :-) – Serge insas Dec 30 '13 at 21:09
  • OMG, I didn't even know about "Execution Transcript"! Amazing sauce! Also that little spreadsheet you made is awesome! It quickly help me identify the slow down. Thanks! – eywu Jan 02 '14 at 06:01
3

The basic tools are the execution transcript and Class Logger (mentioned on previous answers)

Nowadays we could use Stackdriver logging and Class Console which includes console.time and console.timeEnd.

And to make things betters, also we could send Stackdriver logging to Google Data Studio through BigQuery, so we could build helpful metrics to track profiling data.

References

Rubén
  • 24,097
  • 9
  • 55
  • 116
1

To correct Serge's answer, where call to Date function has a problem, you can use this solution.

function myFunction() {
  var start_time = new Date().getTime();
  /*
  your code goes here
  */
  Logger.log('Total execution time is :' + (new Date().getTime()-start_time) + ' ms');
}

you can replace the comment lines with your code. After execution press Ctrl + Enter or Command ⌘ + Enter on Mac to see the logs. You will get something like following:

[17-01-18 12:25:58:932 UTC] Total execution time is :16586 ms

So here total execution time is 16586 ms or in other words 16.586 Seconds.

In addition to above you can add the following snippet in between your code to measure execution time from different parts.

Logger.log('Execution time till checkpoint 1:' + (new Date().getTime()-start_time) + ' ms');
Cyril
  • 417
  • 12
  • 19
1

You should probably use the methods console.time(label) and console.timeEnd(label) since you could get negative results when using Date.

Marcono1234
  • 2,746
  • 12
  • 29
  • I have not seen reports of having negative results when using Date on Google Apps Script server side code. Have you seen one? – Rubén Aug 04 '18 at 16:11
  • 1
    No, I have not seen any either, but I was talking more about the general problem that real world clocks might get synchronized and therefore the resulting difference becomes negative. Though I doubt that this will happen on Google's servers. – Marcono1234 Sep 02 '18 at 22:47