D***0 发帖数: 138 | 1 有很多连续的http request,如果每次都重新open一个connection,performance下降
很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方
法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有
没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call
HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问
题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。
String httpsURL = "https://xxxx.yyyy.com/users.json";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("PUT");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream()
);
JSONObject json = new JSONObject();
json.put("first", "xxx");
json.put("last", "yyy");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();
json.put("first", "aaa");
json.put("last", "bbbb");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();
json.put("first", "eee");
json.put("last", "rrrr");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();
input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
System.out.print( (char)c );
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
input.close();
output.close();
con.disconnect(); |
g*****g 发帖数: 34805 | 2 Try a client library such as Apache HttpClient instead of trying to reinvent
the wheel. It's easy to configure connection management from there.
call
);
【在 D***0 的大作中提到】 : 有很多连续的http request,如果每次都重新open一个connection,performance下降 : 很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方 : 法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有 : 没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call : HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问 : 题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。 : String httpsURL = "https://xxxx.yyyy.com/users.json"; : URL myurl = new URL(httpsURL); : HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); : con.setRequestMethod("PUT");
|
o**2 发帖数: 168 | |
D***0 发帖数: 138 | 4 thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否?
reinvent
【在 g*****g 的大作中提到】 : Try a client library such as Apache HttpClient instead of trying to reinvent : the wheel. It's easy to configure connection management from there. : : call : );
|
D***0 发帖数: 138 | |
g*****g 发帖数: 34805 | 6 sure
【在 D***0 的大作中提到】 : thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否? : : reinvent
|
g**e 发帖数: 6127 | 7 use PoolingClientConnectionManager
【在 D***0 的大作中提到】 : thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否? : : reinvent
|
D***0 发帖数: 138 | 8 Apache commons HttpClient?
【在 g**e 的大作中提到】 : use PoolingClientConnectionManager
|
D***0 发帖数: 138 | 9 谢谢,刚才写了段小程序测试,能写,但是performance一样没达到要求,不知道是我
写的问题还是,因为那个httpput还是每次都new,然后httpClient.execute( httpPut
);在发送完所有的data之后我才
if(mgr != null)
{
mgr.shutdown();
}
if(httpClient != null)
{
httpClient.getConnectionManager().shutdown();
}
不知道是不是因为每次都new httpput的原因。请指教。
【在 g**e 的大作中提到】 : use PoolingClientConnectionManager
|
o**2 发帖数: 168 | 10 Chunked Encoding is part of HTTP 1.1.
【在 D***0 的大作中提到】 : I can only control my side(client side).
|
|
|
g*****g 发帖数: 34805 | 11 If the requests can be executed concurrently, you should do that along with
PoolingClientConnectionManager. If the requests have to be executed
sequentially, there's not much to optimize there beyond keep-alive which the
framework has taken care of. You may still be able to execute multiple
transactions concurrently and achieve high throughput.
Ning Async httpclient is a better option if you need to push the performance
to its extreme.
httpPut
【在 D***0 的大作中提到】 : 谢谢,刚才写了段小程序测试,能写,但是performance一样没达到要求,不知道是我 : 写的问题还是,因为那个httpput还是每次都new,然后httpClient.execute( httpPut : );在发送完所有的data之后我才 : if(mgr != null) : { : mgr.shutdown(); : } : if(httpClient != null) : { : httpClient.getConnectionManager().shutdown();
|
D***0 发帖数: 138 | 12 谢谢,的确这些request不能concurrent,必须sequential,再去试试Ning Async
httpclient。谢谢好虫
with
the
performance
【在 g*****g 的大作中提到】 : If the requests can be executed concurrently, you should do that along with : PoolingClientConnectionManager. If the requests have to be executed : sequentially, there's not much to optimize there beyond keep-alive which the : framework has taken care of. You may still be able to execute multiple : transactions concurrently and achieve high throughput. : Ning Async httpclient is a better option if you need to push the performance : to its extreme. : : httpPut
|
g*****g 发帖数: 34805 | 13 I don't think Async will help your case either. For a single transaction
with multiple sequential requests, the optimization is mostly on server side.
【在 D***0 的大作中提到】 : 谢谢,的确这些request不能concurrent,必须sequential,再去试试Ning Async : httpclient。谢谢好虫 : : with : the : performance
|
D***0 发帖数: 138 | 14 yeah, but the server is out of my control and I can do nothing to change it.
side.
【在 g*****g 的大作中提到】 : I don't think Async will help your case either. For a single transaction : with multiple sequential requests, the optimization is mostly on server side.
|
b***i 发帖数: 3043 | 15 你需要的指标到底怎样?比如,需要每秒钟几次?
call
);
【在 D***0 的大作中提到】 : 有很多连续的http request,如果每次都重新open一个connection,performance下降 : 很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方 : 法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有 : 没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call : HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问 : 题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。 : String httpsURL = "https://xxxx.yyyy.com/users.json"; : URL myurl = new URL(httpsURL); : HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); : con.setRequestMethod("PUT");
|
s******e 发帖数: 493 | 16 there are four tcp/ip keep alive mechanisms (server push). They are Polling,
long polling, http streaming and web socket. Among them only websocket is a
real duplex mechanism. But unfortunately not all browsers support websocket
.
Frameworks can make the connection management transparent. try atmosphere
framework to see if it meets your need. |
D***0 发帖数: 138 | 17 没有什么具体指标,越快越好,因为我希望做成real time update。数据都是根据
simulation结果产生的,所以没有每秒几次之说。
【在 b***i 的大作中提到】 : 你需要的指标到底怎样?比如,需要每秒钟几次? : : call : );
|
D***0 发帖数: 138 | 18 谢谢指点,
恩,这四种其实最想使用web socket,以前弄过一段,但是就是因为浏览器的原因,就
没往这个方向走,其实主要还是想实现一个前台控制,后台push的结构。主要是计算都
在后台,前台实时更新显示和接收user各种控制点击。试过pushlet,也知道comet,但
是没在实际当中用过。感觉web socket是最符合要求的。现在这个问题是看见了有个
firebase的东东,看看能不能用它当中转,目前写的一些测试程序显示performance不
是很好,所以才来问问。
Polling,
a
websocket
【在 s******e 的大作中提到】 : there are four tcp/ip keep alive mechanisms (server push). They are Polling, : long polling, http streaming and web socket. Among them only websocket is a : real duplex mechanism. But unfortunately not all browsers support websocket : . : Frameworks can make the connection management transparent. try atmosphere : framework to see if it meets your need.
|
o**2 发帖数: 168 | |
D***0 发帖数: 138 | 20 还在试验,现在还不知道是不是因为第三方的performance的原因,还是我的写法有问
题,还是java本身的performance的问题,谢谢关心
【在 o**2 的大作中提到】 : 楼主,你的问题解决了没有?
|
o**2 发帖数: 168 | 21 我觉得你这里有几个相关但独立的问题:
1,performance问题;
2,http protocol细节问题;
3,Java对http protocol的支持及其实现细节问题;
4,其他libraries对http protocol的支持及其实现细节问题。
我个人认为你最好把第二个问题弄清楚,这样就算performance问题最终解决不了,你
起码也学到了新的知识,对不对?
要弄清楚http的细节,特别是你的server特定的情况下的细节,你应该用socket来编测
试程序,这样你才能知道到底哪些bytes去了server,和哪些bytes从server来。这应该
不难,你用proxy软件就可以看到你的现有软件的http内容,然后用socket来发这些内
容。再根据http spec,修改或添加内容,并试验你的多request方案。
【在 D***0 的大作中提到】 : 还在试验,现在还不知道是不是因为第三方的performance的原因,还是我的写法有问 : 题,还是java本身的performance的问题,谢谢关心
|
D***0 发帖数: 138 | 22 其实发送和接收的都没问题,就是速度不能达到要求,用wireshark看看和server的
communication应该有帮助。谢谢指点,我会从这几方面查的。
【在 o**2 的大作中提到】 : 我觉得你这里有几个相关但独立的问题: : 1,performance问题; : 2,http protocol细节问题; : 3,Java对http protocol的支持及其实现细节问题; : 4,其他libraries对http protocol的支持及其实现细节问题。 : 我个人认为你最好把第二个问题弄清楚,这样就算performance问题最终解决不了,你 : 起码也学到了新的知识,对不对? : 要弄清楚http的细节,特别是你的server特定的情况下的细节,你应该用socket来编测 : 试程序,这样你才能知道到底哪些bytes去了server,和哪些bytes从server来。这应该 : 不难,你用proxy软件就可以看到你的现有软件的http内容,然后用socket来发这些内
|