This the the microbenchmark code I'm using:
import java.nio.ByteBuffer;
|
| import org.jboss.netty.buffer.ChannelBuffer;
| import org.jboss.netty.buffer.ChannelBuffers;
|
| public class Test {
| public static void main(String[] args) throws Exception {
| long startTime, endTime;
|
| ChannelBuffer buf = ChannelBuffers.dynamicBuffer(1048576);
| byte[] data = new byte[256];
|
| startTime = System.nanoTime();
| for (int j = 0; j < 100; j ++) {
| buf.clear();
| for (int i = 0; i < 1048576 / data.length; i ++) {
| buf.writeBytes(data);
| }
| }
| endTime = System.nanoTime();
|
| System.out.println(endTime - startTime);
|
| startTime = System.nanoTime();
| for (int j = 0; j < 100; j ++) {
| buf.clear();
| for (int i = 0; i < 1048576 / 2; i ++) {
| buf.writeShort((short) 0);
| }
| }
| endTime = System.nanoTime();
|
| System.out.println(endTime - startTime);
|
| ByteBuffer buf2 = ByteBuffer.allocate(1048576);
| startTime = System.nanoTime();
| for (int j = 0; j < 100; j ++) {
| buf2.clear();
| for (int i = 0; i < 1048576 / 2; i ++) {
| buf2.putShort((short) 0);
| }
| }
| endTime = System.nanoTime();
|
| System.out.println(endTime - startTime);
|
| ChannelBuffer buf3 = ChannelBuffers.buffer(1048576);
| startTime = System.nanoTime();
| for (int j = 0; j < 100; j ++) {
| buf3.clear();
| for (int i = 0; i < 1048576 / 2; i ++) {
| buf3.writeShort((short) 0);
| }
| }
| endTime = System.nanoTime();
|
| System.out.println(endTime - startTime);
| }
| }
The following is my result:
41097167
| 348230930
| 444482977
| 73107347
It seems like the boundary checkers in Netty's DynamicChannelBuffer and ByteBuffer are
slowing down the put operation. DynamicChannelBuffer uses a byte array rather than a
ByteBuffer as its internal data store, and that's why DynamicChannelBuffer does not
perform worse than ByteBuffer. It's interesting that ByteBuffer performs even worse
than DynamicChannelBuffer which dynamically increases its capacity on demand, while
ByteBuffer does nothing much in this test case.
If a non-dynamic ChannelBuffer, just a wrapper of a byte array, is used, no boundary check
is done, and therefore it performs pretty well and the only overhead left seems like
increasing the buffer position.
I think it is difficult to remove the existing boundary checker in DynamicChannelBuffer
because it can't expand itself without it. I would suggest you to use non-dynamic
ChannelBuffer whenever possible if the length of the buffer is known.
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4211774#...
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&a...