Improved HTTP client benchmark

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@918642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-03-03 19:19:35 +00:00
parent 2383f4ed9c
commit 635f952f48
8 changed files with 659 additions and 247 deletions

View File

@ -59,6 +59,29 @@
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>7.0.1.v20091125</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.5.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,191 @@
/*
* ====================================================================
*
* 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.client.benchmark;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.URI;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ByteArrayOutputStream2;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class Benchmark {
public static void main(String[] args) throws Exception {
SocketConnector connector = new SocketConnector();
connector.setPort(0);
connector.setRequestBufferSize(12 * 1024);
connector.setResponseBufferSize(12 * 1024);
connector.setAcceptors(2);
QueuedThreadPool threadpool = new QueuedThreadPool();
threadpool.setMinThreads(25);
threadpool.setMaxThreads(200);
Server server = new Server();
server.addConnector(connector);
server.setThreadPool(threadpool);
server.setHandler(new RandomDataHandler());
server.start();
int port = connector.getLocalPort();
int n = 200000;
int contentLen = 2048;
TestHttpAgent[] agents = new TestHttpAgent[] {
new TestHttpClient3(),
new TestHttpJRE(),
new TestHttpCore(),
new TestHttpClient4()
};
byte[] content = new byte[contentLen];
Random rnd = new Random();
rnd.nextBytes(content);
URI target1 = new URI("http", null, "localhost", port, "/rnd", "c=" + contentLen, null);
URI target2 = new URI("http", null, "localhost", port, "/echo", null, null);
try {
for (TestHttpAgent agent: agents) {
System.out.println("=================================");
System.out.println("HTTP agent: " + agent.getClientName());
System.out.println("---------------------------------");
System.out.println(n + " GET requests");
System.out.println("---------------------------------");
long startTime1 = System.currentTimeMillis();
Stats stats1 = agent.get(target1, n);
long finishTime1 = System.currentTimeMillis();
Stats.printStats(target1, startTime1, finishTime1, stats1);
System.out.println("---------------------------------");
System.out.println(n + " POST requests");
System.out.println("---------------------------------");
long startTime2 = System.currentTimeMillis();
Stats stats2 = agent.post(target2, content, n);
long finishTime2 = System.currentTimeMillis();
Stats.printStats(target2, startTime2, finishTime2, stats2);
System.out.println("---------------------------------");
}
} finally {
server.stop();
}
server.join();
}
static class RandomDataHandler extends AbstractHandler {
public RandomDataHandler() {
super();
}
public void handle(
final String target,
final Request baseRequest,
final HttpServletRequest request,
final HttpServletResponse response) throws IOException, ServletException {
if (target.equals("/rnd")) {
rnd(request, response);
} else if (target.equals("/echo")) {
echo(request, response);
} else {
response.setStatus(HttpStatus.NOT_FOUND_404);
Writer writer = response.getWriter();
writer.write("Target not found: " + target);
writer.flush();
}
}
private void rnd(
final HttpServletRequest request,
final HttpServletResponse response) throws IOException, ServletException {
int count = 100;
String s = request.getParameter("c");
try {
count = Integer.parseInt(s);
} catch (NumberFormatException ex) {
response.setStatus(500);
Writer writer = response.getWriter();
writer.write("Invalid query format: " + request.getQueryString());
writer.flush();
return;
}
response.setStatus(200);
response.setContentLength(count);
OutputStream outstream = response.getOutputStream();
byte[] tmp = new byte[1024];
int r = Math.abs(tmp.hashCode());
int remaining = count;
while (remaining > 0) {
int chunk = Math.min(tmp.length, remaining);
for (int i = 0; i < chunk; i++) {
tmp[i] = (byte) ((r + i) % 96 + 32);
}
outstream.write(tmp, 0, chunk);
remaining -= chunk;
}
outstream.flush();
}
private void echo(
final HttpServletRequest request,
final HttpServletResponse response) throws IOException, ServletException {
ByteArrayOutputStream2 buffer = new ByteArrayOutputStream2();
InputStream instream = request.getInputStream();
if (instream != null) {
IO.copy(instream, buffer);
}
byte[] content = buffer.getBuf();
response.setStatus(200);
response.setContentLength(content.length);
OutputStream outstream = response.getOutputStream();
outstream.write(content);
outstream.flush();
}
}
}

View File

@ -0,0 +1,115 @@
/*
* ====================================================================
*
* 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.client.benchmark;
import java.net.URI;
public class Stats {
private int successCount = 0;
private int failureCount = 0;
private String serverName = "unknown";
private long contentLen = 0;
private long totalContentLen = 0;
public Stats() {
super();
}
public int getSuccessCount() {
return successCount;
}
public void setSuccessCount(int successCount) {
this.successCount = successCount;
}
public int getFailureCount() {
return failureCount;
}
public void setFailureCount(int failureCount) {
this.failureCount = failureCount;
}
public String getServerName() {
return serverName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public long getContentLen() {
return contentLen;
}
public void setContentLen(long contentLen) {
this.contentLen = contentLen;
}
public long getTotalContentLen() {
return totalContentLen;
}
public void setTotalContentLen(long totalContentLen) {
this.totalContentLen = totalContentLen;
}
public static void printStats(
final URI targetURI, long startTime, long finishTime, final Stats stats) {
float totalTimeSec = (float) (finishTime - startTime) / 1000;
float reqsPerSec = (float) stats.getSuccessCount() / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) stats.getSuccessCount();
System.out.print("Server Software:\t");
System.out.println(stats.getServerName());
System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(stats.getContentLen());
System.out.println(" bytes");
System.out.println();
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
System.out.print("Complete requests:\t");
System.out.println(stats.getSuccessCount());
System.out.print("Failed requests:\t");
System.out.println(stats.getFailureCount());
System.out.print("Content transferred:\t");
System.out.print(stats.getTotalContentLen());
System.out.println(" bytes");
System.out.print("Requests per second:\t");
System.out.print(reqsPerSec);
System.out.println(" [#/sec] (mean)");
System.out.print("Time per request:\t");
System.out.print(timePerReqMs);
System.out.println(" [ms] (mean)");
}
}

View File

@ -0,0 +1,38 @@
/*
* ====================================================================
*
* 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.client.benchmark;
import java.net.URI;
public interface TestHttpAgent {
String getClientName();
Stats get(URI target, int count) throws Exception;
Stats post(URI target, byte[] content, int n) throws Exception;
}

View File

@ -27,54 +27,51 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TestHttpClient3 {
public class TestHttpClient3 implements TestHttpAgent {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
String targetURI = args[0];
int n = Integer.parseInt(args[1]);
HttpClient httpclient = new HttpClient();
httpclient.getParams().setVersion(
private final HttpClient httpclient;
public TestHttpClient3() {
super();
this.httpclient = new HttpClient();
this.httpclient.getParams().setVersion(
HttpVersion.HTTP_1_1);
httpclient.getParams().setBooleanParameter(
this.httpclient.getParams().setBooleanParameter(
HttpMethodParams.USE_EXPECT_CONTINUE, false);
httpclient.getHttpConnectionManager().getParams()
this.httpclient.getHttpConnectionManager().getParams()
.setStaleCheckingEnabled(false);
GetMethod httpget = new GetMethod(targetURI);
}
byte[] buffer = new byte[4096];
public Stats execute(final HttpMethod httpmethod, int n) throws Exception {
Stats stats = new Stats();
long startTime;
long finishTime;
int successCount = 0;
int failureCount = 0;
String serverName = "unknown";
long total = 0;
long contentLen = 0;
long totalContentLen = 0;
startTime = System.currentTimeMillis();
byte[] buffer = new byte[4096];
for (int i = 0; i < n; i++) {
try {
httpclient.executeMethod(httpget);
InputStream instream = httpget.getResponseBodyAsStream();
this.httpclient.executeMethod(httpmethod);
InputStream instream = httpmethod.getResponseBodyAsStream();
contentLen = 0;
if (instream != null) {
int l = 0;
while ((l = instream.read(buffer)) != -1) {
total += l;
contentLen += l;
}
}
@ -83,45 +80,51 @@ public static void main(String[] args) throws Exception {
} catch (IOException ex) {
failureCount++;
} finally {
httpget.releaseConnection();
httpmethod.releaseConnection();
}
}
finishTime = System.currentTimeMillis();
Header header = httpget.getResponseHeader("Server");
Header header = httpmethod.getResponseHeader("Server");
if (header != null) {
serverName = header.getValue();
stats.setServerName(header.getValue());
}
stats.setSuccessCount(successCount);
stats.setFailureCount(failureCount);
stats.setContentLen(contentLen);
stats.setTotalContentLen(totalContentLen);
return stats;
}
public Stats get(final URI target, int n) throws Exception {
GetMethod httpget = new GetMethod(target.toASCIIString());
return execute(httpget, n);
}
public Stats post(URI target, byte[] content, int n) throws Exception {
PostMethod httppost = new PostMethod(target.toASCIIString());
httppost.setRequestEntity(new ByteArrayRequestEntity(content));
return execute(httppost, n);
}
public String getClientName() {
return "Apache HttpClient 3.1";
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
float totalTimeSec = (float) (finishTime - startTime) / 1000;
float reqsPerSec = (float) successCount / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) successCount;
TestHttpClient3 test = new TestHttpClient3();
System.out.print("Server Software:\t");
System.out.println(serverName);
System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(contentLen);
System.out.println(" bytes");
System.out.println();
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
System.out.print("Complete requests:\t");
System.out.println(successCount);
System.out.print("Failed requests:\t");
System.out.println(failureCount);
System.out.print("Content transferred:\t");
System.out.print(total);
System.out.println(" bytes");
System.out.print("Requests per second:\t");
System.out.print(reqsPerSec);
System.out.println(" [#/sec] (mean)");
System.out.print("Time per request:\t");
System.out.print(timePerReqMs);
System.out.println(" [ms] (mean)");
long startTime = System.currentTimeMillis();
Stats stats = test.get(targetURI, n);
long finishTime = System.currentTimeMillis();
Stats.printStats(targetURI, startTime, finishTime, stats);
}
}

View File

@ -27,28 +27,31 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.util.VersionInfo;
public class TestHttpClient4 {
public class TestHttpClient4 implements TestHttpAgent {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
String targetURI = args[0];
int n = Integer.parseInt(args[1]);
BasicHttpParams params = new BasicHttpParams();
private final HttpClient httpclient;
public TestHttpClient4() {
super();
HttpParams params = new SyncBasicHttpParams();
params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
@ -58,24 +61,21 @@ public static void main(String[] args) throws Exception {
params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
8 * 1024);
DefaultHttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet(targetURI);
this.httpclient = new DefaultHttpClient(params);
}
byte[] buffer = new byte[4096];
long startTime;
long finishTime;
public Stats execute(final HttpUriRequest request, int n) throws Exception {
Stats stats = new Stats();
int successCount = 0;
int failureCount = 0;
String serverName = "unknown";
long total = 0;
long contentLen = 0;
long totalContentLen = 0;
startTime = System.currentTimeMillis();
byte[] buffer = new byte[4096];
for (int i = 0; i < n; i++) {
HttpResponse response = httpclient.execute(httpget);
HttpResponse response = this.httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
@ -84,14 +84,13 @@ public static void main(String[] args) throws Exception {
if (instream != null) {
int l = 0;
while ((l = instream.read(buffer)) != -1) {
total += l;
contentLen += l;
}
}
successCount++;
totalContentLen += contentLen;
} catch (IOException ex) {
httpget.abort();
request.abort();
failureCount++;
} finally {
instream.close();
@ -99,40 +98,49 @@ public static void main(String[] args) throws Exception {
}
Header header = response.getFirstHeader("Server");
if (header != null) {
serverName = header.getValue();
stats.setServerName(header.getValue());
}
}
finishTime = System.currentTimeMillis();
stats.setSuccessCount(successCount);
stats.setFailureCount(failureCount);
stats.setContentLen(contentLen);
stats.setTotalContentLen(totalContentLen);
return stats;
}
public Stats get(final URI target, int n) throws Exception {
HttpGet httpget = new HttpGet(target);
return execute(httpget, n);
}
public Stats post(final URI target, byte[] content, int n) throws Exception {
HttpPost httppost = new HttpPost(target);
httppost.setEntity(new ByteArrayEntity(content));
return execute(httppost, n);
}
public String getClientName() {
VersionInfo vinfo = VersionInfo.loadVersionInfo("org.apache.http.client",
Thread.currentThread().getContextClassLoader());
return "Apache HttpClient 4 (ver: " +
((vinfo != null) ? vinfo.getRelease() : VersionInfo.UNAVAILABLE) + ")";
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
TestHttpClient4 test = new TestHttpClient4();
long startTime = System.currentTimeMillis();
Stats stats = test.get(targetURI, n);
long finishTime = System.currentTimeMillis();
float totalTimeSec = (float) (finishTime - startTime) / 1000;
float reqsPerSec = (float) successCount / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) successCount;
System.out.print("Server Software:\t");
System.out.println(serverName);
System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(contentLen);
System.out.println(" bytes");
System.out.println();
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
System.out.print("Complete requests:\t");
System.out.println(successCount);
System.out.print("Failed requests:\t");
System.out.println(failureCount);
System.out.print("Content transferred:\t");
System.out.print(total);
System.out.println(" bytes");
System.out.print("Requests per second:\t");
System.out.print(reqsPerSec);
System.out.println(" [#/sec] (mean)");
System.out.print("Time per request:\t");
System.out.print(timePerReqMs);
System.out.println(" [ms] (mean)");
Stats.printStats(targetURI, startTime, finishTime, stats);
}
}

View File

@ -30,96 +30,95 @@
import java.net.Socket;
import java.net.URI;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.VersionInfo;
public class TestHttpCore {
public class TestHttpCore implements TestHttpAgent {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
HttpHost targetHost = new HttpHost(
targetURI.getHost(),
targetURI.getPort());
BasicHttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
private final HttpParams params;
private final HttpProcessor httpproc;
private final HttpRequestExecutor httpexecutor;
private final ConnectionReuseStrategy connStrategy;
public TestHttpCore() {
super();
this.params = new SyncBasicHttpParams();
this.params.setParameter(HttpProtocolParams.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
this.params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
false);
params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK,
this.params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK,
false);
params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
this.params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
8 * 1024);
BasicHttpRequest httpget = new BasicHttpRequest("GET", targetURI.getPath());
this.httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
new RequestContent(),
new RequestTargetHost(),
new RequestConnControl(),
new RequestUserAgent()
}, null);
byte[] buffer = new byte[4096];
long startTime;
long finishTime;
this.httpexecutor = new HttpRequestExecutor();
this.connStrategy = new DefaultConnectionReuseStrategy();
}
public Stats execute(
final HttpHost targetHost, final HttpRequest request, int n) throws Exception {
Stats stats = new Stats();
int successCount = 0;
int failureCount = 0;
String serverName = "unknown";
long total = 0;
long contentLen = 0;
long totalContentLen = 0;
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
BasicHttpProcessor httpproc = new BasicHttpProcessor();
// Required protocol interceptors
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
byte[] buffer = new byte[4096];
HttpContext context = new BasicHttpContext();
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
DefaultConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
startTime = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(
targetHost.getHostName(),
targetHost.getPort() > 0 ? targetHost.getPort() : 80);
conn.bind(socket, params);
conn.bind(socket, this.params);
}
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
httpexecutor.preProcess(httpget, httpproc, context);
HttpResponse response = httpexecutor.execute(httpget, conn, context);
httpexecutor.postProcess(response, httpproc, context);
this.httpexecutor.preProcess(request, this.httpproc, context);
HttpResponse response = this.httpexecutor.execute(request, conn, context);
this.httpexecutor.postProcess(response, this.httpproc, context);
HttpEntity entity = response.getEntity();
if (entity != null) {
@ -129,7 +128,6 @@ public static void main(String[] args) throws Exception {
if (instream != null) {
int l = 0;
while ((l = instream.read(buffer)) != -1) {
total += l;
contentLen += l;
}
}
@ -142,45 +140,73 @@ public static void main(String[] args) throws Exception {
instream.close();
}
}
if (!connStrategy.keepAlive(response, context)) {
if (!this.connStrategy.keepAlive(response, context)) {
conn.close();
}
Header header = response.getFirstHeader("Server");
if (header != null) {
serverName = header.getValue();
stats.setServerName(header.getValue());
}
for (HeaderIterator it = request.headerIterator(); it.hasNext();) {
it.next();
it.remove();
}
}
finishTime = System.currentTimeMillis();
stats.setSuccessCount(successCount);
stats.setFailureCount(failureCount);
stats.setContentLen(contentLen);
stats.setTotalContentLen(totalContentLen);
return stats;
}
public Stats get(final URI target, int n) throws Exception {
HttpHost targetHost = new HttpHost(target.getHost(), target.getPort());
StringBuilder buffer = new StringBuilder();
buffer.append(target.getPath());
if (target.getQuery() != null) {
buffer.append("?");
buffer.append(target.getQuery());
}
BasicHttpRequest httpget = new BasicHttpRequest("GET", buffer.toString());
return execute(targetHost, httpget, n);
}
public Stats post(final URI target, byte[] content, int n) throws Exception {
HttpHost targetHost = new HttpHost(target.getHost(), target.getPort());
StringBuilder buffer = new StringBuilder();
buffer.append(target.getPath());
if (target.getQuery() != null) {
buffer.append("?");
buffer.append(target.getQuery());
}
BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST",
buffer.toString());
httppost.setEntity(new ByteArrayEntity(content));
return execute(targetHost, httppost, n);
}
public String getClientName() {
VersionInfo vinfo = VersionInfo.loadVersionInfo("org.apache.http",
Thread.currentThread().getContextClassLoader());
return "Apache HttpCore 4 (ver: " +
((vinfo != null) ? vinfo.getRelease() : VersionInfo.UNAVAILABLE) + ")";
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
TestHttpCore test = new TestHttpCore();
long startTime = System.currentTimeMillis();
Stats stats = test.get(targetURI, n);
long finishTime = System.currentTimeMillis();
float totalTimeSec = (float) (finishTime - startTime) / 1000;
float reqsPerSec = (float) successCount / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) successCount;
System.out.print("Server Software:\t");
System.out.println(serverName);
System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(contentLen);
System.out.println(" bytes");
System.out.println();
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
System.out.print("Complete requests:\t");
System.out.println(successCount);
System.out.print("Failed requests:\t");
System.out.println(failureCount);
System.out.print("Content transferred:\t");
System.out.print(total);
System.out.println(" bytes");
System.out.print("Requests per second:\t");
System.out.print(reqsPerSec);
System.out.println(" [#/sec] (mean)");
System.out.print("Time per request:\t");
System.out.print(timePerReqMs);
System.out.println(" [ms] (mean)");
Stats.printStats(targetURI, startTime, finishTime, stats);
}
}

View File

@ -27,44 +27,49 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
public class TestHttpJRE {
public class TestHttpJRE implements TestHttpAgent {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
URL url = targetURI.toURL();
byte[] buffer = new byte[4096];
long startTime;
long finishTime;
public TestHttpJRE() {
super();
}
public Stats execute(final URI targetURI, byte[] content, int n) throws Exception {
Stats stats = new Stats();
int successCount = 0;
int failureCount = 0;
String serverName = "unknown";
long total = 0;
long contentLen = 0;
long totalContentLen = 0;
startTime = System.currentTimeMillis();
byte[] buffer = new byte[4096];
URL url = targetURI.toURL();
for (int i = 0; i < n; i++) {
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.connect();
if (content != null) {
c.setRequestMethod("POST");
c.setFixedLengthStreamingMode(content.length);
c.setUseCaches (false);
c.setDoInput(true);
c.setDoOutput(true);
OutputStream out = c.getOutputStream();
out.write(content);
out.flush ();
out.close();
}
InputStream instream = c.getInputStream();
try {
contentLen = 0;
if (instream != null) {
int l = 0;
while ((l = instream.read(buffer)) != -1) {
total += l;
contentLen += l;
}
}
@ -79,40 +84,43 @@ public static void main(String[] args) throws Exception {
}
String s = c.getHeaderField("Server");
if (s != null) {
serverName = s;
stats.setServerName(s);
}
}
finishTime = System.currentTimeMillis();
float totalTimeSec = (float) (finishTime - startTime) / 1000;
float reqsPerSec = (float) successCount / totalTimeSec;
float timePerReqMs = (float) (finishTime - startTime) / (float) successCount;
System.out.print("Server Software:\t");
System.out.println(serverName);
System.out.println();
System.out.print("Document URI:\t\t");
System.out.println(targetURI);
System.out.print("Document Length:\t");
System.out.print(contentLen);
System.out.println(" bytes");
System.out.println();
System.out.print("Time taken for tests:\t");
System.out.print(totalTimeSec);
System.out.println(" seconds");
System.out.print("Complete requests:\t");
System.out.println(successCount);
System.out.print("Failed requests:\t");
System.out.println(failureCount);
System.out.print("Content transferred:\t");
System.out.print(total);
System.out.println(" bytes");
System.out.print("Requests per second:\t");
System.out.print(reqsPerSec);
System.out.println(" [#/sec] (mean)");
System.out.print("Time per request:\t");
System.out.print(timePerReqMs);
System.out.println(" [ms] (mean)");
stats.setSuccessCount(successCount);
stats.setFailureCount(failureCount);
stats.setContentLen(contentLen);
stats.setTotalContentLen(totalContentLen);
return stats;
}
public Stats get(final URI target, int n) throws Exception {
return execute(target, null, n);
}
public Stats post(final URI target, byte[] content, int n) throws Exception {
return execute(target, content, n);
}
public String getClientName() {
return "JRE HTTP " + System.getProperty("java.version");
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Usage: <target URI> <no of requests>");
System.exit(-1);
}
URI targetURI = new URI(args[0]);
int n = Integer.parseInt(args[1]);
TestHttpJRE test = new TestHttpJRE();
long startTime = System.currentTimeMillis();
Stats stats = test.get(targetURI, n);
long finishTime = System.currentTimeMillis();
Stats.printStats(targetURI, startTime, finishTime, stats);
}
}