我正在使用Google将使用PageSpeed脚本的数据拉到Google Sheets中。
但是,当我执行脚本并从PageSpeed URL获取数据时,第一个字节的时间数值比我在常规浏览器窗口中运行时要大得多。
例如,当我使用下面的代码在Google Sheets中获取URL时,我到达第一个字节的平均时间是500-510ms。而如果我在我的桌面上使用chrome中的完全相同的URL,通常是60-70ms。
这是我用来拉入数据的代码:
function callPageSpeed(strategy) {
var pageSpeedUrl = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' + pageSpeedMonitorUrl + '&key=' + pageSpeedApiKey + '&strategy=' + strategy;
var response = UrlFetchApp.fetch(pageSpeedUrl);
var json = response.getContentText();
return JSON.parse(json);}然后我使用以下命令将其推送到我的工作表中:
function monitor() {
var desktop = callPageSpeed('desktop');
var mobile = callPageSpeed('mobile');
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('results');
sheet.appendRow([
Utilities.formatDate(new Date(), 'GMT+1', 'yyyy-MM-dd'),
desktop['lighthouseResult']['audits']['time-to-first-byte']['numericValue'].toFixed(0) + " ms"
//some other metrics go here
]);有谁知道这可能是什么原因,以及我如何克服它?
发布于 2019-12-16 17:09:43
直接浏览器执行调用和应用程序脚本执行之间的平均时间差异是由于应用程序脚本代码在谷歌的服务器上运行,而不是像Docs所说的在您的本地机器上运行:
没有什么需要安装的--我们在你的浏览器中提供了一个代码编辑器,你的脚本就可以在谷歌的服务器上运行。
在调用外部API时要考虑到这一点,因为除了时间平均差异之外,它还可能有其他副作用,比如拥有一个不能分配的IP地址。
https://stackoverflow.com/questions/59330869
复制相似问题