我用 JavaScript 创建了一个脚本,在自动浏览器测试期间将其注入(inject)到我们的 Ext JS 应用程序中。该脚本测量将数据加载到我们的网格中所花费的时间。
具体来说,脚本会轮询每个网格,查看是否有第一行或“无数据”消息,一旦所有网格都满足此条件,脚本就会记录 Date.now() 和性能之间的值。 timing.fetchStart,并将其视为页面加载所需的时间。
此脚本或多或少按预期工作,但是与人工测量的计时(Google 秒表 ftw)相比,此测试报告的时间始终比秒表测量的时间长约 300 毫秒。
我的问题是:
- 此逻辑中是否存在会导致错误结果的漏洞?
- 是否有其他准确的方法来实现这一点 测量?
脚本如下:
function loadPoll() {
var i, duration,
dataRow = '.firstRow', noDataRow = '.noData',
grids = ['.grid1', '.grid2', '.grid3','.grid4', 'grid5', 'grid6', 'grid7'];
for (i = 0; i < grids.length; ++i) {
var data = grids[i] + ' ' + dataRow,
noData = grids[i] + ' ' + noDataRow;
if (!(document.querySelector(data) || document.querySelector(noData))) {
window.setTimeout(loadPoll, 100);
return;
}
}
duration = Date.now() - performance.timing.fetchStart;
window.loadTime = duration;
}
loadPoll();
一些注意事项:
虽然我知道人类的 react 时间可能很慢,但我确信 300 毫秒的不一致不是人为造成的 使用 Google 秒表的因素。
查看代码可能会出现多个轮询 元素可能导致 300 毫秒的不一致,但是当我 将被监视的元素数从 7 更改为 1,有 在报告的时间中似乎仍然有 300 毫秒的盈余 自动化测试。
我们的自动化测试在由 Selenium 和 Protractor 。
如果您能对此提供任何见解,请提前致谢!
最佳答案
如果您使用 performance.now()
,时间应该精确到 5 微秒。根据MDN :
The performance.now() method returns a DOMHighResTimeStamp, measured in milliseconds, accurate to five thousandths of a millisecond (5 microseconds).
The returned value represents the time elapsed since the time origin (the PerformanceTiming.navigationStart property).
关于javascript - 使用 JavaScript 测量页面加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702861/