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 2010/11/04 16:48:27 UTC
svn commit: r1031044 - in
/httpcomponents/httpclient/trunk/httpclient-benchmark: ./
src/main/java/org/apache/http/client/benchmark/
Author: olegk
Date: Thu Nov 4 15:48:27 2010
New Revision: 1031044
URL: http://svn.apache.org/viewvc?rev=1031044&view=rev
Log:
Updated HC benchmark to support concurrent request execution; switched Jetty HttpClient to NIO mode
Modified:
httpcomponents/httpclient/trunk/httpclient-benchmark/pom.xml
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Stats.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpAgent.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient3.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpJRE.java
httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/pom.xml?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/pom.xml (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/pom.xml Thu Nov 4 15:48:27 2010
@@ -70,13 +70,13 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
- <version>7.0.1.v20091125</version>
+ <version>7.2.0.v20101020</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
- <version>7.0.1.v20091125</version>
+ <version>7.2.0.v20101020</version>
<scope>compile</scope>
</dependency>
<dependency>
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java Thu Nov 4 15:48:27 2010
@@ -47,6 +47,15 @@ import org.eclipse.jetty.util.thread.Que
public class Benchmark {
public static void main(String[] args) throws Exception {
+
+ String ns = System.getProperty("hc.benchmark.n-requests", "100000");
+ String nc = System.getProperty("hc.benchmark.concurrent", "25");
+ String cls = System.getProperty("hc.benchmark.content-len", "2048");
+
+ int n = Integer.parseInt(ns);
+ int c = Integer.parseInt(nc);
+ int contentLen = Integer.parseInt(cls);
+
SocketConnector connector = new SocketConnector();
connector.setPort(0);
connector.setRequestBufferSize(12 * 1024);
@@ -54,8 +63,8 @@ public class Benchmark {
connector.setAcceptors(2);
QueuedThreadPool threadpool = new QueuedThreadPool();
- threadpool.setMinThreads(25);
- threadpool.setMaxThreads(200);
+ threadpool.setMinThreads(c);
+ threadpool.setMaxThreads(2000);
Server server = new Server();
server.addConnector(connector);
@@ -64,9 +73,10 @@ public class Benchmark {
server.start();
int port = connector.getLocalPort();
- int n = 200000;
- int contentLen = 2048;
+ // Sleep a little
+ Thread.sleep(2000);
+
TestHttpAgent[] agents = new TestHttpAgent[] {
new TestHttpClient3(),
new TestHttpJRE(),
@@ -75,10 +85,6 @@ public class Benchmark {
new TestJettyHttpClient()
};
- for (TestHttpAgent agent: agents) {
- agent.init();
- }
-
byte[] content = new byte[contentLen];
int r = Math.abs(content.hashCode());
for (int i = 0; i < content.length; i++) {
@@ -90,24 +96,30 @@ public class Benchmark {
try {
for (TestHttpAgent agent: agents) {
- System.out.println("=================================");
- System.out.println("HTTP agent: " + agent.getClientName());
- System.out.println("---------------------------------");
- System.out.println(n + " GET requests");
- System.out.println("---------------------------------");
-
- long startTime1 = System.currentTimeMillis();
- Stats stats1 = agent.get(target1, n);
- long finishTime1 = System.currentTimeMillis();
- Stats.printStats(target1, startTime1, finishTime1, stats1);
- System.out.println("---------------------------------");
- System.out.println(n + " POST requests");
- System.out.println("---------------------------------");
-
- long startTime2 = System.currentTimeMillis();
- Stats stats2 = agent.post(target2, content, n);
- long finishTime2 = System.currentTimeMillis();
- Stats.printStats(target2, startTime2, finishTime2, stats2);
+ agent.init();
+ try {
+ System.out.println("=================================");
+ System.out.println("HTTP agent: " + agent.getClientName());
+ System.out.println("---------------------------------");
+ System.out.println(n + " GET requests");
+ System.out.println("---------------------------------");
+
+ long startTime1 = System.currentTimeMillis();
+ Stats stats1 = agent.get(target1, n, c);
+ long finishTime1 = System.currentTimeMillis();
+ Stats.printStats(target1, startTime1, finishTime1, stats1);
+ System.out.println("---------------------------------");
+ System.out.println(n + " POST requests");
+ System.out.println("---------------------------------");
+
+ long startTime2 = System.currentTimeMillis();
+ Stats stats2 = agent.post(target2, content, n, c);
+ long finishTime2 = System.currentTimeMillis();
+ Stats.printStats(target2, startTime2, finishTime2, stats2);
+ } finally {
+ agent.shutdown();
+ }
+ agent.init();
System.out.println("---------------------------------");
}
} finally {
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Stats.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Stats.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Stats.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Stats.java Thu Nov 4 15:48:27 2010
@@ -29,54 +29,68 @@ import java.net.URI;
public class Stats {
+ private final int expectedCount;
+ private final int concurrency;
+
private int successCount = 0;
private int failureCount = 0;
- private String serverName = "unknown";
private long contentLen = 0;
private long totalContentLen = 0;
- public Stats() {
+ public Stats(int expectedCount, int concurrency) {
super();
+ this.expectedCount = expectedCount;
+ this.concurrency = concurrency;
}
- public int getSuccessCount() {
- return successCount;
+ public synchronized boolean isComplete() {
+ return this.successCount + this.failureCount >= this.expectedCount;
}
- public void setSuccessCount(int successCount) {
- this.successCount = successCount;
+ public synchronized void success(long contentLen) {
+ if (isComplete()) return;
+ this.successCount++;
+ this.contentLen = contentLen;
+ this.totalContentLen += contentLen;
+ notifyAll();
}
- public int getFailureCount() {
- return failureCount;
+ public synchronized void failure(long contentLen) {
+ if (isComplete()) return;
+ this.failureCount++;
+ this.contentLen = contentLen;
+ this.totalContentLen += contentLen;
+ notifyAll();
}
- public void setFailureCount(int failureCount) {
- this.failureCount = failureCount;
+ public int getConcurrency() {
+ return this.concurrency;
}
-
- public String getServerName() {
- return serverName;
+
+ public synchronized int getSuccessCount() {
+ return successCount;
}
- public void setServerName(String serverName) {
- this.serverName = serverName;
+ public synchronized int getFailureCount() {
+ return failureCount;
}
- public long getContentLen() {
- return contentLen;
+ public void setFailureCount(int failureCount) {
+ this.failureCount = failureCount;
}
- public void setContentLen(long contentLen) {
- this.contentLen = contentLen;
+ public synchronized long getContentLen() {
+ return contentLen;
}
- public long getTotalContentLen() {
+ public synchronized long getTotalContentLen() {
return totalContentLen;
}
-
- public void setTotalContentLen(long totalContentLen) {
- this.totalContentLen = totalContentLen;
+
+ public synchronized void waitFor() throws InterruptedException {
+ while (!isComplete()) {
+ wait();
+ }
}
public static void printStats(
@@ -85,15 +99,14 @@ public class Stats {
float reqsPerSec = (float) stats.getSuccessCount() / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) stats.getSuccessCount();
- System.out.print("Server Software:\t");
- System.out.println(stats.getServerName());
- System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(stats.getContentLen());
System.out.println(" bytes");
System.out.println();
+ System.out.print("Concurrency level:\t");
+ System.out.println(stats.getConcurrency());
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpAgent.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpAgent.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpAgent.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpAgent.java Thu Nov 4 15:48:27 2010
@@ -31,10 +31,12 @@ public interface TestHttpAgent {
void init() throws Exception;
+ void shutdown() throws Exception;
+
String getClientName();
- Stats get(URI target, int count) throws Exception;
+ Stats get(URI target, int n, int c) throws Exception;
- Stats post(URI target, byte[] content, int n) throws Exception;
+ Stats post(URI target, byte[] content, int n, int c) throws Exception;
}
\ No newline at end of file
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient3.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient3.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient3.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient3.java Thu Nov 4 15:48:27 2010
@@ -29,10 +29,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
-import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
@@ -40,73 +41,107 @@ import org.apache.commons.httpclient.par
public class TestHttpClient3 implements TestHttpAgent {
+ private final MultiThreadedHttpConnectionManager mgr;
private final HttpClient httpclient;
public TestHttpClient3() {
super();
- this.httpclient = new HttpClient();
+ this.mgr = new MultiThreadedHttpConnectionManager();
+ this.httpclient = new HttpClient(this.mgr);
this.httpclient.getParams().setVersion(
HttpVersion.HTTP_1_1);
this.httpclient.getParams().setBooleanParameter(
HttpMethodParams.USE_EXPECT_CONTINUE, false);
this.httpclient.getHttpConnectionManager().getParams()
.setStaleCheckingEnabled(false);
- }
+ this.httpclient.getParams().setSoTimeout(15000);
+
+ HttpMethodRetryHandler retryhandler = new HttpMethodRetryHandler() {
+
+ public boolean retryMethod(final HttpMethod httpmethod, final IOException ex, int count) {
+ return false;
+ }
+
+ };
+ this.httpclient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); }
public void init() {
}
- public Stats execute(final HttpMethod httpmethod, int n) throws Exception {
+ public void shutdown() {
+ this.mgr.shutdown();
+ }
- Stats stats = new Stats();
+ Stats execute(final URI target, final byte[] content, int n, int c) throws Exception {
+ this.mgr.getParams().setMaxTotalConnections(2000);
+ this.mgr.getParams().setDefaultMaxConnectionsPerHost(c);
+ Stats stats = new Stats(n, c);
+ WorkerThread[] workers = new WorkerThread[c];
+ for (int i = 0; i < workers.length; i++) {
+ workers[i] = new WorkerThread(stats, target, content);
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].start();
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].join();
+ }
+ return stats;
+ }
+
+ class WorkerThread extends Thread {
- int successCount = 0;
- int failureCount = 0;
- long contentLen = 0;
- long totalContentLen = 0;
-
- byte[] buffer = new byte[4096];
-
- for (int i = 0; i < n; i++) {
- try {
- this.httpclient.executeMethod(httpmethod);
- InputStream instream = httpmethod.getResponseBodyAsStream();
- contentLen = 0;
- if (instream != null) {
- int l = 0;
- while ((l = instream.read(buffer)) != -1) {
- contentLen += l;
+ private final Stats stats;
+ private final URI target;
+ private final byte[] content;
+
+ WorkerThread(final Stats stats, final URI target, final byte[] content) {
+ super();
+ this.stats = stats;
+ this.target = target;
+ this.content = content;
+ }
+
+ @Override
+ public void run() {
+ byte[] buffer = new byte[4096];
+ while (!this.stats.isComplete()) {
+ HttpMethod httpmethod;
+ if (this.content == null) {
+ GetMethod httpget = new GetMethod(target.toASCIIString());
+ httpmethod = httpget;
+ } else {
+ PostMethod httppost = new PostMethod(target.toASCIIString());
+ httppost.setRequestEntity(new ByteArrayRequestEntity(content));
+ httpmethod = httppost;
+ }
+ long contentLen = 0;
+ try {
+ httpclient.executeMethod(httpmethod);
+ InputStream instream = httpmethod.getResponseBodyAsStream();
+ if (instream != null) {
+ int l = 0;
+ while ((l = instream.read(buffer)) != -1) {
+ contentLen += l;
+ }
}
+ this.stats.success(contentLen);
+ } catch (IOException ex) {
+ this.stats.failure(contentLen);
+ } finally {
+ httpmethod.releaseConnection();
}
- successCount++;
- totalContentLen += contentLen;
- } catch (IOException ex) {
- failureCount++;
- } finally {
- httpmethod.releaseConnection();
}
}
- Header header = httpmethod.getResponseHeader("Server");
- if (header != null) {
- stats.setServerName(header.getValue());
- }
-
- stats.setSuccessCount(successCount);
- stats.setFailureCount(failureCount);
- stats.setContentLen(contentLen);
- stats.setTotalContentLen(totalContentLen);
- return stats;
+
}
- public Stats get(final URI target, int n) throws Exception {
- GetMethod httpget = new GetMethod(target.toASCIIString());
- return execute(httpget, n);
+ public Stats get(final URI target, int n, int c) throws Exception {
+ return execute(target, null, n, c);
}
- public Stats post(URI target, byte[] content, int n) throws Exception {
- PostMethod httppost = new PostMethod(target.toASCIIString());
- httppost.setRequestEntity(new ByteArrayRequestEntity(content));
- return execute(httppost, n);
+ public Stats post(URI target, byte[] content, int n, int c) throws Exception {
+ return execute(target, content, n, c);
}
public String getClientName() {
@@ -115,19 +150,27 @@ public class TestHttpClient3 implements
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.out.println("Usage: <target URI> <no of requests>");
+ System.out.println("Usage: <target URI> <no of requests> <concurrent connections>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
+ int c = 1;
+ if (args.length > 2) {
+ c = Integer.parseInt(args[2]);
+ }
TestHttpClient3 test = new TestHttpClient3();
-
- long startTime = System.currentTimeMillis();
- Stats stats = test.get(targetURI, n);
- long finishTime = System.currentTimeMillis();
-
- Stats.printStats(targetURI, startTime, finishTime, stats);
+ test.init();
+ try {
+ long startTime = System.currentTimeMillis();
+ Stats stats = test.get(targetURI, n, c);
+ long finishTime = System.currentTimeMillis();
+
+ Stats.printStats(targetURI, startTime, finishTime, stats);
+ } finally {
+ test.shutdown();
+ }
}
}
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java Thu Nov 4 15:48:27 2010
@@ -29,25 +29,31 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
-import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
+import org.apache.http.protocol.HttpContext;
import org.apache.http.util.VersionInfo;
public class TestHttpClient4 implements TestHttpAgent {
- private final HttpClient httpclient;
+ private final ThreadSafeClientConnManager mgr;
+ private final DefaultHttpClient httpclient;
public TestHttpClient4() {
super();
@@ -60,66 +66,107 @@ public class TestHttpClient4 implements
false);
params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
8 * 1024);
-
- this.httpclient = new DefaultHttpClient(params);
+ params.setIntParameter(HttpConnectionParams.SO_TIMEOUT,
+ 15000);
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
+ schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
+ schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
+ this.mgr = new ThreadSafeClientConnManager(schemeRegistry);
+ this.httpclient = new DefaultHttpClient(this.mgr, params);
+ this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
+
+ public boolean retryRequest(
+ final IOException exception, int executionCount, final HttpContext context) {
+ return false;
+ }
+
+ });
}
public void init() {
}
- public Stats execute(final HttpUriRequest request, int n) throws Exception {
- Stats stats = new Stats();
+ public void shutdown() {
+ this.mgr.shutdown();
+ }
+
+ Stats execute(final URI target, final byte[] content, int n, int c) throws Exception {
+ this.mgr.setMaxTotal(2000);
+ this.mgr.setDefaultMaxPerRoute(c);
+ Stats stats = new Stats(n, c);
+ WorkerThread[] workers = new WorkerThread[c];
+ for (int i = 0; i < workers.length; i++) {
+ workers[i] = new WorkerThread(stats, target, content);
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].start();
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].join();
+ }
+ return stats;
+ }
+
+ class WorkerThread extends Thread {
- int successCount = 0;
- int failureCount = 0;
- long contentLen = 0;
- long totalContentLen = 0;
-
- byte[] buffer = new byte[4096];
-
- for (int i = 0; i < n; i++) {
- HttpResponse response = this.httpclient.execute(request);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- InputStream instream = entity.getContent();
+ private final Stats stats;
+ private final URI target;
+ private final byte[] content;
+
+ WorkerThread(final Stats stats, final URI target, final byte[] content) {
+ super();
+ this.stats = stats;
+ this.target = target;
+ this.content = content;
+ }
+
+ @Override
+ public void run() {
+ byte[] buffer = new byte[4096];
+ while (!this.stats.isComplete()) {
+ HttpUriRequest request;
+ if (this.content == null) {
+ HttpGet httpget = new HttpGet(target);
+ request = httpget;
+ } else {
+ HttpPost httppost = new HttpPost(target);
+ httppost.setEntity(new ByteArrayEntity(content));
+ request = httppost;
+ }
+ long contentLen = 0;
try {
- contentLen = 0;
- if (instream != null) {
- int l = 0;
- while ((l = instream.read(buffer)) != -1) {
- contentLen += l;
+ HttpResponse response = httpclient.execute(request);
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+ InputStream instream = entity.getContent();
+ try {
+ contentLen = 0;
+ if (instream != null) {
+ int l = 0;
+ while ((l = instream.read(buffer)) != -1) {
+ contentLen += l;
+ }
+ }
+ } finally {
+ instream.close();
}
}
- successCount++;
- totalContentLen += contentLen;
+ this.stats.success(contentLen);
} catch (IOException ex) {
+ this.stats.failure(contentLen);
request.abort();
- failureCount++;
- } finally {
- instream.close();
}
}
- Header header = response.getFirstHeader("Server");
- if (header != null) {
- stats.setServerName(header.getValue());
- }
}
- stats.setSuccessCount(successCount);
- stats.setFailureCount(failureCount);
- stats.setContentLen(contentLen);
- stats.setTotalContentLen(totalContentLen);
- return stats;
+
}
-
- public Stats get(final URI target, int n) throws Exception {
- HttpGet httpget = new HttpGet(target);
- return execute(httpget, n);
+
+ public Stats get(final URI target, int n, int c) throws Exception {
+ return execute(target, null, n ,c);
}
- public Stats post(final URI target, byte[] content, int n) throws Exception {
- HttpPost httppost = new HttpPost(target);
- httppost.setEntity(new ByteArrayEntity(content));
- return execute(httppost, n);
+ public Stats post(final URI target, byte[] content, int n, int c) throws Exception {
+ return execute(target, content, n, c);
}
public String getClientName() {
@@ -131,19 +178,27 @@ public class TestHttpClient4 implements
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.out.println("Usage: <target URI> <no of requests>");
+ System.out.println("Usage: <target URI> <no of requests> <concurrent connections>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
+ int c = 1;
+ if (args.length > 2) {
+ c = Integer.parseInt(args[2]);
+ }
TestHttpClient4 test = new TestHttpClient4();
-
- long startTime = System.currentTimeMillis();
- Stats stats = test.get(targetURI, n);
- long finishTime = System.currentTimeMillis();
-
- Stats.printStats(targetURI, startTime, finishTime, stats);
+ test.init();
+ try {
+ long startTime = System.currentTimeMillis();
+ Stats stats = test.get(targetURI, n, c);
+ long finishTime = System.currentTimeMillis();
+
+ Stats.printStats(targetURI, startTime, finishTime, stats);
+ } finally {
+ test.shutdown();
+ }
}
}
\ No newline at end of file
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java Thu Nov 4 15:48:27 2010
@@ -31,9 +31,9 @@ import java.net.Socket;
import java.net.URI;
import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
@@ -78,6 +78,8 @@ public class TestHttpCore implements Tes
false);
this.params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
8 * 1024);
+ this.params.setIntParameter(HttpConnectionParams.SO_TIMEOUT,
+ 15000);
this.httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
new RequestContent(),
@@ -94,75 +96,10 @@ public class TestHttpCore implements Tes
public void init() {
}
- public Stats execute(
- final HttpHost targetHost, final HttpRequest request, int n) throws Exception {
-
- Stats stats = new Stats();
-
- int successCount = 0;
- int failureCount = 0;
- long contentLen = 0;
- long totalContentLen = 0;
-
- byte[] buffer = new byte[4096];
- HttpContext context = new BasicHttpContext();
-
- DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
- for (int i = 0; i < n; i++) {
- if (!conn.isOpen()) {
- Socket socket = new Socket(
- targetHost.getHostName(),
- targetHost.getPort() > 0 ? targetHost.getPort() : 80);
- conn.bind(socket, this.params);
- }
-
- context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
- context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
-
- this.httpexecutor.preProcess(request, this.httpproc, context);
- HttpResponse response = this.httpexecutor.execute(request, conn, context);
- this.httpexecutor.postProcess(response, this.httpproc, context);
-
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- InputStream instream = entity.getContent();
- try {
- contentLen = 0;
- if (instream != null) {
- int l = 0;
- while ((l = instream.read(buffer)) != -1) {
- contentLen += l;
- }
- }
- successCount++;
- totalContentLen += contentLen;
- } catch (IOException ex) {
- conn.shutdown();
- failureCount++;
- } finally {
- instream.close();
- }
- }
- if (!this.connStrategy.keepAlive(response, context)) {
- conn.close();
- }
- Header header = response.getFirstHeader("Server");
- if (header != null) {
- stats.setServerName(header.getValue());
- }
- for (HeaderIterator it = request.headerIterator(); it.hasNext();) {
- it.next();
- it.remove();
- }
- }
- stats.setSuccessCount(successCount);
- stats.setFailureCount(failureCount);
- stats.setContentLen(contentLen);
- stats.setTotalContentLen(totalContentLen);
- return stats;
+ public void shutdown() {
}
- public Stats get(final URI target, int n) throws Exception {
+ Stats execute(final URI target, final byte[] content, int n, int c) throws Exception {
HttpHost targetHost = new HttpHost(target.getHost(), target.getPort());
StringBuilder buffer = new StringBuilder();
buffer.append(target.getPath());
@@ -170,22 +107,116 @@ public class TestHttpCore implements Tes
buffer.append("?");
buffer.append(target.getQuery());
}
- BasicHttpRequest httpget = new BasicHttpRequest("GET", buffer.toString());
- return execute(targetHost, httpget, n);
+ String requestUri = buffer.toString();
+
+ Stats stats = new Stats(n, c);
+ WorkerThread[] workers = new WorkerThread[c];
+ for (int i = 0; i < workers.length; i++) {
+ workers[i] = new WorkerThread(stats, targetHost, requestUri, content);
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].start();
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].join();
+ }
+ return stats;
}
- public Stats post(final URI target, byte[] content, int n) throws Exception {
- HttpHost targetHost = new HttpHost(target.getHost(), target.getPort());
- StringBuilder buffer = new StringBuilder();
- buffer.append(target.getPath());
- if (target.getQuery() != null) {
- buffer.append("?");
- buffer.append(target.getQuery());
+ class WorkerThread extends Thread {
+
+ private final Stats stats;
+ private final HttpHost targetHost;
+ private final String requestUri;
+ private final byte[] content;
+
+ WorkerThread(final Stats stats,
+ final HttpHost targetHost, final String requestUri, final byte[] content) {
+ super();
+ this.stats = stats;
+ this.targetHost = targetHost;
+ this.requestUri = requestUri;
+ this.content = content;
}
- BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST",
- buffer.toString());
- httppost.setEntity(new ByteArrayEntity(content));
- return execute(targetHost, httppost, n);
+
+ @Override
+ public void run() {
+ byte[] buffer = new byte[4096];
+ HttpContext context = new BasicHttpContext();
+ DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+ try {
+ while (!this.stats.isComplete()) {
+ HttpRequest request;
+ if (this.content == null) {
+ BasicHttpRequest httpget = new BasicHttpRequest("GET", this.requestUri);
+ request = httpget;
+ } else {
+ BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST",
+ this.requestUri);
+ httppost.setEntity(new ByteArrayEntity(this.content));
+ request = httppost;
+ }
+
+ long contentLen = 0;
+ try {
+ if (!conn.isOpen()) {
+ Socket socket = new Socket(
+ this.targetHost.getHostName(),
+ this.targetHost.getPort() > 0 ? this.targetHost.getPort() : 80);
+ conn.bind(socket, params);
+ }
+
+ context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+ context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
+
+ httpexecutor.preProcess(request, httpproc, context);
+ HttpResponse response = httpexecutor.execute(request, conn, context);
+ httpexecutor.postProcess(response, httpproc, context);
+
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+ InputStream instream = entity.getContent();
+ try {
+ contentLen = 0;
+ if (instream != null) {
+ int l = 0;
+ while ((l = instream.read(buffer)) != -1) {
+ contentLen += l;
+ }
+ }
+ } finally {
+ instream.close();
+ }
+ }
+ if (!connStrategy.keepAlive(response, context)) {
+ conn.close();
+ }
+ for (HeaderIterator it = request.headerIterator(); it.hasNext();) {
+ it.next();
+ it.remove();
+ }
+ this.stats.success(contentLen);
+ } catch (IOException ex) {
+ this.stats.failure(contentLen);
+ } catch (HttpException ex) {
+ this.stats.failure(contentLen);
+ }
+ }
+ } finally {
+ try {
+ conn.shutdown();
+ } catch (IOException ignore) {}
+ }
+ }
+
+ }
+
+ public Stats get(final URI target, int n, int c) throws Exception {
+ return execute(target, null, n, c);
+ }
+
+ public Stats post(final URI target, byte[] content, int n, int c) throws Exception {
+ return execute(target, content, n, c);
}
public String getClientName() {
@@ -197,19 +228,27 @@ public class TestHttpCore implements Tes
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.out.println("Usage: <target URI> <no of requests>");
+ System.out.println("Usage: <target URI> <no of requests> <concurrent connections>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
+ int c = 1;
+ if (args.length > 2) {
+ c = Integer.parseInt(args[2]);
+ }
TestHttpCore test = new TestHttpCore();
-
- long startTime = System.currentTimeMillis();
- Stats stats = test.get(targetURI, n);
- long finishTime = System.currentTimeMillis();
-
- Stats.printStats(targetURI, startTime, finishTime, stats);
+ test.init();
+ try {
+ long startTime = System.currentTimeMillis();
+ Stats stats = test.get(targetURI, n, c);
+ long finishTime = System.currentTimeMillis();
+
+ Stats.printStats(targetURI, startTime, finishTime, stats);
+ } finally {
+ test.shutdown();
+ }
}
}
\ No newline at end of file
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpJRE.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpJRE.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpJRE.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpJRE.java Thu Nov 4 15:48:27 2010
@@ -41,68 +41,91 @@ public class TestHttpJRE implements Test
public void init() {
}
- public Stats execute(final URI targetURI, byte[] content, int n) throws Exception {
+ public void shutdown() {
+ }
+
+ Stats execute(final URI targetURI, byte[] content, int n, int c) throws Exception {
+ System.setProperty("http.maxConnections", Integer.toString(c));
+ URL target = targetURI.toURL();
+ Stats stats = new Stats(n, c);
+ WorkerThread[] workers = new WorkerThread[c];
+ for (int i = 0; i < workers.length; i++) {
+ workers[i] = new WorkerThread(stats, target, content);
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].start();
+ }
+ for (int i = 0; i < workers.length; i++) {
+ workers[i].join();
+ }
+ return stats;
+ }
- Stats stats = new Stats();
+ class WorkerThread extends Thread {
- int successCount = 0;
- int failureCount = 0;
- long contentLen = 0;
- long totalContentLen = 0;
-
- byte[] buffer = new byte[4096];
-
- URL url = targetURI.toURL();
- for (int i = 0; i < n; i++) {
- HttpURLConnection c = (HttpURLConnection) url.openConnection();
-
- if (content != null) {
- c.setRequestMethod("POST");
- c.setFixedLengthStreamingMode(content.length);
- c.setUseCaches (false);
- c.setDoInput(true);
- c.setDoOutput(true);
- OutputStream out = c.getOutputStream();
- out.write(content);
- out.flush ();
- out.close();
- }
- InputStream instream = c.getInputStream();
- try {
- contentLen = 0;
- if (instream != null) {
- int l = 0;
- while ((l = instream.read(buffer)) != -1) {
- contentLen += l;
+ private final Stats stats;
+ private final URL target;
+ private final byte[] content;
+
+ WorkerThread(final Stats stats, final URL target, final byte[] content) {
+ super();
+ this.stats = stats;
+ this.target = target;
+ this.content = content;
+ }
+
+ @Override
+ public void run() {
+ byte[] buffer = new byte[4096];
+ while (!this.stats.isComplete()) {
+ long contentLen = 0;
+ try {
+ HttpURLConnection conn = (HttpURLConnection) this.target.openConnection();
+ conn.setReadTimeout(15000);
+
+ if (this.content != null) {
+ conn.setRequestMethod("POST");
+ conn.setFixedLengthStreamingMode(this.content.length);
+ conn.setUseCaches (false);
+ conn.setDoInput(true);
+ conn.setDoOutput(true);
+ OutputStream out = conn.getOutputStream();
+ try {
+ out.write(this.content);
+ out.flush ();
+ } finally {
+ out.close();
+ }
}
+ InputStream instream = conn.getInputStream();
+ if (instream != null) {
+ try {
+ int l = 0;
+ while ((l = instream.read(buffer)) != -1) {
+ contentLen += l;
+ }
+ } finally {
+ instream.close();
+ }
+ }
+ if (conn.getResponseCode() == 200) {
+ this.stats.success(contentLen);
+ } else {
+ this.stats.failure(contentLen);
+ }
+ } catch (IOException ex) {
+ this.stats.failure(contentLen);
}
- if (c.getResponseCode() == 200) {
- successCount++;
- } else {
- failureCount++;
- }
- totalContentLen += contentLen;
- } catch (IOException ex) {
- failureCount++;
- }
- String s = c.getHeaderField("Server");
- if (s != null) {
- stats.setServerName(s);
}
}
- stats.setSuccessCount(successCount);
- stats.setFailureCount(failureCount);
- stats.setContentLen(contentLen);
- stats.setTotalContentLen(totalContentLen);
- return stats;
}
-
- public Stats get(final URI target, int n) throws Exception {
- return execute(target, null, n);
+
+ public Stats get(final URI target, int n, int c) throws Exception {
+ return execute(target, null, n, c);
}
- public Stats post(final URI target, byte[] content, int n) throws Exception {
- return execute(target, content, n);
+ public Stats post(final URI target, byte[] content, int n, int c) throws Exception {
+ return execute(target, content, n, c);
}
public String getClientName() {
@@ -111,19 +134,27 @@ public class TestHttpJRE implements Test
public static void main(String[] args) throws Exception {
if (args.length < 2) {
- System.out.println("Usage: <target URI> <no of requests>");
+ System.out.println("Usage: <target URI> <no of requests> <concurrent connections>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
+ int c = 1;
+ if (args.length > 2) {
+ c = Integer.parseInt(args[2]);
+ }
TestHttpJRE test = new TestHttpJRE();
-
- long startTime = System.currentTimeMillis();
- Stats stats = test.get(targetURI, n);
- long finishTime = System.currentTimeMillis();
-
- Stats.printStats(targetURI, startTime, finishTime, stats);
+ test.init();
+ try {
+ long startTime = System.currentTimeMillis();
+ Stats stats = test.get(targetURI, n, c);
+ long finishTime = System.currentTimeMillis();
+
+ Stats.printStats(targetURI, startTime, finishTime, stats);
+ } finally {
+ test.shutdown();
+ }
}
}
\ No newline at end of file
Modified: httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java?rev=1031044&r1=1031043&r2=1031044&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java Thu Nov 4 15:48:27 2010
@@ -41,92 +41,64 @@ public class TestJettyHttpClient impleme
public TestJettyHttpClient() {
super();
this.client = new HttpClient();
- this.client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
this.client.setRequestBufferSize(8 * 1024);
this.client.setResponseBufferSize(8 * 1024);
+ this.client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
+ this.client.setTimeout(15000);
}
public void init() throws Exception {
this.client.start();
}
- public Stats execute(final URI targetURI, byte[] content, int n) throws Exception {
-
- Stats stats = new Stats();
+ public void shutdown() throws Exception {
+ this.client.stop();
+ }
- int successCount = 0;
- int failureCount = 0;
- long totalContentLen = 0;
+ Stats execute(final URI targetURI, byte[] content, int n, int c) throws Exception {
+ this.client.setMaxConnectionsPerAddress(c);
+ Stats stats = new Stats(n, c);
for (int i = 0; i < n; i++) {
- SimpleHttpExchange exchange = new SimpleHttpExchange();
+ SimpleHttpExchange exchange = new SimpleHttpExchange(stats);
exchange.setURL(targetURI.toASCIIString());
if (content != null) {
exchange.setMethod("POST");
exchange.setRequestContent(new ByteArrayBuffer(content));
}
-
try {
this.client.send(exchange);
- exchange.waitForDone();
- if (exchange.status == 200) {
- successCount++;
- } else {
- failureCount++;
- }
- stats.setContentLen(exchange.contentLen);
- totalContentLen += exchange.contentLen;
} catch (IOException ex) {
- failureCount++;
- }
- if (exchange.server != null) {
- stats.setServerName(exchange.server);
}
}
- stats.setSuccessCount(successCount);
- stats.setFailureCount(failureCount);
- stats.setTotalContentLen(totalContentLen);
+ stats.waitFor();
return stats;
}
- public Stats get(final URI target, int n) throws Exception {
- return execute(target, null, n);
+ public Stats get(final URI target, int n, int c) throws Exception {
+ return execute(target, null, n, c);
}
- public Stats post(final URI target, byte[] content, int n) throws Exception {
- return execute(target, content, n);
+ public Stats post(final URI target, byte[] content, int n, int c) throws Exception {
+ return execute(target, content, n, c);
}
public String getClientName() {
return "Jetty " + Server.getVersion();
}
- public static void main(String[] args) throws Exception {
- if (args.length < 2) {
- System.out.println("Usage: <target URI> <no of requests>");
- System.exit(-1);
- }
- URI targetURI = new URI(args[0]);
- int n = Integer.parseInt(args[1]);
-
- TestJettyHttpClient test = new TestJettyHttpClient();
-
- long startTime = System.currentTimeMillis();
- Stats stats = test.get(targetURI, n);
- long finishTime = System.currentTimeMillis();
-
- Stats.printStats(targetURI, startTime, finishTime, stats);
- }
-
static class SimpleHttpExchange extends HttpExchange {
- private final Buffer serverHeader = new ByteArrayBuffer("Server");
-
- long contentLen = 0;
- int status = 0;
- String server;
-
+ private final Stats stats;
+ private int status = 0;
+ private long contentLen = 0;
+
+ SimpleHttpExchange(final Stats stats) {
+ super();
+ this.stats = stats;
+ }
+
protected void onResponseStatus(
final Buffer version, int status, final Buffer reason) throws IOException {
this.status = status;
@@ -134,19 +106,58 @@ public class TestJettyHttpClient impleme
}
@Override
- protected void onResponseHeader(final Buffer name, final Buffer value) throws IOException {
- super.onResponseHeader(name, value);
- if (name.equalsIgnoreCase(this.serverHeader)) {
- this.server = value.toString("ASCII");
+ protected void onResponseContent(final Buffer content) throws IOException {
+ this.contentLen += content.length();
+ super.onResponseContent(content);
+ }
+
+ @Override
+ protected void onResponseComplete() throws IOException {
+ if (this.status == 200) {
+ this.stats.success(this.contentLen);
+ } else {
+ this.stats.failure(this.contentLen);
}
+ super.onResponseComplete();
}
@Override
- protected void onResponseContent(final Buffer content) throws IOException {
- this.contentLen += content.length();
- super.onResponseContent(content);
+ protected void onConnectionFailed(final Throwable x) {
+ this.stats.failure(this.contentLen);
+ super.onConnectionFailed(x);
}
+ @Override
+ protected void onException(final Throwable x) {
+ this.stats.failure(this.contentLen);
+ super.onException(x);
+ }
+
};
+ public static void main(String[] args) throws Exception {
+ if (args.length < 2) {
+ System.out.println("Usage: <target URI> <no of requests> <concurrent connections>");
+ System.exit(-1);
+ }
+ URI targetURI = new URI(args[0]);
+ int n = Integer.parseInt(args[1]);
+ int c = 1;
+ if (args.length > 2) {
+ c = Integer.parseInt(args[2]);
+ }
+
+ TestJettyHttpClient test = new TestJettyHttpClient();
+ test.init();
+ try {
+ long startTime = System.currentTimeMillis();
+ Stats stats = test.get(targetURI, n, c);
+ long finishTime = System.currentTimeMillis();
+
+ Stats.printStats(targetURI, startTime, finishTime, stats);
+ } finally {
+ test.shutdown();
+ }
+ }
+
}
\ No newline at end of file