You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/09/24 19:58:27 UTC
svn commit: r1389501 - in /httpcomponents/httpcore/trunk/httpcore-ab: ./
src/main/java/org/apache/http/benchmark/ src/test/ src/test/java/
src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/http/
src/test/java/org/apache/http/benchmark/
Author: olegk
Date: Mon Sep 24 17:58:26 2012
New Revision: 1389501
URL: http://svn.apache.org/viewvc?rev=1389501&view=rev
Log:
Refactored HTTP benchmark; added a basic smoke test
Added:
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java (with props)
httpcomponents/httpcore/trunk/httpcore-ab/src/test/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java (with props)
httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java (with props)
Modified:
httpcomponents/httpcore/trunk/httpcore-ab/pom.xml
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
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/HttpBenchmark.java
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/ResultProcessor.java
Modified: httpcomponents/httpcore/trunk/httpcore-ab/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/pom.xml?rev=1389501&r1=1389500&r2=1389501&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/pom.xml (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/pom.xml Mon Sep 24 17:58:26 2012
@@ -53,6 +53,11 @@
<version>1.2</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<properties>
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java?rev=1389501&r1=1389500&r2=1389501&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkConnection.java Mon Sep 24 17:58:26 2012
@@ -26,59 +26,31 @@
*/
package org.apache.http.benchmark;
-import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import org.apache.http.HttpException;
-import org.apache.http.HttpMessage;
-import org.apache.http.entity.BasicHttpEntity;
-import org.apache.http.impl.DefaultHttpClientConnection;
-import org.apache.http.impl.entity.EntityDeserializer;
-import org.apache.http.impl.entity.EntitySerializer;
-import org.apache.http.impl.entity.LaxContentLengthStrategy;
-import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.impl.DefaultBHttpClientConnection;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.io.SessionOutputBuffer;
+import org.apache.http.params.HttpParams;
-class BenchmarkConnection extends DefaultHttpClientConnection {
+class BenchmarkConnection extends DefaultBHttpClientConnection {
private final Stats stats;
- BenchmarkConnection(final Stats stats) {
- super();
+ BenchmarkConnection(final Stats stats, final HttpParams params) {
+ super(params);
this.stats = stats;
}
@Override
- protected EntityDeserializer createEntityDeserializer() {
- return new EntityDeserializer(new LaxContentLengthStrategy()) {
-
- @Override
- protected BasicHttpEntity doDeserialize(
- final SessionInputBuffer inbuffer,
- final HttpMessage message) throws HttpException, IOException {
- BasicHttpEntity entity = super.doDeserialize(inbuffer, message);
- InputStream instream = entity.getContent();
- entity.setContent(new CountingInputStream(instream, stats));
- return entity;
- }
-
- };
+ protected OutputStream createOutputStream(final long len, final SessionOutputBuffer outbuffer) {
+ return new CountingOutputStream(super.createOutputStream(len, outbuffer), this.stats);
}
@Override
- protected EntitySerializer createEntitySerializer() {
- return new EntitySerializer(new StrictContentLengthStrategy()) {
-
- @Override
- protected OutputStream doSerialize(
- final SessionOutputBuffer outbuffer,
- final HttpMessage message) throws HttpException, IOException {
- return new CountingOutputStream(super.doSerialize(outbuffer, message), stats);
- }
-
- };
+ protected InputStream createInputStream(final long len, final SessionInputBuffer inbuffer) {
+ return new CountingInputStream(super.createInputStream(len, inbuffer), this.stats);
}
-
+
}
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=1389501&r1=1389500&r2=1389501&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 Mon Sep 24 17:58:26 2012
@@ -88,7 +88,6 @@ class BenchmarkWorker implements Runnabl
boolean keepalive,
int verbosity,
final SocketFactory socketFactory) {
-
super();
this.context = new BasicHttpContext(null);
this.request = request;
@@ -112,7 +111,7 @@ class BenchmarkWorker implements Runnabl
public void run() {
HttpResponse response = null;
- BenchmarkConnection conn = new BenchmarkConnection(this.stats);
+ BenchmarkConnection conn = new BenchmarkConnection(stats, request.getParams());
String scheme = targetHost.getSchemeName();
String hostname = targetHost.getHostName();
@@ -151,7 +150,7 @@ class BenchmarkWorker implements Runnabl
socket.setSoTimeout(soTimeout);
socket.connect(new InetSocketAddress(hostname, port), connTimeout);
- conn.bind(socket, params);
+ conn.bind(socket);
}
try {
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=1389501&r1=1389500&r2=1389501&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 Mon Sep 24 17:58:26 2012
@@ -173,6 +173,12 @@ public class HttpBenchmark {
}
public String execute() throws Exception {
+ Results results = doExecute();
+ ResultProcessor.printResults(results);
+ return "";
+ }
+
+ public Results doExecute() throws Exception {
URL url = config.getUrl();
HttpHost host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
@@ -266,7 +272,7 @@ public class HttpBenchmark {
}
workerPool.shutdown();
- return ResultProcessor.printResults(workers, host, config.getUrl().toString());
+ return ResultProcessor.collectResults(workers, host, config.getUrl().toString());
}
}
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=1389501&r1=1389500&r2=1389501&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 Mon Sep 24 17:58:26 2012
@@ -45,9 +45,8 @@ public class ResultProcessor {
nf6.setMinimumFractionDigits(6);
}
- static String printResults(BenchmarkWorker[] workers, HttpHost host, String uri) {
-
- double totalTimeNano = 0;
+ static Results collectResults(BenchmarkWorker[] workers, HttpHost host, String uri) {
+ long totalTimeNano = 0;
long successCount = 0;
long failureCount = 0;
long writeErrors = 0;
@@ -68,44 +67,56 @@ public class ResultProcessor {
totalBytesSent += s.getTotalBytesSent();
}
- int threads = workers.length;
- double totalTimeMs = (totalTimeNano / threads) / 1000000; // convert nano secs to milli secs
- double timePerReqMs = totalTimeMs / successCount;
- double totalTimeSec = totalTimeMs / 1000;
- double reqsPerSec = successCount / totalTimeSec;
- long totalBytes = totalBytesRcvd + (totalBytesSent > 0 ? totalBytesSent : 0);
+ Results results = new Results();
+ results.serverName = stats.getServerName();
+ results.hostName = host.getHostName();
+ results.hostPort = host.getPort() > 0 ? host.getPort() :
+ host.getSchemeName().equalsIgnoreCase("https") ? 443 : 80;
+ results.documentPath = uri;
+ results.contentLength = stats.getContentLength();
+ results.concurrencyLevel = workers.length;
+ results.totalTimeNano = totalTimeNano;
+ results.successCount = successCount;
+ results.failureCount = failureCount;
+ results.writeErrors = writeErrors;
+ results.keepAliveCount = keepAliveCount;
+ results.totalBytesRcvd = totalBytesRcvd;
+ results.totalBytesSent = totalBytesSent;
+ results.totalBytes = totalBytesRcvd + (totalBytesSent > 0 ? totalBytesSent : 0);
+ return results;
+ }
- StringBuilder sb = new StringBuilder(1024);
+ static void printResults(final Results results) {
+ int threads = results.getConcurrencyLevel();
+ double totalTimeMs = (results.getTotalTimeNano() / threads) / 1000000; // convert nano secs to milli secs
+ double timePerReqMs = totalTimeMs / results.getSuccessCount();
+ double totalTimeSec = totalTimeMs / 1000;
+ double reqsPerSec = results.getSuccessCount() / totalTimeSec;
- 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 ? Integer.valueOf(host.getPort()) : uri.startsWith("https") ? "443" : "80") + "\n");
- 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) +
+ System.out.println("\nServer Software:\t\t" + results.getServerName());
+ System.out.println( "Server Hostname:\t\t" + results.getHostName());
+ System.out.println( "Server Port:\t\t\t" + Integer.valueOf(results.getHostPort()));
+ System.out.println( "Document Path:\t\t\t" + results.getDocumentPath());
+ System.out.println( "Document Length:\t\t" + results.getContentLength() + " bytes\n");
+ System.out.println( "Concurrency Level:\t\t" + results.getConcurrencyLevel());
+ System.out.println( "Time taken for tests:\t\t" + nf6.format(totalTimeSec) + " seconds");
+ System.out.println( "Complete requests:\t\t" + results.getSuccessCount());
+ System.out.println( "Failed requests:\t\t" + results.getFailureCount());
+ System.out.println( "Write errors:\t\t\t" + results.getWriteErrors());
+ System.out.println( "Kept alive:\t\t\t" + results.getKeepAliveCount());
+ System.out.println( "Total transferred:\t\t" + results.getTotalBytes() + " 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
+ * results.getConcurrencyLevel()) + " [ms] (mean)");
+ System.out.println( "Time per request:\t\t" + nf3.format(timePerReqMs) +
" [ms] (mean, across all concurrent requests)");
- printAndAppend(sb, "Transfer rate:\t\t\t" +
- nf2.format(totalBytesRcvd/1000/totalTimeSec) + " [Kbytes/sec] received");
- printAndAppend(sb, "\t\t\t\t" +
- (totalBytesSent > 0 ? nf2.format(totalBytesSent/1000/totalTimeSec) : Integer.valueOf(-1)) + " kb/s sent");
- printAndAppend(sb, "\t\t\t\t" +
- nf2.format(totalBytes/1000/totalTimeSec) + " kb/s total");
-
- return sb.toString();
+ System.out.println( "Transfer rate:\t\t\t" +
+ nf2.format(results.getTotalBytesRcvd() / 1000 / totalTimeSec) + " [Kbytes/sec] received");
+ System.out.println( "\t\t\t\t" +
+ (results.getTotalBytesSent() > 0 ? nf2.format(results.getTotalBytesSent()
+ / 1000 / totalTimeSec) : Integer.valueOf(-1)) + " kb/s sent");
+ System.out.println( "\t\t\t\t" +
+ nf2.format(results.getTotalBytes() / 1000 / totalTimeSec) + " kb/s total");
}
- private static void printAndAppend(StringBuilder sb, String s) {
- System.out.println(s);
- sb.append(s).append("\r\n");
- }
}
Added: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java?rev=1389501&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java Mon Sep 24 17:58:26 2012
@@ -0,0 +1,133 @@
+/*
+ * ====================================================================
+ * 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.benchmark;
+
+/**
+ * Benchmark results
+ *
+ * @since 4.3
+ */
+public final class Results {
+
+ String serverName;
+ String hostName;
+ int hostPort;
+ String documentPath;
+ long contentLength;
+ int concurrencyLevel;
+ long totalTimeNano;
+ long successCount;
+ long failureCount;
+ long writeErrors;
+ long keepAliveCount;
+ long totalBytesRcvd;
+ long totalBytesSent;
+ long totalBytes;
+
+ Results() {
+ super();
+ this.contentLength = -1;
+ }
+
+ public String getServerName() {
+ return serverName;
+ }
+
+ public String getHostName() {
+ return hostName;
+ }
+
+ public int getHostPort() {
+ return hostPort;
+ }
+
+ public String getDocumentPath() {
+ return documentPath;
+ }
+
+ public long getContentLength() {
+ return contentLength;
+ }
+
+ public int getConcurrencyLevel() {
+ return concurrencyLevel;
+ }
+
+ public long getTotalTimeNano() {
+ return totalTimeNano;
+ }
+
+ public long getSuccessCount() {
+ return successCount;
+ }
+
+ public long getFailureCount() {
+ return failureCount;
+ }
+
+ public long getWriteErrors() {
+ return writeErrors;
+ }
+
+ public long getKeepAliveCount() {
+ return keepAliveCount;
+ }
+
+ public long getTotalBytesRcvd() {
+ return totalBytesRcvd;
+ }
+
+ public long getTotalBytesSent() {
+ return totalBytesSent;
+ }
+
+ public long getTotalBytes() {
+ return totalBytes;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("[serverName=").append(serverName)
+ .append(", hostName=").append(hostName)
+ .append(", hostPort=").append(hostPort)
+ .append(", documentPath=").append(documentPath)
+ .append(", contentLength=").append(contentLength)
+ .append(", concurrencyLevel=").append(concurrencyLevel)
+ .append(", totalTimeNano=").append(totalTimeNano)
+ .append(", successCount=").append(successCount)
+ .append(", failureCount=").append(failureCount)
+ .append(", writeErrors=").append(writeErrors)
+ .append(", keepAliveCount=").append(keepAliveCount)
+ .append(", totalBytesRcvd=").append(totalBytesRcvd)
+ .append(", totalBytesSent=").append(totalBytesSent)
+ .append(", totalBytes=").append(totalBytes)
+ .append("]");
+ return builder.toString();
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Results.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java?rev=1389501&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java Mon Sep 24 17:58:26 2012
@@ -0,0 +1,184 @@
+/*
+ * ====================================================================
+ * 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.benchmark;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import org.apache.http.ConnectionClosedException;
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.HttpServerConnection;
+import org.apache.http.impl.DefaultBHttpServerConnection;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpService;
+import org.apache.http.protocol.ImmutableHttpProcessor;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+import org.apache.http.protocol.UriHttpRequestHandlerMapper;
+
+public class HttpServer {
+
+ private final HttpParams params;
+ private final HttpProcessor httpproc;
+ private final UriHttpRequestHandlerMapper reqistry;
+ private final ServerSocket serversocket;
+
+ private Thread listener;
+ private volatile boolean shutdown;
+
+ public HttpServer(final HttpParams params) throws IOException {
+ super();
+ this.params = params;
+ this.httpproc = new ImmutableHttpProcessor(
+ new HttpResponseInterceptor[] {
+ new ResponseDate(),
+ new ResponseServer(),
+ new ResponseContent(),
+ new ResponseConnControl()
+ });
+ this.reqistry = new UriHttpRequestHandlerMapper();
+ this.serversocket = new ServerSocket(0);
+ }
+
+ public void registerHandler(
+ final String pattern,
+ final HttpRequestHandler handler) {
+ this.reqistry.register(pattern, handler);
+ }
+
+ private HttpServerConnection acceptConnection() throws IOException {
+ Socket socket = this.serversocket.accept();
+ DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(this.params);
+ conn.bind(socket);
+ return conn;
+ }
+
+ public int getPort() {
+ return this.serversocket.getLocalPort();
+ }
+
+ public InetAddress getInetAddress() {
+ return this.serversocket.getInetAddress();
+ }
+
+ public void start() {
+ if (this.listener != null) {
+ throw new IllegalStateException("Listener already running");
+ }
+ this.listener = new Thread(new Runnable() {
+
+ public void run() {
+ while (!shutdown && !Thread.interrupted()) {
+ try {
+ // Set up HTTP connection
+ HttpServerConnection conn = acceptConnection();
+ // Set up the HTTP service
+ HttpService httpService = new HttpService(
+ httpproc,
+ DefaultConnectionReuseStrategy.INSTANCE,
+ DefaultHttpResponseFactory.INSTANCE,
+ reqistry,
+ null,
+ params);
+ // Start worker thread
+ Thread t = new WorkerThread(httpService, conn);
+ t.setDaemon(true);
+ t.start();
+ } catch (InterruptedIOException ex) {
+ break;
+ } catch (IOException e) {
+ break;
+ }
+ }
+ }
+
+ });
+ this.listener.start();
+ }
+
+ public void shutdown() {
+ if (this.shutdown) {
+ return;
+ }
+ this.shutdown = true;
+ try {
+ this.serversocket.close();
+ } catch (IOException ignore) {}
+ this.listener.interrupt();
+ try {
+ this.listener.join(1000);
+ } catch (InterruptedException ignore) {}
+ }
+
+ static class WorkerThread extends Thread {
+
+ private final HttpService httpservice;
+ private final HttpServerConnection conn;
+
+ public WorkerThread(
+ final HttpService httpservice,
+ final HttpServerConnection conn) {
+ super();
+ this.httpservice = httpservice;
+ this.conn = conn;
+ }
+
+ @Override
+ public void run() {
+ HttpContext context = new BasicHttpContext(null);
+ try {
+ while (!Thread.interrupted() && this.conn.isOpen()) {
+ this.httpservice.handleRequest(this.conn, context);
+ }
+ } catch (ConnectionClosedException ex) {
+ } catch (IOException ex) {
+ System.err.println("I/O error: " + ex.getMessage());
+ } catch (HttpException ex) {
+ System.err.println("Unrecoverable HTTP protocol violation: " + ex.getMessage());
+ } finally {
+ try {
+ this.conn.shutdown();
+ } catch (IOException ignore) {}
+ }
+ }
+
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/HttpServer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java?rev=1389501&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java Mon Sep 24 17:58:26 2012
@@ -0,0 +1,71 @@
+package org.apache.http.benchmark;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.params.HttpCoreConfigBuilder;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SmokeTest {
+
+ private HttpServer server;
+
+ @Before
+ public void setup() throws Exception {
+ HttpParams params = new HttpCoreConfigBuilder()
+ .setConnectTimeout(5000)
+ .setSocketTimeout(5000)
+ .setSocketBufferSize(8 * 1024)
+ .setOriginServer("TEST-SERVER/1.1").build();
+ server = new HttpServer(params);
+ server.registerHandler("/", new HttpRequestHandler() {
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+ response.setStatusCode(HttpStatus.SC_OK);
+ response.setEntity(new StringEntity("0123456789ABCDEF", ContentType.TEXT_PLAIN));
+ }
+ });
+ server.start();
+ }
+
+ @After
+ public void shutdown() throws Exception {
+ server.shutdown();
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ Config config = new Config();
+ config.setKeepAlive(true);
+ config.setMethod("GET");
+ config.setUrl(new URL("http://localhost:" + server.getPort() + "/"));
+ config.setThreads(3);
+ config.setRequests(100);
+ HttpBenchmark httpBenchmark = new HttpBenchmark(config);
+ Results results = httpBenchmark.doExecute();
+ Assert.assertNotNull(results);
+ Assert.assertEquals(16, results.getContentLength());
+ Assert.assertEquals(3, results.getConcurrencyLevel());
+ Assert.assertEquals(300, results.getKeepAliveCount());
+ Assert.assertEquals(300, results.getSuccessCount());
+ Assert.assertEquals(0, results.getFailureCount());
+ Assert.assertEquals(0, results.getWriteErrors());
+ Assert.assertEquals(300 * 16, results.getTotalBytes());
+ Assert.assertEquals(300 * 16, results.getTotalBytesRcvd());
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-ab/src/test/java/org/apache/http/benchmark/SmokeTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain