001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/contrib/org/apache/commons/httpclient/contrib/benchmark/BenchmarkWorker.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.contrib.benchmark;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034:
035: import org.apache.commons.httpclient.Header;
036: import org.apache.commons.httpclient.HostConfiguration;
037: import org.apache.commons.httpclient.HttpClient;
038: import org.apache.commons.httpclient.HttpException;
039: import org.apache.commons.httpclient.HttpMethod;
040:
041: /**
042: * <p>Benchmark worker that can execute an HTTP method given number of times</p>
043: *
044: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
045: *
046: * @version $Revision: 480424 $
047: */
048: public class BenchmarkWorker {
049:
050: private byte[] buffer = new byte[4096];
051: private final int verbosity;
052: private final HttpClient httpexecutor;
053:
054: public BenchmarkWorker(final HttpClient httpexecutor, int verbosity) {
055: super ();
056: this .httpexecutor = httpexecutor;
057: this .verbosity = verbosity;
058: }
059:
060: public Stats execute(final HostConfiguration hostconf,
061: final HttpMethod method, int count, boolean keepalive)
062: throws HttpException {
063: Stats stats = new Stats();
064: stats.start();
065: for (int i = 0; i < count; i++) {
066: try {
067: this .httpexecutor.executeMethod(hostconf, method);
068: if (this .verbosity >= 4) {
069: System.out.println(">> " + method.getName() + " "
070: + method.getURI() + " "
071: + method.getParams().getVersion());
072: Header[] headers = method.getRequestHeaders();
073: for (int h = 0; h < headers.length; h++) {
074: System.out.print(">> " + headers[h].toString());
075: }
076: System.out.println();
077: }
078: if (this .verbosity >= 3) {
079: System.out.println(method.getStatusLine()
080: .getStatusCode());
081: }
082: if (this .verbosity >= 4) {
083: System.out.println("<< "
084: + method.getStatusLine().toString());
085: Header[] headers = method.getResponseHeaders();
086: for (int h = 0; h < headers.length; h++) {
087: System.out.print("<< " + headers[h].toString());
088: }
089: System.out.println();
090: }
091: InputStream instream = method.getResponseBodyAsStream();
092: long contentlen = 0;
093: if (instream != null) {
094: int l = 0;
095: while ((l = instream.read(this .buffer)) != -1) {
096: stats.incTotal(l);
097: contentlen += l;
098: }
099: }
100: stats.setContentLength(contentlen);
101: stats.incSuccessCount();
102: } catch (IOException ex) {
103: stats.incFailureCount();
104: if (this .verbosity >= 2) {
105: System.err.println("I/O error: " + ex.getMessage());
106: }
107: } finally {
108: method.releaseConnection();
109: }
110: if (!keepalive) {
111: this .httpexecutor.getHttpConnectionManager()
112: .closeIdleConnections(0);
113: }
114: }
115: stats.finish();
116: Header header = method.getResponseHeader("Server");
117: if (header != null) {
118: stats.setServerName(header.getValue());
119: }
120: return stats;
121: }
122:
123: }
|