/*
* ====================================================================
*
* 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()
.setAllowPoolingConnection(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.6.4";
}
static class SimpleAsyncHandler implements AsyncHandler