|
26 | 26 | import org.openjdk.jmh.annotations.Warmup; |
27 | 27 |
|
28 | 28 | import static io.netty.buffer.Unpooled.EMPTY_BUFFER; |
| 29 | +import static io.netty.buffer.Unpooled.directBuffer; |
29 | 30 | import static io.netty.buffer.Unpooled.wrappedBuffer; |
30 | 31 |
|
31 | 32 | import java.util.ArrayList; |
@@ -48,6 +49,18 @@ ByteBuf newBuffer(int length) { |
48 | 49 | ByteBuf newBuffer(int length) { |
49 | 50 | return newBufferLargeChunks(length); |
50 | 51 | } |
| 52 | + }, |
| 53 | + SMALL_CHUNKS_DIRECT { |
| 54 | + @Override |
| 55 | + ByteBuf newBuffer(int length) { |
| 56 | + return newBufferSmallChunksDirect(length); |
| 57 | + } |
| 58 | + }, |
| 59 | + LARGE_CHUNKS_DIRECT { |
| 60 | + @Override |
| 61 | + ByteBuf newBuffer(int length) { |
| 62 | + return newBufferLargeChunksDirect(length); |
| 63 | + } |
51 | 64 | }; |
52 | 65 | abstract ByteBuf newBuffer(int length); |
53 | 66 | } |
@@ -70,6 +83,8 @@ ByteBuf newBuffer(int length) { |
70 | 83 | @Setup |
71 | 84 | public void setup() { |
72 | 85 | buffer = bufferType.newBuffer(size); |
| 86 | + // Pre-fill so readByte benchmarks have readable content |
| 87 | + buffer.writerIndex(buffer.capacity()); |
73 | 88 | } |
74 | 89 |
|
75 | 90 | @TearDown |
@@ -80,7 +95,7 @@ public void teardown() { |
80 | 95 | private static final ByteProcessor TEST_PROCESSOR = new ByteProcessor() { |
81 | 96 | @Override |
82 | 97 | public boolean process(byte value) throws Exception { |
83 | | - return value == 'b'; // false |
| 98 | + return value != 'b'; // true for non-'b' bytes, so we scan all bytes |
84 | 99 | } |
85 | 100 | }; |
86 | 101 |
|
@@ -91,6 +106,25 @@ public int forEachByte() { |
91 | 106 | return buffer.forEachByteDesc(TEST_PROCESSOR); |
92 | 107 | } |
93 | 108 |
|
| 109 | + @Benchmark |
| 110 | + public int sequentialReadBytes() { |
| 111 | + buffer.readerIndex(0); |
| 112 | + int result = 0; |
| 113 | + for (int i = 0, l = buffer.readableBytes(); i < l; i++) { |
| 114 | + result += buffer.readByte(); |
| 115 | + } |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + @Benchmark |
| 120 | + public int sequentialGetBytes() { |
| 121 | + int result = 0; |
| 122 | + for (int i = 0, l = buffer.capacity(); i < l; i++) { |
| 123 | + result += buffer.getByte(i); |
| 124 | + } |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
94 | 128 | @Benchmark |
95 | 129 | public int sequentialWriteAndRead() { |
96 | 130 | buffer.clear(); |
@@ -136,4 +170,42 @@ private static ByteBuf newBufferLargeChunks(int length) { |
136 | 170 | // Truncate to the requested capacity. |
137 | 171 | return buffer.capacity(length).writerIndex(0); |
138 | 172 | } |
| 173 | + |
| 174 | + private static ByteBuf newDirectChunk(int size) { |
| 175 | + ByteBuf buf = directBuffer(size); |
| 176 | + buf.writerIndex(size); |
| 177 | + return buf; |
| 178 | + } |
| 179 | + |
| 180 | + private static ByteBuf newBufferSmallChunksDirect(int length) { |
| 181 | + |
| 182 | + List<ByteBuf> buffers = new ArrayList<ByteBuf>(((length + 1) / 45) * 19); |
| 183 | + for (int i = 0; i < length + 45; i += 45) { |
| 184 | + for (int j = 1; j <= 9; j++) { |
| 185 | + buffers.add(EMPTY_BUFFER); |
| 186 | + buffers.add(newDirectChunk(j)); |
| 187 | + } |
| 188 | + buffers.add(EMPTY_BUFFER); |
| 189 | + } |
| 190 | + |
| 191 | + ByteBuf buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[0])); |
| 192 | + |
| 193 | + // Truncate to the requested capacity. |
| 194 | + return buffer.capacity(length).writerIndex(0); |
| 195 | + } |
| 196 | + |
| 197 | + private static ByteBuf newBufferLargeChunksDirect(int length) { |
| 198 | + |
| 199 | + List<ByteBuf> buffers = new ArrayList<ByteBuf>((length + 1) / 512); |
| 200 | + for (int i = 0; i < length + 1536; i += 1536) { |
| 201 | + buffers.add(newDirectChunk(512)); |
| 202 | + buffers.add(EMPTY_BUFFER); |
| 203 | + buffers.add(newDirectChunk(1024)); |
| 204 | + } |
| 205 | + |
| 206 | + ByteBuf buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[0])); |
| 207 | + |
| 208 | + // Truncate to the requested capacity. |
| 209 | + return buffer.capacity(length).writerIndex(0); |
| 210 | + } |
139 | 211 | } |