You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by as...@apache.org on 2010/02/14 07:19:06 UTC
svn commit: r909977 - in
/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http:
benchmark/BenchmarkWorker.java benchmark/CommandLineUtils.java
benchmark/Config.java benchmark/HttpBenchmark.java
benchmark/ResultProcessor.java contrib/
Author: asankha
Date: Sun Feb 14 06:19:05 2010
New Revision: 909977
URL: http://svn.apache.org/viewvc?rev=909977&view=rev
Log:
Enhance the Apache AB clone to support any
- HTTP method
- request Gziped responses,
- select chunked or content length encoding
- send Expect-Continue
Additionally the AB clone can now be embedded into other applications - such as to a GUI - which allows the easy specification of a SOAPAction, identity and trust stores - or the complete disabling of SSL server verification for testing
Removed:
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/contrib/
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/CommandLineUtils.java
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.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
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=909977&r1=909976&r2=909977&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 Sun Feb 14 06:19:05 2010
@@ -26,12 +26,14 @@
*/
package org.apache.http.benchmark;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
+import java.security.KeyStore;
import javax.net.SocketFactory;
-import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.*;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
@@ -79,7 +81,13 @@
private final HttpHost targetHost;
private final int count;
private final boolean keepalive;
+ private final boolean disableSSLVerification;
private final Stats stats = new Stats();
+ private final TrustManager[] trustAllCerts;
+ private final String trustStorePath;
+ private final String trustStorePassword;
+ private final String identityStorePath;
+ private final String identityStorePassword;
public BenchmarkWorker(
final HttpParams params,
@@ -87,7 +95,12 @@
final HttpRequest request,
final HttpHost targetHost,
int count,
- boolean keepalive) {
+ boolean keepalive,
+ boolean disableSSLVerification,
+ String trustStorePath,
+ String trustStorePassword,
+ String identityStorePath,
+ String identityStorePassword) {
super();
this.params = params;
@@ -110,6 +123,29 @@
this.connstrategy = new DefaultConnectionReuseStrategy();
this.verbosity = verbosity;
+ this.disableSSLVerification = disableSSLVerification;
+ this.trustStorePath = trustStorePath;
+ this.trustStorePassword = trustStorePassword;
+ this.identityStorePath = identityStorePath;
+ this.identityStorePassword = identityStorePassword;
+
+ // Create a trust manager that does not validate certificate chains
+ trustAllCerts = new TrustManager[]{
+ new X509TrustManager() {
+
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+
+ public void checkClientTrusted(
+ java.security.cert.X509Certificate[] certs, String authType) {
+ }
+
+ public void checkServerTrusted(
+ java.security.cert.X509Certificate[] certs, String authType) {
+ }
+ }
+ };
}
public void run() {
@@ -137,8 +173,38 @@
if (!conn.isOpen()) {
Socket socket = null;
if ("https".equals(targetHost.getSchemeName())) {
- SocketFactory socketFactory = SSLSocketFactory.getDefault();
- socket = socketFactory.createSocket(hostname, port);
+ if (disableSSLVerification) {
+ SSLContext sc = SSLContext.getInstance("SSL");
+ if (identityStorePath != null) {
+ KeyStore identityStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ FileInputStream instream = new FileInputStream(identityStorePath);
+ try {
+ identityStore.load(instream, identityStorePassword.toCharArray());
+ } finally {
+ if (instream != null) {
+ try { instream.close(); } catch (IOException ignore) {}
+ }
+ }
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(
+ KeyManagerFactory.getDefaultAlgorithm());
+ kmf.init(identityStore, identityStorePassword.toCharArray());
+ sc.init(kmf.getKeyManagers(), trustAllCerts, null);
+ } else {
+ sc.init(null, trustAllCerts, null);
+ }
+
+ HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+ socket = sc.getSocketFactory().createSocket(hostname, port);
+ } else {
+ if (trustStorePath != null) {
+ System.setProperty("javax.net.ssl.trustStore", trustStorePath);
+ }
+ if (trustStorePassword != null) {
+ System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
+ }
+ SocketFactory socketFactory = SSLSocketFactory.getDefault();
+ socket = socketFactory.createSocket(hostname, port);
+ }
} else {
socket = new Socket(hostname, port);
}
@@ -171,23 +237,26 @@
}
HttpEntity entity = response.getEntity();
- String charset = EntityUtils.getContentCharSet(entity);
- if (charset == null) {
- charset = HTTP.DEFAULT_CONTENT_CHARSET;
- }
- long contentlen = 0;
if (entity != null) {
- 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);
- System.out.print(s);
+ String charset = EntityUtils.getContentCharSet(entity);
+ if (charset == null) {
+ charset = HTTP.DEFAULT_CONTENT_CHARSET;
+ }
+ long contentlen = 0;
+ if (entity != null) {
+ 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);
+ System.out.print(s);
+ }
}
+ instream.close();
}
- instream.close();
+ stats.setContentLength(contentlen);
}
if (this.verbosity >= 4) {
@@ -200,7 +269,6 @@
} else {
stats.incKeepAliveCount();
}
- stats.setContentLength(contentlen);
} catch (IOException ex) {
ex.printStackTrace();
@@ -208,6 +276,12 @@
if (this.verbosity >= 2) {
System.err.println("I/O error: " + ex.getMessage());
}
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ stats.incFailureCount();
+ if (this.verbosity >= 2) {
+ System.err.println("Generic error: " + ex.getMessage());
+ }
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CommandLineUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CommandLineUtils.java?rev=909977&r1=909976&r2=909977&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CommandLineUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/CommandLineUtils.java Sun Feb 14 06:19:05 2010
@@ -36,12 +36,12 @@
import java.net.URL;
public class CommandLineUtils {
-
+
public static Options getOptions() {
- Option iopt = new Option("i", false, "Do HEAD requests instead of GET.");
+ Option iopt = new Option("i", false, "Do HEAD requests instead of GET (deprecated)");
iopt.setRequired(false);
- Option oopt = new Option("o", false, "Use HTTP/S 1.0");
+ Option oopt = new Option("o", false, "Use HTTP/S 1.0 instead of 1.1 (default)");
oopt.setRequired(false);
Option kopt = new Option("k", false, "Enable the HTTP KeepAlive feature, " +
@@ -49,23 +49,37 @@
"Default is no KeepAlive");
kopt.setRequired(false);
+ Option uopt = new Option("u", false, "Chunk entity. Default is false");
+ uopt.setRequired(false);
+
+ Option xopt = new Option("x", false, "Use Expect-Continue. Default is false");
+ xopt.setRequired(false);
+
+ Option gopt = new Option("g", false, "Accept GZip. Default is false");
+ gopt.setRequired(false);
+
Option nopt = new Option("n", true, "Number of requests to perform for the " +
"benchmarking session. The default is to just perform a single " +
"request which usually leads to non-representative benchmarking " +
- "results.");
+ "results");
nopt.setRequired(false);
nopt.setArgName("requests");
Option copt = new Option("c", true, "Concurrency while performing the " +
- "benchmarking session. The default is to just use a single thread/client.");
+ "benchmarking session. The default is to just use a single thread/client");
copt.setRequired(false);
copt.setArgName("concurrency");
- Option popt = new Option("p", true, "File containing data to POST.");
+ Option popt = new Option("p", true, "File containing data to POST or PUT");
popt.setRequired(false);
- popt.setArgName("POST-postFile");
+ popt.setArgName("Payload file");
- Option Topt = new Option("T", true, "Content-type header to use for POST data.");
+ Option mopt = new Option("m", true, "HTTP Method. Default is POST. " +
+ "Possible options are GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE");
+ mopt.setRequired(false);
+ mopt.setArgName("HTTP method");
+
+ Option Topt = new Option("T", true, "Content-type header to use for POST/PUT data");
Topt.setRequired(false);
Topt.setArgName("content-type");
@@ -82,15 +96,19 @@
Option vopt = new Option("v", true, "Set verbosity level - 4 and above " +
"prints response content, 3 and above prints " +
"information on headers, 2 and above prints response codes (404, 200, " +
- "etc.), 1 and above prints warnings and info.");
+ "etc.), 1 and above prints warnings and info");
vopt.setRequired(false);
vopt.setArgName("verbosity");
- Option hopt = new Option("h", false, "Display usage information.");
+ Option hopt = new Option("h", false, "Display usage information");
nopt.setRequired(false);
Options options = new Options();
options.addOption(iopt);
+ options.addOption(mopt);
+ options.addOption(uopt);
+ options.addOption(xopt);
+ options.addOption(gopt);
options.addOption(kopt);
options.addOption(nopt);
options.addOption(copt);
@@ -141,7 +159,7 @@
if (!file.exists()) {
printError("File not found: " + file);
}
- config.setPostFile(file);
+ config.setPayloadFile(file);
}
if (cmd.hasOption('T')) {
@@ -169,9 +187,27 @@
if (cmd.hasOption('o')) {
config.setUseHttp1_0(true);
}
-
+
+ if (cmd.hasOption('m')) {
+ config.setMethod(cmd.getOptionValue('m'));
+ } else if (cmd.hasOption('p')) {
+ config.setMethod("POST");
+ }
+
+ if (cmd.hasOption('u')) {
+ config.setUseChunking(true);
+ }
+
+ if (cmd.hasOption('x')) {
+ config.setUseExpectContinue(true);
+ }
+
+ if (cmd.hasOption('g')) {
+ config.setUseAcceptGZip(true);
+ }
+
String[] cmdargs = cmd.getArgs();
- if (cmdargs.length > 1) {
+ if (cmdargs.length > 0) {
try {
config.setUrl(new URL(cmdargs[0]));
} catch (MalformedURLException e) {
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java?rev=909977&r1=909976&r2=909977&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java Sun Feb 14 06:19:05 2010
@@ -38,11 +38,23 @@
private int verbosity;
private boolean headInsteadOfGet;
private boolean useHttp1_0;
- private File postFile;
private String contentType;
private String[] headers;
private int socketTimeout;
-
+ private String method = "GET";
+ private boolean useChunking;
+ private boolean useExpectContinue;
+ private boolean useAcceptGZip;
+ private File payloadFile = null;
+ private String payloadText = null;
+ private String soapAction = null;
+
+ private boolean disableSSLVerification = true;
+ private String trustStorePath = null;
+ private String identityStorePath = null;
+ private String trustStorePassword = null;
+ private String identityStorePassword = null;
+
public Config() {
super();
this.url = null;
@@ -52,7 +64,8 @@
this.verbosity = 0;
this.headInsteadOfGet = false;
this.useHttp1_0 = false;
- this.postFile = null;
+ this.payloadFile = null;
+ this.payloadText = null;
this.contentType = null;
this.headers = null;
this.socketTimeout = 60000;
@@ -104,6 +117,7 @@
public void setHeadInsteadOfGet(boolean headInsteadOfGet) {
this.headInsteadOfGet = headInsteadOfGet;
+ this.method = "HEAD";
}
public boolean isUseHttp1_0() {
@@ -114,12 +128,12 @@
this.useHttp1_0 = useHttp1_0;
}
- public File getPostFile() {
- return postFile;
+ public File getPayloadFile() {
+ return payloadFile;
}
- public void setPostFile(File postFile) {
- this.postFile = postFile;
+ public void setPayloadFile(File payloadFile) {
+ this.payloadFile = payloadFile;
}
public String getContentType() {
@@ -146,4 +160,91 @@
this.socketTimeout = socketTimeout;
}
+ public void setMethod(String method) {
+ this.method = method;
+ }
+
+ public void setUseChunking(boolean useChunking) {
+ this.useChunking = useChunking;
+ }
+
+ public void setUseExpectContinue(boolean useExpectContinue) {
+ this.useExpectContinue = useExpectContinue;
+ }
+
+ public void setUseAcceptGZip(boolean useAcceptGZip) {
+ this.useAcceptGZip = useAcceptGZip;
+ }
+
+ public String getMethod() {
+ return method;
+ }
+
+ public boolean isUseChunking() {
+ return useChunking;
+ }
+
+ public boolean isUseExpectContinue() {
+ return useExpectContinue;
+ }
+
+ public boolean isUseAcceptGZip() {
+ return useAcceptGZip;
+ }
+
+ public String getPayloadText() {
+ return payloadText;
+ }
+
+ public String getSoapAction() {
+ return soapAction;
+ }
+
+ public boolean isDisableSSLVerification() {
+ return disableSSLVerification;
+ }
+
+ public String getTrustStorePath() {
+ return trustStorePath;
+ }
+
+ public String getIdentityStorePath() {
+ return identityStorePath;
+ }
+
+ public String getTrustStorePassword() {
+ return trustStorePassword;
+ }
+
+ public String getIdentityStorePassword() {
+ return identityStorePassword;
+ }
+
+ public void setPayloadText(String payloadText) {
+ this.payloadText = payloadText;
+ }
+
+ public void setSoapAction(String soapAction) {
+ this.soapAction = soapAction;
+ }
+
+ public void setDisableSSLVerification(boolean disableSSLVerification) {
+ this.disableSSLVerification = disableSSLVerification;
+ }
+
+ public void setTrustStorePath(String trustStorePath) {
+ this.trustStorePath = trustStorePath;
+ }
+
+ public void setIdentityStorePath(String identityStorePath) {
+ this.identityStorePath = identityStorePath;
+ }
+
+ public void setTrustStorePassword(String trustStorePassword) {
+ this.trustStorePassword = trustStorePassword;
+ }
+
+ public void setIdentityStorePassword(String identityStorePassword) {
+ this.identityStorePassword = identityStorePassword;
+ }
}
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=909977&r1=909976&r2=909977&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 Sun Feb 14 06:19:05 2010
@@ -26,6 +26,7 @@
*/
package org.apache.http.benchmark;
+import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.apache.commons.cli.CommandLine;
@@ -49,6 +50,8 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
+import org.apache.http.HttpEntity;
+import org.apache.http.entity.StringEntity;
/**
* Main program of the HTTP benchmark.
@@ -59,7 +62,7 @@
public class HttpBenchmark {
private final Config config;
-
+
private HttpParams params = null;
private HttpRequest[] request = null;
private HttpHost host = null;
@@ -78,61 +81,62 @@
Config config = new Config();
CommandLineUtils.parseCommandLine(cmd, config);
-
+
if (config.getUrl() == null) {
CommandLineUtils.showUsage(options);
System.exit(1);
}
-
+
HttpBenchmark httpBenchmark = new HttpBenchmark(config);
httpBenchmark.execute();
}
-
+
public HttpBenchmark(final Config config) {
super();
this.config = config != null ? config : new Config();
}
-
- private void prepare() {
+
+ private void prepare() throws UnsupportedEncodingException {
// prepare http params
- params = getHttpParams(config.getSocketTimeout(), config.isUseHttp1_0());
+ params = getHttpParams(config.getSocketTimeout(), config.isUseHttp1_0(), config.isUseExpectContinue());
URL url = config.getUrl();
host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
- StringBuilder buffer = new StringBuilder();
- buffer.append(url.getPath());
- if (url.getQuery() != null) {
- buffer.append('?');
- buffer.append(url.getQuery());
- }
- String requestURI = buffer.toString();
-
+ HttpEntity entity = null;
+
// Prepare requests for each thread
+ if (config.getPayloadFile() != null) {
+ entity = new FileEntity(config.getPayloadFile(), config.getContentType());
+ ((FileEntity) entity).setChunked(config.isUseChunking());
+ contentLength = config.getPayloadFile().length();
+
+ } else if (config.getPayloadText() != null) {
+ entity = new StringEntity(config.getPayloadText(), config.getContentType(), "UTF-8");
+ ((StringEntity) entity).setChunked(config.isUseChunking());
+ contentLength = config.getPayloadText().getBytes().length;
+ }
request = new HttpRequest[config.getThreads()];
- if (config.getPostFile() != null) {
- FileEntity entity = new FileEntity(config.getPostFile(), config.getContentType());
- contentLength = entity.getContentLength();
- if (config.getPostFile().length() > 100000) {
- entity.setChunked(true);
- }
-
- for (int i = 0; i < request.length; i++) {
- BasicHttpEntityEnclosingRequest httppost =
- new BasicHttpEntityEnclosingRequest("POST", requestURI);
+ for (int i = 0; i < request.length; i++) {
+ if ("POST".equals(config.getMethod())) {
+ BasicHttpEntityEnclosingRequest httppost =
+ new BasicHttpEntityEnclosingRequest("POST", url.getPath());
httppost.setEntity(entity);
request[i] = httppost;
- }
-
- } else if (config.isHeadInsteadOfGet()) {
- for (int i = 0; i < request.length; i++) {
- request[i] = new BasicHttpRequest("HEAD", requestURI);
- }
-
- } else {
- for (int i = 0; i < request.length; i++) {
- request[i] = new BasicHttpRequest("GET", requestURI);
+ } else if ("PUT".equals(config.getMethod())) {
+ BasicHttpEntityEnclosingRequest httpput =
+ new BasicHttpEntityEnclosingRequest("PUT", url.getPath());
+ httpput.setEntity(entity);
+ request[i] = httpput;
+ } else {
+ String path = url.getPath();
+ if (url.getQuery() != null && url.getQuery().length() > 0) {
+ path += "?" + url.getQuery();
+ } else if (path.trim().length() == 0) {
+ path = "/";
+ }
+ request[i] = new BasicHttpRequest(config.getMethod(), path);
}
}
@@ -155,9 +159,21 @@
}
}
}
+
+ if (config.isUseAcceptGZip()) {
+ for (int i = 0; i < request.length; i++) {
+ request[i].addHeader(new DefaultHeader("Accept-Encoding", "gzip"));
+ }
+ }
+
+ if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
+ for (int i = 0; i < request.length; i++) {
+ request[i].addHeader(new DefaultHeader("SOAPAction", config.getSoapAction()));
+ }
+ }
}
- public void execute() {
+ public String execute() throws Exception {
prepare();
@@ -165,7 +181,7 @@
config.getThreads(), config.getThreads(), 5, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
new ThreadFactory() {
-
+
public Thread newThread(Runnable r) {
return new Thread(r, "ClientPool");
}
@@ -177,11 +193,16 @@
for (int i = 0; i < workers.length; i++) {
workers[i] = new BenchmarkWorker(
params,
- config.getVerbosity(),
+ config.getVerbosity(),
request[i],
host,
- config.getRequests(),
- config.isKeepAlive());
+ config.getRequests(),
+ config.isKeepAlive(),
+ config.isDisableSSLVerification(),
+ config.getTrustStorePath(),
+ config.getTrustStorePassword(),
+ config.getIdentityStorePath(),
+ config.getIdentityStorePassword());
workerPool.execute(workers[i]);
}
@@ -194,18 +215,19 @@
}
workerPool.shutdown();
- ResultProcessor.printResults(workers, host, config.getUrl().toString(), contentLength);
+ return ResultProcessor.printResults(workers, host, config.getUrl().toString(), contentLength);
}
- private HttpParams getHttpParams(int socketTimeout, boolean useHttp1_0) {
+ private HttpParams getHttpParams(
+ int socketTimeout, boolean useHttp1_0, boolean useExpectContinue) {
+
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
useHttp1_0 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1)
.setParameter(HttpProtocolParams.USER_AGENT, "HttpCore-AB/1.1")
- .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false)
+ .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, useExpectContinue)
.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
.setIntParameter(HttpConnectionParams.SO_TIMEOUT, socketTimeout);
return params;
}
-
}
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=909977&r1=909976&r2=909977&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 Sun Feb 14 06:19:05 2010
@@ -45,7 +45,7 @@
nf6.setMinimumFractionDigits(6);
}
- static void printResults(BenchmarkWorker[] workers, HttpHost host,
+ static String printResults(BenchmarkWorker[] workers, HttpHost host,
String uri, long contentLength) {
double totalTimeNano = 0;
@@ -75,28 +75,37 @@
long totalBytesSent = contentLength * successCount;
long totalBytes = totalBytesRcvd + (totalBytesSent > 0 ? totalBytesSent : 0);
- System.out.println("\nServer Software:\t\t" + stats.getServerName());
- System.out.println("Server Hostname:\t\t" + host.getHostName());
- System.out.println("Server Port:\t\t\t" +
+ StringBuilder sb = new StringBuilder(1024);
+
+ printAndAppend(sb,"\nServer Software:\t\t" + stats.getServerName());
+ printAndAppend(sb, "Server Hostname:\t\t" + host.getHostName());
+ printAndAppend(sb, "Server Port:\t\t\t" +
(host.getPort() > 0 ? host.getPort() : uri.startsWith("https") ? "443" : "80") + "\n");
- System.out.println("Document Path:\t\t\t" + uri);
- System.out.println("Document Length:\t\t" + stats.getContentLength() + " bytes\n");
- System.out.println("Concurrency Level:\t\t" + workers.length);
- System.out.println("Time taken for tests:\t\t" + nf6.format(totalTimeSec) + " seconds");
- System.out.println("Complete requests:\t\t" + successCount);
- System.out.println("Failed requests:\t\t" + failureCount);
- System.out.println("Write errors:\t\t\t" + writeErrors);
- System.out.println("Kept alive:\t\t\t" + keepAliveCount);
- System.out.println("Total transferred:\t\t" + totalBytes + " bytes");
- System.out.println("Requests per second:\t\t" + nf2.format(reqsPerSec) + " [#/sec] (mean)");
- System.out.println("Time per request:\t\t" + nf3.format(timePerReqMs * workers.length) + " [ms] (mean)");
- System.out.println("Time per request:\t\t" + nf3.format(timePerReqMs) +
+ printAndAppend(sb, "Document Path:\t\t\t" + uri);
+ printAndAppend(sb, "Document Length:\t\t" + stats.getContentLength() + " bytes\n");
+ printAndAppend(sb, "Concurrency Level:\t\t" + workers.length);
+ printAndAppend(sb, "Time taken for tests:\t\t" + nf6.format(totalTimeSec) + " seconds");
+ printAndAppend(sb, "Complete requests:\t\t" + successCount);
+ printAndAppend(sb, "Failed requests:\t\t" + failureCount);
+ printAndAppend(sb, "Write errors:\t\t\t" + writeErrors);
+ printAndAppend(sb, "Kept alive:\t\t\t" + keepAliveCount);
+ printAndAppend(sb, "Total transferred:\t\t" + totalBytes + " bytes");
+ printAndAppend(sb, "Requests per second:\t\t" + nf2.format(reqsPerSec) + " [#/sec] (mean)");
+ printAndAppend(sb, "Time per request:\t\t" + nf3.format(timePerReqMs * workers.length) + " [ms] (mean)");
+ printAndAppend(sb, "Time per request:\t\t" + nf3.format(timePerReqMs) +
" [ms] (mean, across all concurrent requests)");
- System.out.println("Transfer rate:\t\t\t" +
+ printAndAppend(sb, "Transfer rate:\t\t\t" +
nf2.format(totalBytesRcvd/1000/totalTimeSec) + " [Kbytes/sec] received");
- System.out.println("\t\t\t\t" +
+ printAndAppend(sb, "\t\t\t\t" +
(totalBytesSent > 0 ? nf2.format(totalBytesSent/1000/totalTimeSec) : -1) + " kb/s sent");
- System.out.println("\t\t\t\t" +
+ printAndAppend(sb, "\t\t\t\t" +
nf2.format(totalBytes/1000/totalTimeSec) + " kb/s total");
+
+ return sb.toString();
+ }
+
+ private static void printAndAppend(StringBuilder sb, String s) {
+ System.out.println(s);
+ sb.append(s).append("\r\n");
}
}