我使用 HttpURLConnection
按顺序向服务器 HttpServer
发出多个短请求。我期望的最大吞吐量是多少?我无法获得超过 25 条记录/秒的数据。
我需要每秒至少 5000 条记录。这是使用 HttpURLConnection 的正确方法吗?
下面是我的客户端代码:
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();
}
}
谢谢你帮助我。
最佳答案
在此方法中,使用一台计算机,您不会在一秒钟内打开 5000 个连接、发送数据并关闭所有 5000 个连接。
如果您能够在客户端上执行此操作,您可能会杀死服务器计算机;大多数超过 100 q/s 的网络应用程序主要提供缓存数据。
关于java - HttpURLConnection 最大吞吐量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371601/