[英]HttpURLConnection Max Throughput
I was using the HttpURLConnection
to make multiple short requests in the sequence to a server HttpServer
. 我使用
HttpURLConnection
依次向服务器HttpServer
发出多个短请求。 What is the maximum throughput that I can expect? 我可以预期的最大吞吐量是多少? I am not able to get it upwards of 25 records/second.
我无法使其每秒超过25条记录。
I need to get it upwards of atleast 5000 records/sec. 我需要使它至少达到5000条记录/秒。 Is this a right approach to use
HttpURLConnection
? 这是使用
HttpURLConnection
的正确方法吗?
Below is my client code: 以下是我的客户代码:
public class TestGatewayUser {
public static void main( String[] args ) throws IOException {
byte[] bytes = TestGatewayUser.getDataAsBytes();
Date d1 = new Date();
for ( int i = 0; i < 10000; ++i ) {
URL url = new URL( "http://IP:PORT/fetchInfo" );
HttpURLConnection conn = ( HttpURLConnection ) url.openConnection();
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
byte[] bytes = TestGatewayUser.getDataAsBytes();
conn.setRequestProperty( "Content-Length", Integer.toString( bytes.length ) );
conn.setUseCaches( false );
conn.setDoInput( true );
conn.setDoOutput( true );
conn.connect();
OutputStream out = conn.getOutputStream();
out.write( bytes );
out.flush();
out.close();
int responseCode = conn.getResponseCode();
InputStream stream = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1];
while ( stream.read( b ) != -1 ) {
bos.write( b );
}
byte[] byteArray = bos.toByteArray();
stream.close();
if ( i % 200 == 0 ) {
System.out.println( 200.0 / ( new Date().getTime() - d1.getTime() ) * 1000 );
}
conn.disconnect();//**Should I use this or not? Java Doc says optional.**
}
Date d2 = new Date();
System.out.println( d2.getTime() - d1.getTime() );
}
private static byte[] getDataAsBytes() throws IOException {
StringBuffer buf = new StringBuffer();
buf.append( "1367249:2,4,5,31,32,35,59,68,77,389,532,558,353,30002,371" );
return buf.toString().getBytes();
}
}
Thanks for helping me. 谢谢你帮我
You're not going to get 5000 connections opened, data sent, and all 5000 connections closed in a second in this method, using one machine. 使用一台计算机,这种方法不会在一秒钟内打开5000个连接,发送数据并关闭所有5000个连接。
If you were able to do it on the client, you'd likely kill the server machine; 如果您能够在客户端上执行此操作,则可能会杀死服务器计算机。 most web apps that go over 100 q/s are serving mostly cached data.
大多数速度超过100 q / s的Web应用程序都在提供大部分缓存的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.