diff --git a/httpclient-benchmark/pom.xml b/httpclient-benchmark/pom.xml index bd2fc0fd3..a5d37a6ab 100644 --- a/httpclient-benchmark/pom.xml +++ b/httpclient-benchmark/pom.xml @@ -40,6 +40,18 @@ http://hc.apache.org/httpcomponents-client jar + + + jboss + JBoss Repository + default + http://oss.sonatype.org/content/repositories/releases + + false + + + + org.apache.httpcomponents @@ -83,6 +95,12 @@ 1.5.10 compile + + com.ning + async-http-client + 1.3.3 + compile + diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java index dd53ec39a..f5d6b7c61 100644 --- a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/Benchmark.java @@ -48,8 +48,8 @@ public class Benchmark { public static void main(String[] args) throws Exception { - String ns = System.getProperty("hc.benchmark.n-requests", "100000"); - String nc = System.getProperty("hc.benchmark.concurrent", "25"); + String ns = System.getProperty("hc.benchmark.n-requests", "200000"); + String nc = System.getProperty("hc.benchmark.concurrent", "100"); String cls = System.getProperty("hc.benchmark.content-len", "2048"); int n = Integer.parseInt(ns); @@ -82,7 +82,8 @@ public class Benchmark { new TestHttpJRE(), new TestHttpCore(), new TestHttpClient4(), - new TestJettyHttpClient() + new TestJettyHttpClient(), + new TestNingHttpClient() }; byte[] content = new byte[contentLen]; diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java index c50f19223..e6d8be2b8 100644 --- a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestJettyHttpClient.java @@ -107,7 +107,7 @@ public class TestJettyHttpClient implements TestHttpAgent { @Override protected void onResponseContent(final Buffer content) throws IOException { - this.contentLen += content.length(); + this.contentLen += content.asArray().length; super.onResponseContent(content); } diff --git a/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java new file mode 100644 index 000000000..8fb390efe --- /dev/null +++ b/httpclient-benchmark/src/main/java/org/apache/http/client/benchmark/TestNingHttpClient.java @@ -0,0 +1,165 @@ +/* + * ==================================================================== + * + * 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 + * . + * + */ +package org.apache.http.client.benchmark; + +import java.io.IOException; +import java.net.URI; + +import com.ning.http.client.AsyncHandler; +import com.ning.http.client.AsyncHttpClient; +import com.ning.http.client.AsyncHttpClientConfig; +import com.ning.http.client.HttpResponseBodyPart; +import com.ning.http.client.HttpResponseHeaders; +import com.ning.http.client.HttpResponseStatus; +import com.ning.http.client.Request; + +public class TestNingHttpClient implements TestHttpAgent { + + private AsyncHttpClient client; + + public TestNingHttpClient() { + super(); + } + + public void init() throws Exception { + } + + public void shutdown() throws Exception { + this.client.close(); + } + + Stats execute(final URI targetURI, byte[] content, int n, int c) throws Exception { + if (this.client != null) { + this.client.close(); + } + AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder() + .setKeepAlive(true) + .setCompressionEnabled(false) + .setMaximumConnectionsPerHost(c) + .setMaximumConnectionsTotal(2000) + .setRequestTimeoutInMs(15000) + .build(); + this.client = new AsyncHttpClient(config); + + Stats stats = new Stats(n, c); + + for (int i = 0; i < n; i++) { + Request request; + if (content == null) { + request = this.client.prepareGet(targetURI.toASCIIString()) + .build(); + } else { + request = this.client.preparePost(targetURI.toASCIIString()) + .setBody(content) + .build(); + } + try { + this.client.executeRequest(request, new SimpleAsyncHandler(stats)); + } catch (IOException ex) { + } + } + stats.waitFor(); + return stats; + } + + public Stats get(final URI target, int n, int c) throws Exception { + return execute(target, null, n, c); + } + + public Stats post(final URI target, byte[] content, int n, int c) throws Exception { + return execute(target, content, n, c); + } + + public String getClientName() { + return "Ning Async HTTP client 1.3"; + } + + static class SimpleAsyncHandler implements AsyncHandler { + + private final Stats stats; + private int status = 0; + private long contentLen = 0; + + SimpleAsyncHandler(final Stats stats) { + super(); + this.stats = stats; + } + + public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception { + this.status = responseStatus.getStatusCode(); + return STATE.CONTINUE; + } + + public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception { + return STATE.CONTINUE; + } + + public STATE onBodyPartReceived(final HttpResponseBodyPart bodyPart) throws Exception { + this.contentLen += bodyPart.getBodyPartBytes().length; + return STATE.CONTINUE; + } + + public Object onCompleted() throws Exception { + if (this.status == 200) { + this.stats.success(this.contentLen); + } else { + this.stats.failure(this.contentLen); + } + return STATE.CONTINUE; + } + + public void onThrowable(final Throwable t) { + this.stats.failure(this.contentLen); + } + + }; + + public static void main(String[] args) throws Exception { + if (args.length < 2) { + System.out.println("Usage: "); + System.exit(-1); + } + URI targetURI = new URI(args[0]); + int n = Integer.parseInt(args[1]); + int c = 1; + if (args.length > 2) { + c = Integer.parseInt(args[2]); + } + + TestNingHttpClient test = new TestNingHttpClient(); + test.init(); + try { + long startTime = System.currentTimeMillis(); + Stats stats = test.get(targetURI, n, c); + long finishTime = System.currentTimeMillis(); + + Stats.printStats(targetURI, startTime, finishTime, stats); + } finally { + test.shutdown(); + } + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2f6509291..faeec05c2 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ maven-notice-plugin org.apache.httpcomponents + 0.0.2-SNAPSHOT attach-notice-license