001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.benchmark;
028
029import java.text.NumberFormat;
030
031import org.apache.http.HttpHost;
032
033public class ResultProcessor {
034
035    static NumberFormat nf2 = NumberFormat.getInstance();
036    static NumberFormat nf3 = NumberFormat.getInstance();
037    static NumberFormat nf6 = NumberFormat.getInstance();
038
039    static {
040        nf2.setMaximumFractionDigits(2);
041        nf2.setMinimumFractionDigits(2);
042        nf3.setMaximumFractionDigits(3);
043        nf3.setMinimumFractionDigits(3);
044        nf6.setMaximumFractionDigits(6);
045        nf6.setMinimumFractionDigits(6);
046    }
047
048    static Results collectResults(final BenchmarkWorker[] workers, final HttpHost host, final String uri) {
049        long totalTimeNano = 0;
050        long successCount    = 0;
051        long failureCount    = 0;
052        long writeErrors     = 0;
053        long keepAliveCount  = 0;
054        long totalBytesRcvd  = 0;
055        long totalBytesSent  = 0;
056
057        final Stats stats = workers[0].getStats();
058
059        for (final BenchmarkWorker worker : workers) {
060            final Stats s = worker.getStats();
061            totalTimeNano  += s.getDuration();
062            successCount   += s.getSuccessCount();
063            failureCount   += s.getFailureCount();
064            writeErrors    += s.getWriteErrors();
065            keepAliveCount += s.getKeepAliveCount();
066            totalBytesRcvd += s.getTotalBytesRecv();
067            totalBytesSent += s.getTotalBytesSent();
068        }
069
070        final Results results = new Results();
071        results.serverName = stats.getServerName();
072        results.hostName = host.getHostName();
073        results.hostPort = host.getPort() > 0 ? host.getPort() :
074            host.getSchemeName().equalsIgnoreCase("https") ? 443 : 80;
075        results.documentPath = uri;
076        results.contentLength = stats.getContentLength();
077        results.concurrencyLevel = workers.length;
078        results.totalTimeNano = totalTimeNano;
079        results.successCount = successCount;
080        results.failureCount = failureCount;
081        results.writeErrors = writeErrors;
082        results.keepAliveCount = keepAliveCount;
083        results.totalBytesRcvd = totalBytesRcvd;
084        results.totalBytesSent = totalBytesSent;
085        results.totalBytes = totalBytesRcvd + (totalBytesSent > 0 ? totalBytesSent : 0);
086        return results;
087    }
088
089    static void printResults(final Results results) {
090        final int threads = results.getConcurrencyLevel();
091        final double totalTimeMs  = (results.getTotalTimeNano() / threads) / 1000000; // convert nano secs to milli secs
092        final double timePerReqMs = totalTimeMs / results.getSuccessCount();
093        final double totalTimeSec = totalTimeMs / 1000;
094        final double reqsPerSec   = results.getSuccessCount() / totalTimeSec;
095
096        System.out.println("\nServer Software:\t\t" + results.getServerName());
097        System.out.println( "Server Hostname:\t\t" + results.getHostName());
098        System.out.println( "Server Port:\t\t\t" + Integer.valueOf(results.getHostPort()));
099        System.out.println( "Document Path:\t\t\t" + results.getDocumentPath());
100        System.out.println( "Document Length:\t\t" + results.getContentLength() + " bytes\n");
101        System.out.println( "Concurrency Level:\t\t" + results.getConcurrencyLevel());
102        System.out.println( "Time taken for tests:\t\t" + nf6.format(totalTimeSec) + " seconds");
103        System.out.println( "Complete requests:\t\t" + results.getSuccessCount());
104        System.out.println( "Failed requests:\t\t" + results.getFailureCount());
105        System.out.println( "Write errors:\t\t\t" + results.getWriteErrors());
106        System.out.println( "Kept alive:\t\t\t" + results.getKeepAliveCount());
107        System.out.println( "Total transferred:\t\t" + results.getTotalBytes() + " bytes");
108        System.out.println( "Requests per second:\t\t" + nf2.format(reqsPerSec) + " [#/sec] (mean)");
109        System.out.println( "Time per request:\t\t" + nf3.format(timePerReqMs
110                * results.getConcurrencyLevel()) + " [ms] (mean)");
111        System.out.println( "Time per request:\t\t" + nf3.format(timePerReqMs) +
112            " [ms] (mean, across all concurrent requests)");
113        System.out.println( "Transfer rate:\t\t\t" +
114            nf2.format(results.getTotalBytesRcvd() / 1000 / totalTimeSec) + " [Kbytes/sec] received");
115        System.out.println( "\t\t\t\t" +
116            (results.getTotalBytesSent() > 0 ? nf2.format(results.getTotalBytesSent()
117                    / 1000 / totalTimeSec) : Integer.valueOf(-1)) + " kb/s sent");
118        System.out.println( "\t\t\t\t" +
119            nf2.format(results.getTotalBytes() / 1000 / totalTimeSec) + " kb/s total");
120    }
121
122}