1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufUtil;
20 import io.netty.buffer.Unpooled;
21 import io.netty.microbench.util.AbstractMicrobenchmark;
22 import org.openjdk.jmh.annotations.Benchmark;
23 import org.openjdk.jmh.annotations.Measurement;
24 import org.openjdk.jmh.annotations.OutputTimeUnit;
25 import org.openjdk.jmh.annotations.Threads;
26 import org.openjdk.jmh.annotations.Warmup;
27
28 import java.util.concurrent.TimeUnit;
29
30 import static io.netty.handler.codec.http.HttpConstants.*;
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 int 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 ByteBuf buf = Unpooled.directBuffer(16);
43
44 @Benchmark
45 public ByteBuf shortInt() {
46 return ByteBufUtil.writeShortBE(buf, CRLF_SHORT).resetWriterIndex();
47 }
48
49 @Benchmark
50 public ByteBuf mediumInt() {
51 return ByteBufUtil.writeMediumBE(buf, ZERO_CRLF_MEDIUM).resetWriterIndex();
52 }
53
54 @Benchmark
55 public ByteBuf byteArray2() {
56 return buf.writeBytes(CRLF).resetWriterIndex();
57 }
58
59 @Benchmark
60 public ByteBuf byteArray3() {
61 return buf.writeBytes(ZERO_CRLF).resetWriterIndex();
62 }
63
64 @Benchmark
65 public ByteBuf chainedBytes2() {
66 return buf.writeByte(CR).writeByte(LF).resetWriterIndex();
67 }
68
69 @Benchmark
70 public ByteBuf chainedBytes3() {
71 return buf.writeByte('0').writeByte(CR).writeByte(LF).resetWriterIndex();
72 }
73 }