Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 311171056E for ; Thu, 2 May 2013 10:28:57 +0000 (UTC) Received: (qmail 64232 invoked by uid 500); 2 May 2013 10:28:56 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 64193 invoked by uid 500); 2 May 2013 10:28:56 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 64181 invoked by uid 99); 2 May 2013 10:28:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 May 2013 10:28:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 May 2013 10:28:55 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 65D8723889FA for ; Thu, 2 May 2013 10:28:35 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1478315 - in /httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark: TestJettyHttpClient.java TestNingHttpClient.java Date: Thu, 02 May 2013 10:28:35 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130502102835.65D8723889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Thu May 2 10:28:34 2013 New Revision: 1478315 URL: http://svn.apache.org/r1478315 Log: Use semaphore to ensure concurrency limit for async clients and avoid flooding them with too many execution requests Modified: httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java Modified: httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java URL: http://svn.apache.org/viewvc/httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java?rev=1478315&r1=1478314&r2=1478315&view=diff ============================================================================== --- httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java (original) +++ httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java Thu May 2 10:28:34 2013 @@ -27,6 +27,7 @@ package org.apache.http.client.benchmark import java.io.IOException; import java.net.URI; +import java.util.concurrent.Semaphore; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpExchange; @@ -60,9 +61,10 @@ public class TestJettyHttpClient impleme Stats execute(final URI targetURI, final byte[] content, final int n, final int c) throws Exception { this.client.setMaxConnectionsPerAddress(c); final Stats stats = new Stats(n, c); - + final Semaphore semaphore = new Semaphore(c); for (int i = 0; i < n; i++) { - final SimpleHttpExchange exchange = new SimpleHttpExchange(stats); + semaphore.acquire(); + final SimpleHttpExchange exchange = new SimpleHttpExchange(stats, semaphore); exchange.setURL(targetURI.toASCIIString()); if (content != null) { @@ -72,6 +74,7 @@ public class TestJettyHttpClient impleme try { this.client.send(exchange); } catch (final IOException ex) { + semaphore.release(); stats.failure(0L); } } @@ -97,12 +100,15 @@ public class TestJettyHttpClient impleme static class SimpleHttpExchange extends HttpExchange { private final Stats stats; + private final Semaphore semaphore; + private int status = 0; private long contentLen = 0; - SimpleHttpExchange(final Stats stats) { + SimpleHttpExchange(final Stats stats, final Semaphore semaphore) { super(); this.stats = stats; + this.semaphore = semaphore; } @Override @@ -122,6 +128,7 @@ public class TestJettyHttpClient impleme @Override protected void onResponseComplete() throws IOException { + this.semaphore.release(); if (this.status == 200) { this.stats.success(this.contentLen); } else { @@ -132,12 +139,14 @@ public class TestJettyHttpClient impleme @Override protected void onConnectionFailed(final Throwable x) { + this.semaphore.release(); this.stats.failure(this.contentLen); super.onConnectionFailed(x); } @Override protected void onException(final Throwable x) { + this.semaphore.release(); this.stats.failure(this.contentLen); super.onException(x); } Modified: httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java URL: http://svn.apache.org/viewvc/httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java?rev=1478315&r1=1478314&r2=1478315&view=diff ============================================================================== --- httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java (original) +++ httpcomponents/benchmark/httpclient/trunk/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java Thu May 2 10:28:34 2013 @@ -27,6 +27,7 @@ package org.apache.http.client.benchmark import java.io.IOException; import java.net.URI; +import java.util.concurrent.Semaphore; import com.ning.http.client.AsyncHandler; import com.ning.http.client.AsyncHttpClient; @@ -68,7 +69,9 @@ public class TestNingHttpClient implemen final Stats stats = new Stats(n, c); + final Semaphore semaphore = new Semaphore(c); for (int i = 0; i < n; i++) { + semaphore.acquire(); Request request; if (content == null) { request = this.client.prepareGet(targetURI.toASCIIString()) @@ -79,8 +82,10 @@ public class TestNingHttpClient implemen .build(); } try { - this.client.executeRequest(request, new SimpleAsyncHandler(stats)); + this.client.executeRequest(request, new SimpleAsyncHandler(stats, semaphore)); } catch (final IOException ex) { + semaphore.release(); + stats.failure(0L); } } stats.waitFor(); @@ -105,12 +110,15 @@ public class TestNingHttpClient implemen static class SimpleAsyncHandler implements AsyncHandler { private final Stats stats; + private final Semaphore semaphore; + private int status = 0; private long contentLen = 0; - SimpleAsyncHandler(final Stats stats) { + SimpleAsyncHandler(final Stats stats, final Semaphore semaphore) { super(); this.stats = stats; + this.semaphore = semaphore; } @Override @@ -132,6 +140,7 @@ public class TestNingHttpClient implemen @Override public Object onCompleted() throws Exception { + this.semaphore.release(); if (this.status == 200) { this.stats.success(this.contentLen); } else { @@ -142,6 +151,7 @@ public class TestNingHttpClient implemen @Override public void onThrowable(final Throwable t) { + this.semaphore.release(); this.stats.failure(this.contentLen); }