View Javadoc
1   /*
2    * Copyright 2016 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;
17  
18  import io.netty.microbench.util.AbstractMicrobenchmark;
19  import io.netty.util.internal.RecyclableArrayList;
20  import org.openjdk.jmh.annotations.Benchmark;
21  import org.openjdk.jmh.annotations.Param;
22  import org.openjdk.jmh.annotations.Scope;
23  import org.openjdk.jmh.annotations.State;
24  import org.openjdk.jmh.annotations.TearDown;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  @State(Scope.Benchmark)
30  public class CodecOutputListBenchmark extends AbstractMicrobenchmark {
31  
32      private static final Object ELEMENT = new Object();
33      private CodecOutputList codecOutputList;
34      private RecyclableArrayList recycleableArrayList;
35      private List<Object> arrayList;
36  
37      @Param({ "1", "4" })
38      public int elements;
39  
40      @TearDown
41      public void destroy() {
42          codecOutputList.recycle();
43          recycleableArrayList.recycle();
44      }
45  
46      @Benchmark
47      public void codecOutList() {
48          codecOutputList = CodecOutputList.newInstance();
49          benchmarkAddAndClear(codecOutputList, elements);
50      }
51  
52      @Benchmark
53      public void recyclableArrayList() {
54          recycleableArrayList = RecyclableArrayList.newInstance(16);
55          benchmarkAddAndClear(recycleableArrayList, elements);
56      }
57  
58      @Benchmark
59      public void arrayList() {
60          arrayList = new ArrayList<Object>(16);
61          benchmarkAddAndClear(arrayList, elements);
62      }
63  
64      private static void benchmarkAddAndClear(List<Object> list, int elements) {
65          for (int i = 0; i < elements; i++) {
66              list.add(ELEMENT);
67          }
68          list.clear();
69      }
70  }