Android HttpClient性能 [英] Android HttpClient performance

查看:105
本文介绍了Android HttpClient性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用许多对Web服务的http请求的android应用.首先,我在每个请求之前创建一个新的HttpClient实例. 为了提高性能,我尝试在多个线程中进行请求.因此,我使用ThreadSafeConnectionManager创建了一个由所有线程共享的HttpClient实例:

I developing android app which uses a lot of http requests to web service. At first, I was creating a new HttpClient instance before every request. To increase performance I try to do requests in many threads. So, I created single HttpClient instance, shared by all threads, using ThreadSafeConnectionManager:

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

BasicHttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, true);

ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, registry);
HttpClient client = new DefaultHttpClient(connManager, params);

但是性能下降了,令我惊讶.我已经测量了时间,可以用这种时间来满足请求:

But performance decreased, to my surprise. I have measured time, to be spended to exequte requests in such way:

long startTime = System.currentTimeMillis();
HttpResponse response = client.execute(postRequest);
long reqTime = System.currentTimeMillis() - startTime;
Log.i("SyncTimer", "Request time:" + reqTime);

这是一条日志,我通过简单的DefaultHttpClient获得此请求,而每个请求都没有参数新实例:

Here this is a log, which I get with simple DefaultHttpClient without parameters new instance per request:

01-11 11:10:51.136: INFO/SyncTimer(18400): Request time:1076
01-11 11:10:54.686: INFO/SyncTimer(18400): Request time:1051
01-11 11:10:57.996: INFO/SyncTimer(18400): Request time:1054
01-11 11:10:59.166: INFO/SyncTimer(18400): Request time:1070
01-11 11:11:00.346: INFO/SyncTimer(18400): Request time:1172
01-11 11:11:02.656: INFO/SyncTimer(18400): Request time:1043

我从ThreadSafeClientConnManager和单个HttpClient实例中得到了什么:

And what I get with ThreadSafeClientConnManager and single HttpClient instance:

01-11 11:06:06.926: INFO/SyncTimer(18267): Request time:7001
01-11 11:06:10.412: INFO/SyncTimer(18267): Request time:3385
01-11 11:06:20.222: INFO/SyncTimer(18267): Request time:9801
01-11 11:06:23.622: INFO/SyncTimer(18267): Request time:2058
01-11 11:06:29.906: INFO/SyncTimer(18267): Request time:6268
01-11 11:06:34.746: INFO/SyncTimer(18267): Request time:3525
01-11 11:06:50.302: INFO/SyncTimer(18267): Request time:15551

会发生什么,我该如何应对?

What happens and how can I fight this?

更新

使用Keep-alive优势-这就是我想要的.但是当我为每个请求连接创建新的HttpClient实例时,无法重用.尽管如此,这样的版本运行速度更快,原因我不清楚.

Use keep-alive advantage - is what I want. But when I create new HttpClient instance for every request connection can not be reused. Despite of this, such version runs faster, reasons of it is unclear for me.

推荐答案

这一切都非常简单.默认情况下,HttpClient仅允许HTTP规范要求的两个并发连接到同一目标主机.因此,实际上,您的工作线程将大部分执行时间都花在了阻塞上,以等待这两个连接可用.

It is all very simple. HttpClient per default allows only two concurrent connections to the same target host as required by the HTTP specification. So, effectively your worker threads spend most of their execution time blocked waiting for those two connections to become available.

您应该增加每个路由的最大连接数"限制,以减少/消除工作线程争用.

You should increase the 'max connections per route' limit to reduce / eliminate worker thread contention.

您可能还想查看Apache HttpComponents项目用于衡量HttpClient性能的基准.

You might also want to check out the benchmark used by Apache HttpComponents project to measure performance of HttpClient.

http://wiki.apache.org/HttpComponents/HttpClient3vsHttpClient4vsHttpCore

这篇关于Android HttpClient性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆