You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/02/11 14:50:01 UTC
svn commit: r1243063 - in
/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark:
BenchmarkWorker.java DefaultHeader.java HttpBenchmark.java
Author: olegk
Date: Sat Feb 11 13:50:00 2012
New Revision: 1243063
URL: http://svn.apache.org/viewvc?rev=1243063&view=rev
Log:
Refactored HttpBenchmark initialization code
Modified:
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/DefaultHeader.java
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java?rev=1243063&r1=1243062&r2=1243063&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java Sat Feb 11 13:50:00 2012
@@ -26,18 +26,12 @@
*/
package org.apache.http.benchmark;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
-import java.security.KeyStore;
-import javax.net.ssl.KeyManagerFactory;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLSocketFactory;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
+import javax.net.SocketFactory;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
@@ -50,15 +44,13 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
-import org.apache.http.params.DefaultedHttpParams;
+import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
@@ -72,11 +64,10 @@ import org.apache.http.protocol.RequestU
*
* @since 4.0
*/
-public class BenchmarkWorker implements Runnable {
+class BenchmarkWorker implements Runnable {
- private byte[] buffer = new byte[4096];
+ private final byte[] buffer = new byte[4096];
private final int verbosity;
- private final HttpParams params;
private final HttpContext context;
private final BasicHttpProcessor httpProcessor;
private final HttpRequestExecutor httpexecutor;
@@ -85,29 +76,18 @@ public class BenchmarkWorker implements
private final HttpHost targetHost;
private final int count;
private final boolean keepalive;
- private final boolean disableSSLVerification;
+ private final SocketFactory socketFactory;
private final Stats stats = new Stats();
- private final TrustManager[] trustAllCerts;
- private final String trustStorePath;
- private final String trustStorePassword;
- private final String identityStorePath;
- private final String identityStorePassword;
public BenchmarkWorker(
- final HttpParams params,
- int verbosity,
final HttpRequest request,
final HttpHost targetHost,
int count,
boolean keepalive,
- boolean disableSSLVerification,
- String trustStorePath,
- String trustStorePassword,
- String identityStorePath,
- String identityStorePassword) {
+ int verbosity,
+ final SocketFactory socketFactory) {
super();
- this.params = params;
this.context = new BasicHttpContext(null);
this.request = request;
this.targetHost = targetHost;
@@ -127,29 +107,7 @@ public class BenchmarkWorker implements
this.connstrategy = new DefaultConnectionReuseStrategy();
this.verbosity = verbosity;
- this.disableSSLVerification = disableSSLVerification;
- this.trustStorePath = trustStorePath;
- this.trustStorePassword = trustStorePassword;
- this.identityStorePath = identityStorePath;
- this.identityStorePassword = identityStorePassword;
-
- // Create a trust manager that does not validate certificate chains
- trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
-
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return null;
- }
-
- public void checkClientTrusted(
- java.security.cert.X509Certificate[] certs, String authType) {
- }
-
- public void checkServerTrusted(
- java.security.cert.X509Certificate[] certs, String authType) {
- }
- }
- };
+ this.socketFactory = socketFactory;
}
public void run() {
@@ -174,7 +132,6 @@ public class BenchmarkWorker implements
this.context.setAttribute(ExecutionContext.HTTP_REQUEST, this.request);
stats.start();
- request.setParams(new DefaultedHttpParams(new BasicHttpParams(), this.params));
for (int i = 0; i < count; i++) {
try {
@@ -182,39 +139,13 @@ public class BenchmarkWorker implements
if (!conn.isOpen()) {
Socket socket;
- if ("https".equals(targetHost.getSchemeName())) {
- if (disableSSLVerification) {
- SSLContext sc = SSLContext.getInstance("SSL");
- if (identityStorePath != null) {
- KeyStore identityStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(identityStorePath);
- try {
- identityStore.load(instream, identityStorePassword.toCharArray());
- } finally {
- try { instream.close(); } catch (IOException ignore) {}
- }
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(
- KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(identityStore, identityStorePassword.toCharArray());
- sc.init(kmf.getKeyManagers(), trustAllCerts, null);
- } else {
- sc.init(null, trustAllCerts, null);
- }
- socket = sc.getSocketFactory().createSocket();
-
- } else {
- if (trustStorePath != null) {
- System.setProperty("javax.net.ssl.trustStore", trustStorePath);
- }
- if (trustStorePassword != null) {
- System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
- }
- socket = SSLSocketFactory.getDefault().createSocket();
- }
+ if (socketFactory != null) {
+ socket = socketFactory.createSocket();
} else {
socket = new Socket();
}
+ HttpParams params = request.getParams();
int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
int soTimeout = HttpConnectionParams.getSoTimeout(params);
@@ -282,13 +213,11 @@ public class BenchmarkWorker implements
}
} catch (IOException ex) {
- ex.printStackTrace();
stats.incFailureCount();
if (this.verbosity >= 2) {
System.err.println("I/O error: " + ex.getMessage());
}
} catch (Exception ex) {
- ex.printStackTrace();
stats.incFailureCount();
if (this.verbosity >= 2) {
System.err.println("Generic error: " + ex.getMessage());
@@ -308,7 +237,6 @@ public class BenchmarkWorker implements
try {
conn.close();
} catch (IOException ex) {
- ex.printStackTrace();
stats.incFailureCount();
if (this.verbosity >= 2) {
System.err.println("I/O error: " + ex.getMessage());
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/DefaultHeader.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/DefaultHeader.java?rev=1243063&r1=1243062&r2=1243063&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/DefaultHeader.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/DefaultHeader.java Sat Feb 11 13:50:00 2012
@@ -29,7 +29,7 @@ package org.apache.http.benchmark;
import org.apache.http.message.BasicHeader;
-public class DefaultHeader extends BasicHeader {
+class DefaultHeader extends BasicHeader {
private static final long serialVersionUID = 3465786867105185103L;
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java?rev=1243063&r1=1243062&r2=1243063&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java Sat Feb 11 13:50:00 2012
@@ -26,7 +26,8 @@
*/
package org.apache.http.benchmark;
-import java.io.UnsupportedEncodingException;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.net.URL;
import org.apache.commons.cli.CommandLine;
@@ -47,10 +48,20 @@ import org.apache.http.params.HttpParams
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
+import java.security.KeyStore;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+
+import javax.net.SocketFactory;
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509TrustManager;
+
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
@@ -64,10 +75,6 @@ public class HttpBenchmark {
private final Config config;
- private HttpParams params = null;
- private HttpRequest[] request = null;
- private HttpHost host = null;
-
public static void main(String[] args) throws Exception {
Options options = CommandLineUtils.getOptions();
@@ -96,13 +103,16 @@ public class HttpBenchmark {
this.config = config != null ? config : new Config();
}
- private void prepare() throws UnsupportedEncodingException {
- // prepare http params
- params = getHttpParams(config.getSocketTimeout(), config.isUseHttp1_0(), config.isUseExpectContinue());
+ private HttpRequest createRequest() {
+ HttpParams params = new BasicHttpParams();
+ params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
+ config.isUseHttp1_0() ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1)
+ .setParameter(HttpProtocolParams.USER_AGENT, "HttpCore-AB/1.1")
+ .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, config.isUseExpectContinue())
+ .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+ .setIntParameter(HttpConnectionParams.SO_TIMEOUT, config.getSocketTimeout());
URL url = config.getUrl();
- host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
-
HttpEntity entity = null;
// Prepare requests for each thread
@@ -112,39 +122,34 @@ public class HttpBenchmark {
fe.setChunked(config.isUseChunking());
entity = fe;
} else if (config.getPayloadText() != null) {
- StringEntity se = new StringEntity(config.getPayloadText(),
+ StringEntity se = StringEntity.create(config.getPayloadText(),
ContentType.parse(config.getContentType()));
se.setChunked(config.isUseChunking());
entity = se;
}
- request = new HttpRequest[config.getThreads()];
-
- for (int i = 0; i < request.length; i++) {
- if ("POST".equals(config.getMethod())) {
- BasicHttpEntityEnclosingRequest httppost =
- new BasicHttpEntityEnclosingRequest("POST", url.getPath());
- httppost.setEntity(entity);
- request[i] = httppost;
- } else if ("PUT".equals(config.getMethod())) {
- BasicHttpEntityEnclosingRequest httpput =
- new BasicHttpEntityEnclosingRequest("PUT", url.getPath());
- httpput.setEntity(entity);
- request[i] = httpput;
- } else {
- String path = url.getPath();
- if (url.getQuery() != null && url.getQuery().length() > 0) {
- path += "?" + url.getQuery();
- } else if (path.trim().length() == 0) {
- path = "/";
- }
- request[i] = new BasicHttpRequest(config.getMethod(), path);
+ HttpRequest request;
+ if ("POST".equals(config.getMethod())) {
+ BasicHttpEntityEnclosingRequest httppost =
+ new BasicHttpEntityEnclosingRequest("POST", url.getPath());
+ httppost.setEntity(entity);
+ request = httppost;
+ } else if ("PUT".equals(config.getMethod())) {
+ BasicHttpEntityEnclosingRequest httpput =
+ new BasicHttpEntityEnclosingRequest("PUT", url.getPath());
+ httpput.setEntity(entity);
+ request = httpput;
+ } else {
+ String path = url.getPath();
+ if (url.getQuery() != null && url.getQuery().length() > 0) {
+ path += "?" + url.getQuery();
+ } else if (path.trim().length() == 0) {
+ path = "/";
}
+ request = new BasicHttpRequest(config.getMethod(), path);
}
if (!config.isKeepAlive()) {
- for (int i = 0; i < request.length; i++) {
- request[i].addHeader(new DefaultHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
- }
+ request.addHeader(new DefaultHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
}
String[] headers = config.getHeaders();
@@ -154,29 +159,25 @@ public class HttpBenchmark {
int pos = s.indexOf(':');
if (pos != -1) {
Header header = new DefaultHeader(s.substring(0, pos).trim(), s.substring(pos + 1));
- for (int j = 0; j < request.length; j++) {
- request[j].addHeader(header);
- }
+ request.addHeader(header);
}
}
}
if (config.isUseAcceptGZip()) {
- for (int i = 0; i < request.length; i++) {
- request[i].addHeader(new DefaultHeader("Accept-Encoding", "gzip"));
- }
+ request.addHeader(new DefaultHeader("Accept-Encoding", "gzip"));
}
if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
- for (int i = 0; i < request.length; i++) {
- request[i].addHeader(new DefaultHeader("SOAPAction", config.getSoapAction()));
- }
+ request.addHeader(new DefaultHeader("SOAPAction", config.getSoapAction()));
}
+ return request;
}
public String execute() throws Exception {
- prepare();
+ URL url = config.getUrl();
+ HttpHost host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
ThreadPoolExecutor workerPool = new ThreadPoolExecutor(
config.getThreads(), config.getThreads(), 5, TimeUnit.SECONDS,
@@ -190,20 +191,71 @@ public class HttpBenchmark {
});
workerPool.prestartAllCoreThreads();
+ SocketFactory socketFactory = null;
+ if ("https".equals(host.getSchemeName())) {
+ TrustManager[] trustManagers = null;
+ if (config.isDisableSSLVerification()) {
+ // Create a trust manager that does not validate certificate chains
+ trustManagers = new TrustManager[] {
+ new X509TrustManager() {
+
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+
+ public void checkClientTrusted(
+ java.security.cert.X509Certificate[] certs, String authType) {
+ }
+
+ public void checkServerTrusted(
+ java.security.cert.X509Certificate[] certs, String authType) {
+ }
+ }
+ };
+ } else if (config.getTrustStorePath() != null) {
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ FileInputStream instream = new FileInputStream(config.getTrustStorePath());
+ try {
+ trustStore.load(instream, config.getTrustStorePath() != null ?
+ config.getTrustStorePath().toCharArray() : null);
+ } finally {
+ try { instream.close(); } catch (IOException ignore) {}
+ }
+ TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
+ TrustManagerFactory.getDefaultAlgorithm());
+ tmfactory.init(trustStore);
+ trustManagers = tmfactory.getTrustManagers();
+ }
+ KeyManager[] keyManagers = null;
+ if (config.getIdentityStorePath() != null) {
+ KeyStore identityStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ FileInputStream instream = new FileInputStream(config.getIdentityStorePath());
+ try {
+ identityStore.load(instream, config.getIdentityStorePassword() != null ?
+ config.getIdentityStorePassword().toCharArray() : null);
+ } finally {
+ try { instream.close(); } catch (IOException ignore) {}
+ }
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(
+ KeyManagerFactory.getDefaultAlgorithm());
+ kmf.init(identityStore, config.getIdentityStorePassword() != null ?
+ config.getIdentityStorePassword().toCharArray() : null);
+ keyManagers = kmf.getKeyManagers();
+ }
+ SSLContext sc = SSLContext.getInstance("SSL");
+ sc.init(keyManagers, trustManagers, null);
+ socketFactory = sc.getSocketFactory();
+ }
+
BenchmarkWorker[] workers = new BenchmarkWorker[config.getThreads()];
for (int i = 0; i < workers.length; i++) {
workers[i] = new BenchmarkWorker(
- params,
- config.getVerbosity(),
- request[i],
+ createRequest(),
host,
config.getRequests(),
config.isKeepAlive(),
- config.isDisableSSLVerification(),
- config.getTrustStorePath(),
- config.getTrustStorePassword(),
- config.getIdentityStorePath(),
- config.getIdentityStorePassword());
+ config.getVerbosity(),
+ socketFactory);
workerPool.execute(workers[i]);
}
@@ -219,16 +271,4 @@ public class HttpBenchmark {
return ResultProcessor.printResults(workers, host, config.getUrl().toString());
}
- private HttpParams getHttpParams(
- int socketTimeout, boolean useHttp1_0, boolean useExpectContinue) {
-
- HttpParams params = new BasicHttpParams();
- params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
- useHttp1_0 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1)
- .setParameter(HttpProtocolParams.USER_AGENT, "HttpCore-AB/1.1")
- .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, useExpectContinue)
- .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
- .setIntParameter(HttpConnectionParams.SO_TIMEOUT, socketTimeout);
- return params;
- }
}