From e461c10cecad2adffbb05c107f6067dc614fad45 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 9 Aug 2011 11:11:21 +0000 Subject: [PATCH] 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 --- httpclient-benchmark/pom.xml | 8 +- .../client/benchmark/TestHttpClient4.java | 6 +- .../http/client/benchmark/TestHttpCore.java | 74 ++++++++++--------- .../client/benchmark/TestNingHttpClient.java | 2 +- 4 files changed, 46 insertions(+), 44 deletions(-) diff --git a/httpclient-benchmark/pom.xml b/httpclient-benchmark/pom.xml index e21c9bd80..6c0ee7be9 100644 --- a/httpclient-benchmark/pom.xml +++ b/httpclient-benchmark/pom.xml @@ -59,12 +59,6 @@ ${project.version} compile - - org.apache.httpcomponents - httpcore - 4.1.1 - compile - commons-httpclient commons-httpclient @@ -98,7 +92,7 @@ com.ning async-http-client - 1.5.0 + 1.6.4 compile diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java index eb14f85c5..abe42c587 100644 --- a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpClient4.java @@ -42,7 +42,7 @@ 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.impl.conn.PoolingClientConnectionManager; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; @@ -52,7 +52,7 @@ import org.apache.http.util.VersionInfo; public class TestHttpClient4 implements TestHttpAgent { - private final ThreadSafeClientConnManager mgr; + private final PoolingClientConnectionManager mgr; private final DefaultHttpClient httpclient; public TestHttpClient4() { @@ -71,7 +71,7 @@ public class TestHttpClient4 implements TestHttpAgent { 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.mgr = new PoolingClientConnectionManager(schemeRegistry); this.httpclient = new DefaultHttpClient(this.mgr, params); this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java index 7d53b44af..77df3a24c 100644 --- a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestHttpCore.java @@ -27,11 +27,13 @@ package org.apache.http.client.benchmark; import java.io.IOException; import java.io.InputStream; -import java.net.Socket; import java.net.URI; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HeaderIterator; +import org.apache.http.HttpClientConnection; import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -41,7 +43,8 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.entity.ByteArrayEntity; 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.BasicHttpRequest; import org.apache.http.params.HttpConnectionParams; @@ -66,6 +69,7 @@ public class TestHttpCore implements TestHttpAgent { private final HttpProcessor httpproc; private final HttpRequestExecutor httpexecutor; private final ConnectionReuseStrategy connStrategy; + private final BasicConnPool pool; public TestHttpCore() { super(); @@ -91,6 +95,8 @@ public class TestHttpCore implements TestHttpAgent { this.httpexecutor = new HttpRequestExecutor(); this.connStrategy = new DefaultConnectionReuseStrategy(); + + this.pool = new BasicConnPool(this.params); } 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 { + this.pool.setMaxTotal(2000); + this.pool.setDefaultMaxPerRoute(c); HttpHost targetHost = new HttpHost(target.getHost(), target.getPort()); StringBuilder buffer = new StringBuilder(); buffer.append(target.getPath()); @@ -143,29 +151,27 @@ public class TestHttpCore implements TestHttpAgent { 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; + 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 future = pool.lease(targetHost, null); + try { + BasicPoolEntry entry = future.get(); try { - if (!conn.isOpen()) { - Socket socket = new Socket( - this.targetHost.getHostName(), - this.targetHost.getPort() > 0 ? this.targetHost.getPort() : 80); - conn.bind(socket, params); - } - + HttpClientConnection conn = entry.getConnection(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost); @@ -188,8 +194,8 @@ public class TestHttpCore implements TestHttpAgent { instream.close(); } } - if (!connStrategy.keepAlive(response, context)) { - conn.close(); + if (connStrategy.keepAlive(response, context)) { + reusable = true; } for (HeaderIterator it = request.headerIterator(); it.hasNext();) { it.next(); @@ -200,16 +206,18 @@ public class TestHttpCore implements TestHttpAgent { } else { this.stats.failure(contentLen); } - } catch (IOException ex) { - this.stats.failure(contentLen); - } catch (HttpException ex) { - this.stats.failure(contentLen); + } finally { + pool.release(entry, reusable); } + } 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) {} } } diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java index 4e8de960c..28b43377d 100644 --- a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java @@ -94,7 +94,7 @@ public class TestNingHttpClient implements TestHttpAgent { } public String getClientName() { - return "Ning async HTTP client 1.5.0"; + return "Ning async HTTP client 1.6.4"; } static class SimpleAsyncHandler implements AsyncHandler {