AsyncHttpClient 跟httpclient 性能对比(使用NettyAsyncHttpProvider,效率更高)

oyhk 学习笔记

AsyncHttpClient 补充测试,昨晚花了点时间去研究了 AsyncHttpClient 发现它还有几种基本其它框架的实现方式,在API文档中看出来的,现在介绍下其它基于其它方式去请求服务器...

下面是三种情况的性能对比

  • com.ning.http.client.providers.apache
    • 这个是基本apache httpclient 3.1的提供实现...效率自己去试吧,经过httpclient4.1.1,我不想再去测试性能了,想测试的朋友们可以自己测试
  • com.ning.http.client.providers.grizzly
    • 这个是基本grizzly 2.0 的提供实现,Grizzly是一个应用程序框架,专门用于解决编写成千上万用户访问服务器时候产生的各种问题。Grizzly框架诞生于GlassFish项目,能够帮助开发人员利用Java NIO API构建可扩展、高性能、健壮的服务器,编写出可伸缩的服务器端应用。 具体的话,去官网参考吧..这个测试我也没有去实现,实现起来,有点麻烦,要搭环境
package com.oyhk.asynchttpclient;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
import com.ning.http.client.Response;
import com.ning.http.client.providers.netty.NettyAsyncHttpProvider;
import com.ning.http.client.providers.netty.NettyResponse;

@SuppressWarnings("unused")
public class AsyncHttpClientTest {

	public static int runCount = 10000;

	@Test
	public void main() {
		// --------------AsyncHttpClient-------------------------
		long start1 = System.currentTimeMillis();
		this.AsyncHttpClientTest1();
		long end1 = System.currentTimeMillis();
		System.out.println("使用AsyncHttpClient 共用时间 " + (end1 - start1) + "ms");
		// --------------NettyAsyncHttpProvider-------------------------
		long start2 = System.currentTimeMillis();
		this.AsyncHttpClientTest2();
		long end2 = System.currentTimeMillis();
		System.out.println("使用AsyncHttpClient中的NettyAsyncHttpProvider 共用时间 " + (end2 - start2) + "ms");
		// --------------httpClient-------------------------
		long start11 = System.currentTimeMillis();
		this.HttpCientTest();
		long end11 = System.currentTimeMillis();
		System.out.println("使用HttpClient 共用时间 " + (end11 - start11) + "ms");
	}

	@Test
	public void AsyncHttpClientTest1() {
		try {
			AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
			builder.setMaximumConnectionsTotal(10000);
			AsyncHttpClient asyncHttpClient = new AsyncHttpClient(builder.build());
			for (int i = 0; i < runCount; i++) {
				Futurefuture = asyncHttpClient.prepareGet("http://localhost:8802/comment/get").execute();
				NettyResponse nettyResponse = (NettyResponse) future.get();
				// System.out.println(nettyResponse.getResponseBody());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void AsyncHttpClientTest2() {
		try {
			Builder config = new AsyncHttpClientConfig.Builder();
			NettyAsyncHttpProvider nettyAsyncHttpProvider = new NettyAsyncHttpProvider(config.build());
			AsyncHttpClient asyncHttpClient = new AsyncHttpClient(nettyAsyncHttpProvider);
			for (int i = 0; i < runCount; i++) {
				Futurefuture = asyncHttpClient.prepareGet("http://localhost:8802/comment/get").execute();
				NettyResponse nettyResponse = (NettyResponse) future.get();
				// System.out.println(nettyResponse.getResponseBody());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void HttpCientTest() {
		try {
			for (int i = 0; i < runCount; i++) {
				HttpClient httpClient = new DefaultHttpClient();
				HttpGet httpGet = new HttpGet("http://localhost:8802/comment/get");
				HttpResponse httpResponse = httpClient.execute(httpGet);
				HttpEntity entity = httpResponse.getEntity();
				// System.out.println(entity.getContent());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
下面是输出结果:   大家参考参考
使用AsyncHttpClient 共用时间 8505ms
使用AsyncHttpClient中的NettyAsyncHttpProvider 共用时间 5981ms
使用HttpClient 共用时间 28190ms
有写得不对的地方,欢迎大家指正