1

我使用 firefoxdriver 和 firefox 21 对 selenium 进行了 htmlunit
的性能测试。性能测试是通过 Eclipse 在我的 windows7 机器上进行的。
当两者都禁用 javascript 时,性能是相同的。
当两者都打开了 javascript 时,htmlunit 2.12比 firefox 慢150% 。
我想这是由于蜘蛛猴引擎在犀牛上的优势。
有没有办法配置 rhino 会更快?
有没有其他方法可以加快 htmlunit 的速度?

package utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.util.Date;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class PerformanceTest {
public static void main(String[] args)  {
    String[] urls = new String[] {
...
};
      Date beforeSelenium = new Date();
      System.out.println("Going to run selenium");
      testSelenium(urls);
      Date afterSelenium = new Date();

      Date beforehtmlUnit= new Date();
      System.out.println("Going to run htmlunit");
      testHtmlUnit(urls);
      Date afterhtmlUnit = new Date();

      System.out.println(
                DateFormat.getTimeInstance(DateFormat.LONG).format(beforeSelenium));
      System.out.println(
                DateFormat.getTimeInstance(DateFormat.LONG).format(afterSelenium));
      System.out.println(
                DateFormat.getTimeInstance(DateFormat.LONG).format(beforehtmlUnit));
      System.out.println(
                DateFormat.getTimeInstance(DateFormat.LONG).format(afterhtmlUnit));


}
public static void testSelenium(String[] urls) {
 WebDriver driver = new FirefoxDriver();
 int i=0;
 for(String url:urls) {
     i++;
     System.out.println(i);
     // And now use this to visit Google
     driver.get(url);
     String str = driver.getPageSource();
     System.out.println(str);

 }
 driver.close();
}

public static void testHtmlUnit(String[] urls)  {
WebClient client = new WebClient(BrowserVersion.FIREFOX_17);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setRedirectEnabled(true);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setCssEnabled(true);
client.getOptions().setUseInsecureSSL(true);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
int i=0;
 for(String url:urls) {
     i++;
     System.out.println(i);
     // And now use this to visit Google
     HtmlPage page;
    try {
        page = client.getPage(url);
        String str = page.asText();
         System.out.println(str);
    } catch (FailingHttpStatusCodeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


 }
}

}

4

2 回答 2

5

HtmlUnit use a modified, repacked Rhino JavaScript engine. By default is used a interpreter mode. You can enable JavaScript optimization. Then the JavaScript will be compiled to Java byte code.

WebClient webClient = new WebClient();
JavaScriptEngine sriptEngine = webClient.getJavaScriptEngine();
HtmlUnitContextFactory factory = sriptEngine.getContextFactory();
Context context = factory.enterContext();
context.setOptimizationLevel(9);

But this must not speed up your page. It can also slow down it.

于 2013-11-30T15:46:25.923 回答
4

JS 引擎是 HtmlUnit 性能的主要问题。

有一些选项:

  1. 更改Rhino 优化设置
  2. 编写Rhino Optimized Javascript(几乎不够)。
  3. 适配外部引擎(如 Chromium)。
  4. 等待纳肖恩
  5. 切换到PhantomJs
于 2013-05-24T02:08:17.707 回答