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.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  }