hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r233341 - /jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java
Date Thu, 18 Aug 2005 18:03:10 GMT
Author: olegk
Date: Thu Aug 18 11:03:07 2005
New Revision: 233341

URL: http://svn.apache.org/viewcvs?rev=233341&view=rev
Log:
Added an option to switch oldIO and NIO HTTP data receivers / transmitters

Modified:
    jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java

Modified: jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java?rev=233341&r1=233340&r2=233341&view=diff
==============================================================================
--- jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java
(original)
+++ jakarta/httpclient/trunk/coyote-httpconnector/src/tests/tests/peformance/PerformanceTest.java
Thu Aug 18 11:03:07 2005
@@ -1,6 +1,37 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
 package tests.peformance;
 
 import java.io.IOException;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
 import java.util.Random;
 
 import org.apache.http.HttpClientConnection;
@@ -18,6 +49,8 @@
 import org.apache.http.impl.DefaultHttpParams;
 import org.apache.http.impl.HttpGetRequest;
 import org.apache.http.impl.HttpPostRequest;
+import org.apache.http.impl.io.DefaultHttpDataReceiverFactory;
+import org.apache.http.impl.io.DefaultHttpDataTransmitterFactory;
 import org.apache.http.impl.io.NIOSocketFactory;
 import org.apache.http.io.SocketFactory;
 import org.apache.http.params.HttpParams;
@@ -28,13 +61,24 @@
 import org.apache.http.protocol.RequestTargetHost;
 import org.apache.http.protocol.RequestUserAgent;
 
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision$
+ * 
+ * @since 4.0
+ */
 public class PerformanceTest {
 
 	private final HttpRequestExecutor httpexecutor;
+    private final NumberFormat numformat;
 	
 	private PerformanceTest() {
 		super();
 		this.httpexecutor = createExecutor();
+        this.numformat = new DecimalFormat("###,###"); 
 	}
 	
 	private static HttpRequestExecutor createExecutor() {
@@ -65,8 +109,9 @@
 		Random rnd = new Random();
 		rnd.nextBytes(stuff);
 		ByteArrayEntity entity = new ByteArrayEntity(stuff);
+        entity.setChunked(false);
 		r3.setEntity(entity);
-		return new HttpMutableRequest[] { r1, r2, r3};
+		return new HttpMutableRequest[] { r1, r2, r3 };
 	}
 	
 	private void execute(
@@ -75,26 +120,42 @@
 			int repeatCount) throws IOException, HttpException {
 		long total = 0;
         for (int i = 0; i < repeatCount; i++) {
-        	long start = System.nanoTime();
-            HttpResponse response = this.httpexecutor.execute(request, conn);
-            // Consume as string
-            EntityConsumer.toString(response.getEntity());
-        	long t = System.nanoTime() - start;
-        	total += t;
+            try {
+                long start = System.nanoTime();
+                HttpResponse response = this.httpexecutor.execute(request, conn);
+                // Consume as string
+                EntityConsumer.toString(response.getEntity());
+                long t = System.nanoTime() - start;
+                total += t;
+            } catch (IOException ex) {
+                System.out.println("Request failed: " + ex.toString());
+            }
         }
     	System.out.println("Request: " + request.getRequestLine());
-    	System.out.println("Average (nanosec): " + (total / repeatCount));
+    	System.out.println("Average (nanosec): " + this.numformat.format(total / repeatCount));
 	}
 
 	public static void main(String[] args) throws Exception {
-		if (args.length != 2) {
-			System.out.println("Usage tests.performance.PerformanceTest <port> <count>");
+		if (args.length < 2) {
+			System.out.println("Usage tests.performance.PerformanceTest <port> <count>
[NIO|OldIO]");
 			System.exit(1);
 		}
 		PerformanceTest test = new PerformanceTest();
 		
 		int port = Integer.parseInt(args[0]);
 		int count = Integer.parseInt(args[1]);
+        boolean useNIO = true;
+        if (args.length >= 3) {
+            String s = args[2];
+            if (s.equalsIgnoreCase("NIO")) {
+                useNIO = true;
+            } else if (s.equalsIgnoreCase("OldIO")) {
+                useNIO = false;
+            } else {
+                System.out.println("Invalid parameter: " + s);
+                System.exit(1);
+            }
+        }
 		
         SocketFactory socketfactory = NIOSocketFactory.getSocketFactory();
         Protocol http = new Protocol("http", socketfactory, 80);
@@ -104,10 +165,12 @@
         
         Thread.sleep(1000);
         
-        HttpClientConnection conn = new DefaultHttpClientConnection(host);
+        DefaultHttpClientConnection conn = new DefaultHttpClientConnection(host);
+        conn.setReceiverFactory(new DefaultHttpDataReceiverFactory(useNIO));
+        conn.setTransmitterFactory(new DefaultHttpDataTransmitterFactory(useNIO));
         try {
         	for (int i = 0; i < requests.length; i++) {
-            	test.execute(requests[i], conn, count);
+        	    test.execute(requests[i], conn, count);
         	}
         } finally {
         	conn.close();



Mime
View raw message