hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
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 GMT
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<Object> {
 
         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);
         }
 



Mime
View raw message