1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.http;
17
18 import io.netty5.buffer.api.Buffer;
19 import io.netty5.buffer.api.BufferAllocator;
20 import io.netty5.microbench.util.AbstractMicrobenchmark;
21 import org.openjdk.jmh.annotations.Benchmark;
22 import org.openjdk.jmh.annotations.Measurement;
23 import org.openjdk.jmh.annotations.OutputTimeUnit;
24 import org.openjdk.jmh.annotations.Threads;
25 import org.openjdk.jmh.annotations.Warmup;
26
27 import java.util.concurrent.TimeUnit;
28
29 import static io.netty5.handler.codec.http.HttpConstants.CR;
30 import static io.netty5.handler.codec.http.HttpConstants.LF;
31
32 @Threads(1)
33 @Warmup(iterations = 3)
34 @Measurement(iterations = 3)
35 @OutputTimeUnit(TimeUnit.MICROSECONDS)
36 public class WriteBytesVsShortOrMediumBenchmark extends AbstractMicrobenchmark {
37 private static final short CRLF_SHORT = (CR << 8) + LF;
38 private static final byte[] CRLF = { CR, LF };
39 private static final int ZERO_CRLF_MEDIUM = ('0' << 16) + (CR << 8) + LF;
40 private static final byte[] ZERO_CRLF = { '0', CR, LF };
41
42 private final Buffer buf = BufferAllocator.offHeapUnpooled().allocate(16);
43
44 @Benchmark
45 public Buffer shortInt() {
46 return buf.writeShort(CRLF_SHORT).writerOffset(0);
47 }
48
49 @Benchmark
50 public Buffer mediumInt() {
51 return buf.writeMedium(ZERO_CRLF_MEDIUM).writerOffset(0);
52 }
53
54 @Benchmark
55 public Buffer byteArray2() {
56 return buf.writeBytes(CRLF).writerOffset(0);
57 }
58
59 @Benchmark
60 public Buffer byteArray3() {
61 return buf.writeBytes(ZERO_CRLF).writerOffset(0);
62 }
63
64 @Benchmark
65 public Buffer chainedBytes2() {
66 return buf.writeByte(CR).writeByte(LF).writerOffset(0);
67 }
68
69 @Benchmark
70 public Buffer chainedBytes3() {
71 return buf.writeByte((byte) '0').writeByte(CR).writeByte(LF).writerOffset(0);
72 }
73 }