HttpURLConnection最大吞吐量

时间:2015-07-12 19:19:11

标签: java httpurlconnection httpserver

我使用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();
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

使用一台计算机,您不会在此方法中打开5000个连接,发送数据以及一秒钟内关闭所有5000个连接。

如果您能够在客户端上执行此操作,则可能会杀死服务器计算机;大多数超过100 q / s的网络应用程序主要提供缓存数据。