HttpCore benchmark sampler to make use of a connection pool; upgraded Ning Http Client to version 1.6.4

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/conn-mgmt-redesign@1155309 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-08-09 11:11:21 +00:00
parent 6be907264f
commit e461c10cec
4 changed files with 46 additions and 44 deletions

View File

@ -59,12 +59,6 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>commons-httpclient</groupId> <groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId> <artifactId>commons-httpclient</artifactId>
@ -98,7 +92,7 @@
<dependency> <dependency>
<groupId>com.ning</groupId> <groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId> <artifactId>async-http-client</artifactId>
<version>1.5.0</version> <version>1.6.4</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -42,7 +42,7 @@ import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams; import org.apache.http.params.HttpProtocolParams;
@ -52,7 +52,7 @@ import org.apache.http.util.VersionInfo;
public class TestHttpClient4 implements TestHttpAgent { public class TestHttpClient4 implements TestHttpAgent {
private final ThreadSafeClientConnManager mgr; private final PoolingClientConnectionManager mgr;
private final DefaultHttpClient httpclient; private final DefaultHttpClient httpclient;
public TestHttpClient4() { public TestHttpClient4() {
@ -71,7 +71,7 @@ public class TestHttpClient4 implements TestHttpAgent {
SchemeRegistry schemeRegistry = new SchemeRegistry(); SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
this.mgr = new ThreadSafeClientConnManager(schemeRegistry); this.mgr = new PoolingClientConnectionManager(schemeRegistry);
this.httpclient = new DefaultHttpClient(this.mgr, params); this.httpclient = new DefaultHttpClient(this.mgr, params);
this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {

View File

@ -27,11 +27,13 @@ package org.apache.http.client.benchmark;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.Socket;
import java.net.URI; import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.http.ConnectionReuseStrategy; import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HeaderIterator; import org.apache.http.HeaderIterator;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
@ -41,7 +43,8 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection; import org.apache.http.impl.pool.BasicConnPool;
import org.apache.http.impl.pool.BasicPoolEntry;
import org.apache.http.message.BasicHttpEntityEnclosingRequest; import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
@ -66,6 +69,7 @@ public class TestHttpCore implements TestHttpAgent {
private final HttpProcessor httpproc; private final HttpProcessor httpproc;
private final HttpRequestExecutor httpexecutor; private final HttpRequestExecutor httpexecutor;
private final ConnectionReuseStrategy connStrategy; private final ConnectionReuseStrategy connStrategy;
private final BasicConnPool pool;
public TestHttpCore() { public TestHttpCore() {
super(); super();
@ -91,6 +95,8 @@ public class TestHttpCore implements TestHttpAgent {
this.httpexecutor = new HttpRequestExecutor(); this.httpexecutor = new HttpRequestExecutor();
this.connStrategy = new DefaultConnectionReuseStrategy(); this.connStrategy = new DefaultConnectionReuseStrategy();
this.pool = new BasicConnPool(this.params);
} }
public void init() { public void init() {
@ -100,6 +106,8 @@ public class TestHttpCore implements TestHttpAgent {
} }
Stats execute(final URI target, final byte[] content, int n, int c) throws Exception { Stats execute(final URI target, final byte[] content, int n, int c) throws Exception {
this.pool.setMaxTotal(2000);
this.pool.setDefaultMaxPerRoute(c);
HttpHost targetHost = new HttpHost(target.getHost(), target.getPort()); HttpHost targetHost = new HttpHost(target.getHost(), target.getPort());
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
buffer.append(target.getPath()); buffer.append(target.getPath());
@ -143,29 +151,27 @@ public class TestHttpCore implements TestHttpAgent {
public void run() { public void run() {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
HttpContext context = new BasicHttpContext(); 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; 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;
boolean reusable = false;
Future<BasicPoolEntry> future = pool.lease(targetHost, null);
try {
BasicPoolEntry entry = future.get();
try { try {
if (!conn.isOpen()) { HttpClientConnection conn = entry.getConnection();
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_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
@ -188,8 +194,8 @@ public class TestHttpCore implements TestHttpAgent {
instream.close(); instream.close();
} }
} }
if (!connStrategy.keepAlive(response, context)) { if (connStrategy.keepAlive(response, context)) {
conn.close(); reusable = true;
} }
for (HeaderIterator it = request.headerIterator(); it.hasNext();) { for (HeaderIterator it = request.headerIterator(); it.hasNext();) {
it.next(); it.next();
@ -200,16 +206,18 @@ public class TestHttpCore implements TestHttpAgent {
} else { } else {
this.stats.failure(contentLen); this.stats.failure(contentLen);
} }
} catch (IOException ex) { } finally {
this.stats.failure(contentLen); pool.release(entry, reusable);
} catch (HttpException ex) {
this.stats.failure(contentLen);
} }
} catch (InterruptedException ex) {
this.stats.failure(contentLen);
} catch (ExecutionException ex) {
this.stats.failure(contentLen);
} catch (IOException ex) {
this.stats.failure(contentLen);
} catch (HttpException ex) {
this.stats.failure(contentLen);
} }
} finally {
try {
conn.shutdown();
} catch (IOException ignore) {}
} }
} }

View File

@ -94,7 +94,7 @@ public class TestNingHttpClient implements TestHttpAgent {
} }
public String getClientName() { public String getClientName() {
return "Ning async HTTP client 1.5.0"; return "Ning async HTTP client 1.6.4";
} }
static class SimpleAsyncHandler implements AsyncHandler<Object> { static class SimpleAsyncHandler implements AsyncHandler<Object> {