Profiling the Performance of a Google App Script

旧城冷巷雨未停 提交于 2019-12-06 19:20:54

问题


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?


回答1:


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.




回答2:


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




回答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

  • Stackdriver Logging for Google Apps Script is now available
  • Identify and understand errors in Apps Script with Stackdriver



回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/20846094/profiling-the-performance-of-a-google-app-script

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