View Javadoc
1   /*
2    * Copyright 2018 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.buffer;
17  
18  import io.netty.microbench.util.AbstractMicrobenchmark;
19  import org.openjdk.jmh.annotations.Benchmark;
20  import org.openjdk.jmh.annotations.Measurement;
21  import org.openjdk.jmh.annotations.Param;
22  import org.openjdk.jmh.annotations.Setup;
23  import org.openjdk.jmh.annotations.TearDown;
24  import org.openjdk.jmh.annotations.Warmup;
25  
26  import java.nio.charset.Charset;
27  import java.util.Arrays;
28  import java.util.concurrent.TimeUnit;
29  
30  @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
31  @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
32  public class AbstractByteBufGetCharSequenceBenchmark extends AbstractMicrobenchmark {
33  
34      public enum ByteBufType {
35          DIRECT {
36              @Override
37              ByteBuf newBuffer(byte[] bytes, int length) {
38                  ByteBuf buffer = Unpooled.directBuffer(length);
39                  buffer.writeBytes(bytes, 0, length);
40                  return buffer;
41              }
42          },
43          HEAP_OFFSET {
44              @Override
45              ByteBuf newBuffer(byte[] bytes, int length) {
46                  return Unpooled.wrappedBuffer(bytes, 1, length);
47              }
48          },
49          HEAP {
50              @Override
51              ByteBuf newBuffer(byte[] bytes, int length) {
52                  return Unpooled.wrappedBuffer(bytes, 0, length);
53              }
54          },
55          COMPOSITE {
56              @Override
57              ByteBuf newBuffer(byte[] bytes, int length) {
58                  CompositeByteBuf buffer = Unpooled.compositeBuffer();
59                  int offset = 0;
60                  // 8 buffers per composite.
61                  int capacity = length / 8;
62  
63                  while (length > 0) {
64                      buffer.addComponent(true, Unpooled.wrappedBuffer(bytes, offset, Math.min(length, capacity)));
65                      length -= capacity;
66                      offset += capacity;
67                  }
68                  return buffer;
69              }
70          };
71          abstract ByteBuf newBuffer(byte[] bytes, int length);
72      }
73  
74      @Param({ "8", "64", "1024", "10240", "1073741824" })
75      public int size;
76  
77      @Param({ "US-ASCII", "ISO_8859_1" })
78      public String charsetName;
79  
80      @Param
81      public ByteBufType bufferType;
82  
83      private ByteBuf buffer;
84      private Charset charset;
85  
86      @Override
87      protected String[] jvmArgs() {
88          // Ensure we minimize the GC overhead by sizing the heap big enough.
89          return new String[] { "-XX:MaxDirectMemorySize=2g", "-Xmx8g", "-Xms8g", "-Xmn6g" };
90      }
91  
92      @Setup
93      public void setup() {
94          byte[] bytes = new byte[size + 2];
95          Arrays.fill(bytes, (byte) 'a');
96  
97          // Use an offset to not allow any optimizations because we use the exact passed in byte[] for heap buffers.
98          buffer = bufferType.newBuffer(bytes, size);
99          charset = Charset.forName(charsetName);
100     }
101 
102     @TearDown
103     public void teardown() {
104         buffer.release();
105     }
106 
107     @Benchmark
108     public int getCharSequence() {
109         return traverse(buffer.getCharSequence(buffer.readerIndex(), size, charset));
110     }
111 
112     @Benchmark
113     public int getCharSequenceOld() {
114         return traverse(buffer.toString(buffer.readerIndex(), size, charset));
115     }
116 
117     private static int traverse(CharSequence cs) {
118         int i = 0, len = cs.length();
119         while (i < len && cs.charAt(i++) != 0) {
120             // ensure result is "used"
121         }
122         return i;
123     }
124 }