Profiling the Performance of a Google App Script

你。 提交于 2019-12-05 00:53:06

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.

Serge insas

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...

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

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');

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!