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 org.openjdk.jmh.annotations.Benchmark;
19  import org.openjdk.jmh.annotations.Measurement;
20  import org.openjdk.jmh.annotations.Param;
21  import org.openjdk.jmh.annotations.Setup;
22  import org.openjdk.jmh.annotations.TearDown;
23  import org.openjdk.jmh.annotations.Warmup;
24  
25  import io.netty.microbench.util.AbstractMicrobenchmark;
26  
27  import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
28  import static io.netty.buffer.Unpooled.wrappedBuffer;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Random;
33  import java.util.concurrent.TimeUnit;
34  
35  @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
36  @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
37  public class CompositeByteBufRandomAccessBenchmark extends AbstractMicrobenchmark {
38  
39      public enum ByteBufType {
40          SMALL_CHUNKS {
41              @Override
42              ByteBuf newBuffer(int length) {
43                  return newBufferSmallChunks(length);
44              }
45          },
46          LARGE_CHUNKS {
47              @Override
48              ByteBuf newBuffer(int length) {
49                  return newBufferLargeChunks(length);
50              }
51          };
52          abstract ByteBuf newBuffer(int length);
53      }
54  
55      @Param({
56              "64",
57              "10240",
58              "1024000",
59      }) // ({ "64", "1024", "10240", "102400", "1024000" })
60      public int size;
61  
62      @Param
63      public ByteBufType bufferType;
64  
65      private ByteBuf buffer;
66      private Random random;
67  
68      @Setup
69      public void setup() {
70          buffer = bufferType.newBuffer(size);
71          random = new Random(0L);
72      }
73  
74      @TearDown
75      public void teardown() {
76          buffer.release();
77      }
78  
79      @Benchmark
80      public long setGetLong() {
81          int i = random.nextInt(size - 8);
82          return buffer.setLong(i, 1).getLong(i);
83      }
84  
85      @Benchmark
86      public ByteBuf setLong() {
87          int i = random.nextInt(size - 8);
88          return buffer.setLong(i, 1);
89      }
90  
91      private static ByteBuf newBufferSmallChunks(int length) {
92  
93          List<ByteBuf> buffers = new ArrayList<ByteBuf>(((length + 1) / 45) * 19);
94          for (int i = 0; i < length + 45; i += 45) {
95              for (int j = 1; j <= 9; j++) {
96                  buffers.add(EMPTY_BUFFER);
97                  buffers.add(wrappedBuffer(new byte[j]));
98              }
99              buffers.add(EMPTY_BUFFER);
100         }
101 
102         ByteBuf buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[0]));
103 
104         // Truncate to the requested capacity.
105         return buffer.capacity(length).writerIndex(0);
106     }
107 
108     private static ByteBuf newBufferLargeChunks(int length) {
109 
110         List<ByteBuf> buffers = new ArrayList<ByteBuf>((length + 1) / 512);
111         for (int i = 0; i < length + 1536; i += 1536) {
112             buffers.add(wrappedBuffer(new byte[512]));
113             buffers.add(EMPTY_BUFFER);
114             buffers.add(wrappedBuffer(new byte[1024]));
115         }
116 
117         ByteBuf buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[0]));
118 
119         // Truncate to the requested capacity.
120         return buffer.capacity(length).writerIndex(0);
121     }
122 }