GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Restore CompositeByteBuf sequential benchmark · netty/netty@fe2f04f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • microbench/src/main/java/io/netty/buffer
    • CompositeByteBufSequentialBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎microbench/src/main/java/io/netty/buffer/CompositeByteBufSequentialBenchmark.java‎

Copy file name to clipboardExpand all lines: microbench/src/main/java/io/netty/buffer/CompositeByteBufSequentialBenchmark.java
+73-1Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openjdk.jmh.annotations.Warmup;
2727

2828
import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
29+
import static io.netty.buffer.Unpooled.directBuffer;
2930
import static io.netty.buffer.Unpooled.wrappedBuffer;
3031

3132
import java.util.ArrayList;
@@ -48,6 +49,18 @@ ByteBuf newBuffer(int length) {
4849
ByteBuf newBuffer(int length) {
4950
return newBufferLargeChunks(length);
5051
}
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+
}
5164
};
5265
abstract ByteBuf newBuffer(int length);
5366
}
@@ -70,6 +83,8 @@ ByteBuf newBuffer(int length) {
7083
@Setup
7184
public void setup() {
7285
buffer = bufferType.newBuffer(size);
86+
// Pre-fill so readByte benchmarks have readable content
87+
buffer.writerIndex(buffer.capacity());
7388
}
7489

7590
@TearDown
@@ -80,7 +95,7 @@ public void teardown() {
8095
private static final ByteProcessor TEST_PROCESSOR = new ByteProcessor() {
8196
@Override
8297
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
8499
}
85100
};
86101

@@ -91,6 +106,25 @@ public int forEachByte() {
91106
return buffer.forEachByteDesc(TEST_PROCESSOR);
92107
}
93108

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+
94128
@Benchmark
95129
public int sequentialWriteAndRead() {
96130
buffer.clear();
@@ -136,4 +170,42 @@ private static ByteBuf newBufferLargeChunks(int length) {
136170
// Truncate to the requested capacity.
137171
return buffer.capacity(length).writerIndex(0);
138172
}
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+
}
139211
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.