You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2006/03/24 21:36:24 UTC
svn commit: r388628 - in
/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark:
./ BenchmarkWorker.java FileRequestEntity.java HttpBenchmark.java Stats.java
Author: olegk
Date: Fri Mar 24 12:36:22 2006
New Revision: 388628
URL: http://svn.apache.org/viewcvs?rev=388628&view=rev
Log:
A simple HTTP benchmark tool based on HttpClient, which implements a subset of AB (Apache Benchmark) interface
Backported from Jakarta HttpComponents HttpCore
Added:
jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/
- copied from r388545, jakarta/httpcomponents/trunk/http-core/src/contrib/org/apache/http/contrib/benchmark/
jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java (with props)
Modified:
jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java
jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/HttpBenchmark.java
jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/Stats.java
Modified: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java?rev=388628&r1=388545&r2=388628&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java Fri Mar 24 12:36:22 2006
@@ -26,87 +26,75 @@
* <http://www.apache.org/>.
*
*/
-package org.apache.http.contrib.benchmark;
+package org.apache.commons.httpclient.contrib.benchmark;
import java.io.IOException;
import java.io.InputStream;
-import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.Header;
-import org.apache.http.HttpClientConnection;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpException;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.protocol.HttpRequestExecutor;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
/**
- * <p>
- * </p>
+ * <p>Benchmark worker that can execute an HTTP method given number of times</p>
+ *
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision$
- *
- * @since 4.0
*/
public class BenchmarkWorker {
private byte[] buffer = new byte[4096];
private final int verbosity;
- private final HttpRequestExecutor httpexecutor;
- private final ConnectionReuseStrategy connstrategy;
+ private final HttpClient httpexecutor;
- public BenchmarkWorker(final HttpRequestExecutor httpexecutor, int verbosity) {
+ public BenchmarkWorker(final HttpClient httpexecutor, int verbosity) {
super();
this.httpexecutor = httpexecutor;
- this.connstrategy = new DefaultConnectionReuseStrategy();
this.verbosity = verbosity;
}
public Stats execute(
- final HttpRequest request,
- final HttpClientConnection conn,
+ final HostConfiguration hostconf,
+ final HttpMethod method,
int count,
boolean keepalive) throws HttpException {
- HttpResponse response = null;
Stats stats = new Stats();
stats.start();
for (int i = 0; i < count; i++) {
try {
- response = this.httpexecutor.execute(request, conn);
+ this.httpexecutor.executeMethod(hostconf, method);
if (this.verbosity >= 4) {
- System.out.println(">> " + request.getRequestLine().toString());
- Header[] headers = request.getAllHeaders();
+ System.out.println(">> " + method.getName() + " " +
+ method.getURI() + " " + method.getParams().getVersion());
+ Header[] headers = method.getRequestHeaders();
for (int h = 0; h < headers.length; h++) {
- System.out.println(">> " + headers[h].toString());
+ System.out.print(">> " + headers[h].toString());
}
System.out.println();
}
if (this.verbosity >= 3) {
- System.out.println(response.getStatusLine().getStatusCode());
+ System.out.println(method.getStatusLine().getStatusCode());
}
if (this.verbosity >= 4) {
- System.out.println("<< " + response.getStatusLine().toString());
- Header[] headers = response.getAllHeaders();
+ System.out.println("<< " + method.getStatusLine().toString());
+ Header[] headers = method.getResponseHeaders();
for (int h = 0; h < headers.length; h++) {
- System.out.println("<< " + headers[h].toString());
+ System.out.print("<< " + headers[h].toString());
}
System.out.println();
}
- HttpEntity entity = response.getEntity();
+ InputStream instream = method.getResponseBodyAsStream();
long contentlen = 0;
- if (entity != null) {
- InputStream instream = entity.getContent();
+ if (instream != null) {
int l = 0;
while ((l = instream.read(this.buffer)) != -1) {
stats.incTotal(l);
contentlen += l;
}
}
- if (!keepalive || !this.connstrategy.keepAlive(response)) {
- conn.close();
- }
stats.setContentLength(contentlen);
stats.incSuccessCount();
} catch (IOException ex) {
@@ -114,14 +102,17 @@
if (this.verbosity >= 2) {
System.err.println("I/O error: " + ex.getMessage());
}
+ } finally {
+ method.releaseConnection();
+ }
+ if (!keepalive) {
+ this.httpexecutor.getHttpConnectionManager().closeIdleConnections(0);
}
}
stats.finish();
- if (response != null) {
- Header header = response.getFirstHeader("Server");
- if (header != null) {
- stats.setServerName(header.getValue());
- }
+ Header header = method.getResponseHeader("Server");
+ if (header != null) {
+ stats.setServerName(header.getValue());
}
return stats;
}
Added: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java?rev=388628&view=auto
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java (added)
+++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java Fri Mar 24 12:36:22 2006
@@ -0,0 +1,73 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 1999-2006 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 org.apache.commons.httpclient.contrib.benchmark;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.httpclient.methods.RequestEntity;
+
+public class FileRequestEntity implements RequestEntity {
+
+ final File file;
+ final String contentType;
+
+ public FileRequestEntity(final File file, final String contentType) {
+ super();
+ if (file == null) {
+ throw new IllegalArgumentException("File may not be null");
+ }
+ this.file = file;
+ this.contentType = contentType;
+ }
+ public long getContentLength() {
+ return this.file.length();
+ }
+
+ public String getContentType() {
+ return this.contentType;
+ }
+
+ public boolean isRepeatable() {
+ return true;
+ }
+
+ public void writeRequest(final OutputStream out) throws IOException {
+ byte[] tmp = new byte[4096];
+ int i = 0;
+ InputStream instream = new FileInputStream(this.file);
+ while ((i = instream.read(tmp)) >= 0) {
+ out.write(tmp, 0, i);
+ }
+ }
+
+}
Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/FileRequestEntity.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/HttpBenchmark.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/HttpBenchmark.java?rev=388628&r1=388545&r2=388628&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/HttpBenchmark.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/HttpBenchmark.java Fri Mar 24 12:36:22 2006
@@ -26,7 +26,7 @@
* <http://www.apache.org/>.
*
*/
-package org.apache.http.contrib.benchmark;
+package org.apache.commons.httpclient.contrib.benchmark;
import java.io.File;
import java.net.URL;
@@ -37,57 +37,30 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpVersion;
-import org.apache.http.Scheme;
-import org.apache.http.entity.FileEntity;
-import org.apache.http.impl.DefaultHttpClientConnection;
-import org.apache.http.impl.DefaultHttpParams;
-import org.apache.http.impl.io.PlainSocketFactory;
-import org.apache.http.impl.io.SSLSocketFactory;
-import org.apache.http.message.HttpGet;
-import org.apache.http.message.HttpHead;
-import org.apache.http.message.HttpPost;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.HttpProtocolParams;
-import org.apache.http.protocol.HttpRequestExecutor;
-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.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
/**
- * <p>
- * </p>
+ * <p>A simple HTTP benchmark tool, which implements a subset of AB (Apache Benchmark) interface</p>
+ *
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision$
- *
- * @since 4.0
*/
public class HttpBenchmark {
- private static HttpRequestExecutor createRequestExecutor() {
- HttpParams params = new DefaultHttpParams(null);
- params.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
- .setParameter(HttpProtocolParams.USER_AGENT, "Jakarta HttpComponents")
- .setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false)
- .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false);
-
- HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
- httpexecutor.setParams(params);
-
- // Required request interceptors
- httpexecutor.addInterceptor(new RequestContent());
- httpexecutor.addInterceptor(new RequestTargetHost());
- // Recommended request interceptors
- httpexecutor.addInterceptor(new RequestConnControl());
- httpexecutor.addInterceptor(new RequestUserAgent());
- httpexecutor.addInterceptor(new RequestExpectContinue());
- return httpexecutor;
+ private static HttpClient createRequestExecutor() {
+ HttpClient httpclient = new HttpClient();
+ httpclient.getParams().setVersion(HttpVersion.HTTP_1_1);
+ httpclient.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
+ httpclient.getHttpConnectionManager().getParams().setStaleCheckingEnabled(false);
+ return httpclient;
}
public static void main(String[] args) throws Exception {
@@ -183,22 +156,17 @@
// Parse the target url
URL url = new URL(args[0]);
- // Register standard protocol schemes
- Scheme.registerScheme("http",
- new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- Scheme.registerScheme("https",
- new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
-
- // Prepare connection
- HttpHost host = new HttpHost(
+ // Prepare host configuration
+ HostConfiguration hostconf = new HostConfiguration();
+ hostconf.setHost(
url.getHost(),
url.getPort(),
- Scheme.getScheme(url.getProtocol()));
- DefaultHttpClientConnection conn = new DefaultHttpClientConnection(host);
+ url.getProtocol());
// Prepare request
- HttpRequest request = null;
+ HttpMethod method = null;
if (cmd.hasOption('p')) {
+ PostMethod httppost = new PostMethod(url.getPath());
File file = new File(cmd.getOptionValue('p'));
if (!file.exists()) {
System.err.println("File not found: " + file);
@@ -208,32 +176,29 @@
if (cmd.hasOption('T')) {
contenttype = cmd.getOptionValue('T');
}
- FileEntity entity = new FileEntity(file, contenttype);
+ FileRequestEntity entity = new FileRequestEntity(file, contenttype);
+ httppost.setRequestEntity(entity);
if (file.length() > 100000) {
- entity.setChunked(true);
+ httppost.setContentChunked(true);
}
- HttpPost httppost = new HttpPost(url.getPath());
- httppost.setEntity(entity);
- request = httppost;
+ method = httppost;
} else if (cmd.hasOption('i')) {
- HttpHead httphead = new HttpHead(url.getPath());
- request = httphead;
+ HeadMethod httphead = new HeadMethod(url.getPath());
+ method = httphead;
} else {
- HttpGet httpget = new HttpGet(url.getPath());
- request = httpget;
+ GetMethod httpget = new GetMethod(url.getPath());
+ method = httpget;
+ }
+ if (!keepAlive) {
+ method.addRequestHeader("Connection", "close");
}
// Prepare request executor
- HttpRequestExecutor executor = createRequestExecutor();
+ HttpClient executor = createRequestExecutor();
BenchmarkWorker worker = new BenchmarkWorker(executor, verbosity);
// Execute
- Stats stats = null;
- try {
- stats = worker.execute(request, conn, num, keepAlive);
- } finally {
- conn.close();
- }
+ Stats stats = worker.execute(hostconf, method, num, keepAlive);
// Show the results
float totalTimeSec = (float)stats.getDuration() / 1000;
@@ -243,16 +208,16 @@
System.out.print("Server Software:\t");
System.out.println(stats.getServerName());
System.out.print("Server Hostname:\t");
- System.out.println(host.getHostName());
+ System.out.println(hostconf.getHost());
System.out.print("Server Port:\t\t");
- if (host.getPort() > 0) {
- System.out.println(host.getPort());
+ if (hostconf.getPort() > 0) {
+ System.out.println(hostconf.getPort());
} else {
- System.out.println(host.getScheme().getDefaultPort());
+ System.out.println(hostconf.getProtocol().getDefaultPort());
}
System.out.println();
System.out.print("Document Path:\t\t");
- System.out.println(request.getRequestLine().getUri());
+ System.out.println(method.getURI());
System.out.print("Document Length:\t");
System.out.print(stats.getContentLength());
System.out.println(" bytes");
Modified: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/Stats.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/Stats.java?rev=388628&r1=388545&r2=388628&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/Stats.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/benchmark/Stats.java Fri Mar 24 12:36:22 2006
@@ -26,16 +26,14 @@
* <http://www.apache.org/>.
*
*/
-package org.apache.http.contrib.benchmark;
+package org.apache.commons.httpclient.contrib.benchmark;
/**
- * <p>
- * </p>
+ * <p>Benchmark statistics</p>
+ *
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision$
- *
- * @since 4.0
*/
public class Stats {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org