You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/02/10 21:02:22 UTC

svn commit: r1242905 - /httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/

Author: olegk
Date: Fri Feb 10 20:02:22 2012
New Revision: 1242905

URL: http://svn.apache.org/viewvc?rev=1242905&view=rev
Log:
More reliable counting of received and sent content bytes

Added:
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java   (with props)
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java   (with props)
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java
    httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Stats.java

Added: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java?rev=1242905&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java Fri Feb 10 20:02:22 2012
@@ -0,0 +1,84 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.http.benchmark;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpMessage;
+import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.impl.DefaultHttpClientConnection;
+import org.apache.http.impl.entity.EntityDeserializer;
+import org.apache.http.impl.entity.EntitySerializer;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.io.SessionInputBuffer;
+import org.apache.http.io.SessionOutputBuffer;
+
+class BenchmarkConnection extends DefaultHttpClientConnection {
+
+    private final Stats stats;
+    
+    BenchmarkConnection(final Stats stats) {
+        super();
+        this.stats = stats;
+    }
+
+    @Override
+    protected EntityDeserializer createEntityDeserializer() {
+        return new EntityDeserializer(new LaxContentLengthStrategy()) {
+
+            @Override
+            protected BasicHttpEntity doDeserialize(
+                    final SessionInputBuffer inbuffer, 
+                    final HttpMessage message) throws HttpException, IOException {
+                BasicHttpEntity entity = super.doDeserialize(inbuffer, message);
+                InputStream instream = entity.getContent();
+                entity.setContent(new CountingInputStream(instream, stats));
+                return entity;
+            }
+            
+        };
+    }
+
+    @Override
+    protected EntitySerializer createEntitySerializer() {
+        return new EntitySerializer(new StrictContentLengthStrategy()) {
+
+            @Override
+            protected OutputStream doSerialize(
+                    final SessionOutputBuffer outbuffer, 
+                    final HttpMessage message) throws HttpException, IOException {
+                return new CountingOutputStream(super.doSerialize(outbuffer, message), stats);
+            }
+            
+        };
+    }
+    
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java?rev=1242905&r1=1242904&r2=1242905&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java Fri Feb 10 20:02:22 2012
@@ -50,7 +50,6 @@ import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.entity.ContentType;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpClientConnection;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
@@ -156,7 +155,7 @@ public class BenchmarkWorker implements 
     public void run() {
 
         HttpResponse response = null;
-        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+        BenchmarkConnection conn = new BenchmarkConnection(this.stats);
 
         String scheme = targetHost.getSchemeName();
         String hostname = targetHost.getHostName();
@@ -261,7 +260,6 @@ public class BenchmarkWorker implements 
                     InputStream instream = entity.getContent();
                     int l = 0;
                     while ((l = instream.read(this.buffer)) != -1) {
-                        stats.incTotalBytesRecv(l);
                         contentlen += l;
                         if (this.verbosity >= 4) {
                             String s = new String(this.buffer, 0, l, charset);

Added: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java?rev=1242905&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java Fri Feb 10 20:02:22 2012
@@ -0,0 +1,78 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.http.benchmark;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+class CountingInputStream extends FilterInputStream {
+
+    private final Stats stats;
+    
+    CountingInputStream(final InputStream instream, final Stats stats) {
+        super(instream);
+        this.stats = stats;
+    }
+
+    @Override
+    public int read() throws IOException {
+        int b = this.in.read();
+        if (b != -1) {
+            this.stats.incTotalBytesRecv(1);
+        }
+        return b;
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        int bytesRead = this.in.read(b);
+        if (bytesRead > 0) {
+            this.stats.incTotalBytesRecv(bytesRead);
+        }
+        return bytesRead;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        int bytesRead = this.in.read(b, off, len);
+        if (bytesRead > 0) {
+            this.stats.incTotalBytesRecv(bytesRead);
+        }
+        return bytesRead;
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        long bytesRead = this.in.skip(n);
+        if (bytesRead > 0) {
+            this.stats.incTotalBytesRecv(bytesRead);
+        }
+        return bytesRead;
+    }
+    
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingInputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java?rev=1242905&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java Fri Feb 10 20:02:22 2012
@@ -0,0 +1,60 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.http.benchmark;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+class CountingOutputStream extends FilterOutputStream {
+
+    private final Stats stats;
+    
+    CountingOutputStream(final OutputStream outstream, final Stats stats) {
+        super(outstream);
+        this.stats = stats;
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        this.out.write(b);
+        this.stats.incTotalBytesSent(1);
+    }
+
+    @Override
+    public void write(byte[] b) throws IOException {
+        this.out.write(b);
+        this.stats.incTotalBytesSent(b.length);
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        this.out.write(b, off, len);
+        this.stats.incTotalBytesSent(len);
+    }
+    
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CountingOutputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java?rev=1242905&r1=1242904&r2=1242905&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/HttpBenchmark.java Fri Feb 10 20:02:22 2012
@@ -67,7 +67,6 @@ public class HttpBenchmark {
     private HttpParams params = null;
     private HttpRequest[] request = null;
     private HttpHost host = null;
-    private long contentLength = -1;
 
     public static void main(String[] args) throws Exception {
 
@@ -112,14 +111,11 @@ public class HttpBenchmark {
             fe.setContentType(config.getContentType());
             fe.setChunked(config.isUseChunking());
             entity = fe;
-            contentLength = config.getPayloadFile().length();
-
         } else if (config.getPayloadText() != null) {
             StringEntity se = new StringEntity(config.getPayloadText(), 
                     ContentType.parse(config.getContentType()));
             se.setChunked(config.isUseChunking());
             entity = se;
-            contentLength = config.getPayloadText().getBytes().length;
         }
         request = new HttpRequest[config.getThreads()];
 
@@ -220,7 +216,7 @@ public class HttpBenchmark {
         }
 
         workerPool.shutdown();
-        return ResultProcessor.printResults(workers, host, config.getUrl().toString(), contentLength);
+        return ResultProcessor.printResults(workers, host, config.getUrl().toString());
     }
 
     private HttpParams getHttpParams(

Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java?rev=1242905&r1=1242904&r2=1242905&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java Fri Feb 10 20:02:22 2012
@@ -45,8 +45,7 @@ public class ResultProcessor {
         nf6.setMinimumFractionDigits(6);
     }
 
-    static String printResults(BenchmarkWorker[] workers, HttpHost host,
-        String uri, long contentLength) {
+    static String printResults(BenchmarkWorker[] workers, HttpHost host, String uri) {
 
         double totalTimeNano = 0;
         long successCount    = 0;
@@ -54,6 +53,7 @@ public class ResultProcessor {
         long writeErrors     = 0;
         long keepAliveCount  = 0;
         long totalBytesRcvd  = 0;
+        long totalBytesSent  = 0;
 
         Stats stats = workers[0].getStats();
 
@@ -65,6 +65,7 @@ public class ResultProcessor {
             writeErrors    += s.getWriteErrors();
             keepAliveCount += s.getKeepAliveCount();
             totalBytesRcvd += s.getTotalBytesRecv();
+            totalBytesSent += s.getTotalBytesSent();
         }
 
         int threads = workers.length;
@@ -72,7 +73,6 @@ public class ResultProcessor {
         double timePerReqMs = totalTimeMs / successCount;
         double totalTimeSec = totalTimeMs / 1000;
         double reqsPerSec   = successCount / totalTimeSec;
-        long totalBytesSent = contentLength * successCount;
         long totalBytes     = totalBytesRcvd + (totalBytesSent > 0 ? totalBytesSent : 0);
 
         StringBuilder sb = new StringBuilder(1024);

Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Stats.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Stats.java?rev=1242905&r1=1242904&r2=1242905&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Stats.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Stats.java Fri Feb 10 20:02:22 2012
@@ -42,6 +42,7 @@ public class Stats {
     private int keepAliveCount = 0;
     private String serverName = null;
     private long totalBytesRecv = 0;
+    private long totalBytesSent = 0;
     private long contentLength = -1;
 
     public Stats() {
@@ -111,10 +112,18 @@ public class Stats {
         return this.totalBytesRecv;
     }
 
-    public void incTotalBytesRecv(int n) {
+    public void incTotalBytesRecv(long n) {
         this.totalBytesRecv += n;
     }
 
+    public long getTotalBytesSent() {
+        return this.totalBytesSent;
+    }
+
+    public void incTotalBytesSent(long n) {
+        this.totalBytesSent += n;
+    }
+
     public long getContentLength() {
         return this.contentLength;
     }