2013-04-11 38 views
2

loadspeed.js計時正確嗎?loadspeed.js時間:onload,DomContentLoaded還是別的?

因爲我在Chrome中的loaspeed.js和developpor工具欄之間有不同的結果。

var page = require('webpage').create(), 
    system = require('system'), 
    t, address; 

page.viewportSize = { width: 1024, height: 768 }; 

if (system.args.length === 1) { 
    console.log('Usage: loadspeed.js <some URL>'); 
    phantom.exit(1); 
} else { 
    t = Date.now(); 
    address = system.args[1]; 
    page.open(address, function (status) { 
     if (status !== 'success') { 
      console.log('FAIL to load the address'); 
     } else { 
      t = Date.now() - t; 
      console.log('#1 Loading time ' + t + ' msec'); 
      t = Date.now(); 
      page.open(address, function (status) { 
      if (status !== 'success') { 
       console.log('FAIL to load the address'); 
      } else { 
       t = Date.now() - t; 
       console.log('#2 Loading time ' + t + ' msec'); 
      } 
      phantom.exit(); 
     }); 
     } 
    }); 
} 

運行腳本給我

>phantomjs.exe loadspeed.js http://www.google.com 
#1 Loading time 348 msec 
#2 Loading time 202 msec 

有了Chrome developper工具欄中的私密模式,我可以看到這個(近兩個運行相同) toolbar result

正如你所看到的,我沒有相同的結果(注意:每次),它最終建議加載速度爲DOMContentLoaded的事件

該腳本中是否有「未配置」的功能?

也許我錯了,但簡單地說,我該如何確定頁面加載時間?

+0

比較Date.now()的'的'值是充滿了問題,#1是JavaScript的Date對象的分辨率。有關更多信息,請參閱http://stackoverflow.com/questions/131068/in-javascript-is-there-a-source-for-time-with-a-consistent-resolution-in-millis。 – 2013-04-19 20:39:28

+0

謝謝,但我只是想要一個近似的加載時間:分辨率是否爲50毫秒無關緊要。 – Cybermaxs 2013-05-06 09:33:49

回答

2

試試這個就在您的通話之前page.open:

page.onInitialized = function() { 
    page.evaluate(function() { 
     document.addEventListener('load', function() { 
      t = Date.now() - t; 
      console.log('#1 Loading time ' + t + ' msec'); 
     }, false); 
    }); 
}; 
+0

感謝您的建議,但您無法在文檔上添加加載。你必須調用window.addEventListener – Cybermaxs 2013-05-06 09:31:23

相關問題