2013-05-08 127 views
0

我试图用node.js编写一个性能工具,这样我就可以自动执行它,并将结果存储在MySQL中。该工具应该收集浏览器加载特定网页所用的时间。我使用HttpWatch测量性能,结果以秒显示。使用的浏览器是Firefox。 下面是我使用一段脚本来运行性能测试:node.js循环迭代后崩溃

var MyUrls = [ 
    "http://google.com", 
    "http://yahoo.com" 
    ];  

try { 

     var win32ole = require('win32ole'); 
     var control = win32ole.client.Dispatch('HttpWatch.Controller'); 
     var plugin = control.Firefox.New(); 
     for (var i=0; i < MyUrls.length; i++) { 
      var url = MyUrls[i]; 
      console.log(url); 
      for(var j=0; j < 14; j++) { 
      // Start Recording HTTP traffic 
      plugin.Log.EnableFilter(false); 
      // Clear Cache and cookier before each test 
      plugin.ClearCache(); 
      plugin.ClearAllCookies(); 
      plugin.ClearSessionCookies(); 
      plugin.Record(); 
      // Goto to the URL and wait for the page to be loaded 
      plugin.GotoURL(url); 
      control.Wait(plugin, -1); 
      // Stop recording HTTP 
      plugin.Stop(); 
      if (plugin.Log.Pages.Count != 0) 
      {   
       // Display summary statistics for page 
       var summary = plugin.Log.Pages(0).Entries.Summary; 
       console.log(summary.Time); 
      } 
      } 
     } 
     plugin.CloseBrowser(); 
    } catch(e) { 
     console.log('*** exception cached ***\n' + e); 
    } 

内循环的第二次迭代后,我发现了以下错误:

C:\xampp\htdocs\test\browser-perf>node FF-load-navigation.js 
http://localhost/NFC-performance/Bing.htm 
[Number (VT_R8 or VT_I8 bug?)] 
2.718 
[Number (VT_R8 or VT_I8 bug?)] 
2.718 
OLE error: [EnableFilter] -2147352570 [EnableFilter] IDispatch::GetIDsOfNames Au 
toWrap() failed 

让别人之前看过这个?你可以帮我吗?

+0

您正在使用的Firefox“插件”的API文档在哪里?我怀疑像ClearCache和ClearCookies这样的方法是同步的(即在操作完成之前阻塞执行)。如果它们是异步的,则需要为它们指定回调。 https://github.com/idobatter/node-win32ole说“异步,非阻塞win32ole绑定” - 我的预期。 – 2013-05-08 11:20:20

+0

这很古怪,WIN32OLE的自述说,它应该是非阻塞的,但示例显示各种功能只是以同步的方式。这就像自述文件直接躺在你的脸上一样。 – 2013-05-08 11:23:59

回答

0

你要记住,节点是异步

所以,对于循环同时运行到plugin.CloseBrowser();,这显然不是你想要的,因为这就是使其关闭,这将导致问题在for循环的东西。

,而你想,要后运行for循环结束。

看一下async一个简单的方法来做到这一点。

async.each(MyUrls, function (callback) { 
    ... 
    callback() 
}, function(err){ 
    plugin.CloseBrowser(); 
}); 

对于你的内循环也必须做同样的事情。

+0

你能否给我更多尼亚尔想法的细节? – cybertextron 2013-05-08 15:30:05

+0

@philippe当然。所以节点异步工作。所以,如果我说一个for循环像你上面,然后是一些代码之后,节点将运行for循环,并在同一时间,运行代码的下位。在你的代码,它运行for循环,然后同时调用'plugin.CloseBrowser();',并考虑到你使用'plugin' VAR在循环,我假定调用上面的函数会造成很多问题。找我? – Niall 2013-05-08 15:39:08