View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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  }