1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }