E-Mail: basecn@163.com
首发地址:http://blog.csdn.net/basecn/archive/2010/10/12/5935289.aspx
经过两天的测试发现,影响MINA网络性能的一个因素是她的ReceiveBufferSize。如果没有设置过,那么这个值默认是64K。
当发送小数据量时,这个参数没什么影响。如果发送大数据量,几百K或几兆时,通过观察ProtocolDecoder的doDecode方法,就会发现问题。
回过头先来看看doDecode方法,要求用户实现并返回boolean值。参考MINA的example可知,根据业务定义,当接收到的数据达不到预期时,应该返回false,这时MINA会尝试再次接收。
每次接收的量是多少呢?默认是64K,当设置receiveBufferSize大于64K时,新的receiveBufferSize才会生效,否则仍然使用64K做为接收缓冲。
OK,这样就可以知道,需要设置更大的receieBufferSize来增加接收缓存,以便一次性的接收更多的数据。
结果?还是不行!
最后找到的setReceiveBufferSize方法文档,这个方法是java.net.Socket的。MINA依赖的最底层的实现!
里面提到可以设置更大的值来提高大数据量的网络I/O性能,以减小往复读取的数据块。
在远端连接时,这个值也用来设置TCP接收窗口。
通常,窗口值可以在socket连接后的任何时间设置。然而,如果接收窗口大于64K时,远端需要在socket连接前设置这个值。
要注意下面两点:
1、 从ServerSocket接收socket时,要在ServerSocket绑定地址前调用 ServerSocket.setReceiveBufferSize(int)
2、 对于客户端,setReceiveBufferSize()要在连接前设置。
setReceiveBufferSize
public void setReceiveBufferSize(int size) throws SocketException
Sets the SO_RCVBUF option to the specified value for this Socket. The SO_RCVBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.
Increasing the receive buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data.
Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should callgetReceiveBufferSize().
The value of SO_RCVBUF is also used to set the TCP receive window that is advertized to the remote peer. Generally, the window size can be modified at any time when a socket is connected. However, if a receive window larger than 64K is required then this must be requested before the socket is connected to the remote peer. There are two cases to be aware of:
1. For sockets accepted from a ServerSocket, this must be done by calling ServerSocket.setReceiveBufferSize(int) before the ServerSocket is bound to a local address.
2. For client sockets, setReceiveBufferSize() must be called before connecting the socket to its remote peer.