开发者

Decent buffer size for file reading then sending out packets

I have a little web server application which reads X bytes from a file stream then outputs those bytes to an open TCP stream.

I played around with it a little bit and, for example, having a buffer size of 1024 then bumping it up 5120 didn't see much improvement. I also tried making it with a size of 1, to which I saw a huge speed decrea开发者_高级运维se.

I know I'm going to be limited by the read speed of the medium, and also by the network, so I'm curious about how to set this number. Is there a certain number I can set this to which will be optimal in all situations?


Is there a certain number I can set this to which will be optimal in all situations?

No, because it depends on the factors you mentioned, as well as on the buffering in your I/O library and in the operating system. Using some value that is a multiple of the disk block size, memory page size, and/or network packet size (don't forget the protocol headers!) is generally a wise move, though.

If speed is essential, then don't copy the blocks though memory at all if you can avoid it. Linux, for example, has the sendfile system call for this.


There isn't a certain number. But running a lot of test with different buffer dimension and perhaps using a network simulation tool to simulate different network conditions, could help a lot in selecting different suitable buffer dimension. I played this way while optimizing a VoIP client and it helped me a lot.


The more important factor is the socket send buffer size. Set this to at least 32k.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜