我使用 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();
}
}
}
}