1 /*
2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package org.openjdk.bench.java.util.concurrent;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.concurrent.CopyOnWriteArrayList;
34 import java.util.concurrent.TimeUnit;
35
36 import org.openjdk.jmh.annotations.Benchmark;
37 import org.openjdk.jmh.annotations.BenchmarkMode;
38 import org.openjdk.jmh.annotations.Fork;
39 import org.openjdk.jmh.annotations.Measurement;
40 import org.openjdk.jmh.annotations.Mode;
41 import org.openjdk.jmh.annotations.OutputTimeUnit;
42 import org.openjdk.jmh.annotations.Scope;
43 import org.openjdk.jmh.annotations.State;
44 import org.openjdk.jmh.annotations.Warmup;
45
46 @BenchmarkMode(Mode.AverageTime)
47 @State(Scope.Benchmark)
48 @OutputTimeUnit(TimeUnit.NANOSECONDS)
49 @Fork(1)
50 @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
51 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
52 public class CopyOnWriteArrayListBenchmark {
53
54 private static byte[] getSerializedBytes(CopyOnWriteArrayList<?> list) throws IOException {
55 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
56 ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
57 objectOut.writeObject(list);
58
59 objectOut.close();
60 return bytesOut.toByteArray();
61 }
62
63 private Collection<Object> emptyCollection = new ArrayList<>();
64 private Object[] emptyArray = new Object[0];
65
66 private Collection<Object> oneItemCollection = Arrays.asList("");
67 private Object[] oneItemArray = new Object[] { "" };
68
69 private CopyOnWriteArrayList<?> emptyInstance = new CopyOnWriteArrayList<>();
70 private CopyOnWriteArrayList<?> oneItemInstance = new CopyOnWriteArrayList<>(oneItemArray);
71
72 private byte[] emptyInstanceBytes;
73 private byte[] oneInstanceBytes;
74
75 public CopyOnWriteArrayListBenchmark() {
76 try {
77 emptyInstanceBytes = getSerializedBytes(emptyInstance);
78 oneInstanceBytes = getSerializedBytes(oneItemInstance);
79 } catch (IOException e) {
80 throw new RuntimeException(e);
81 }
82 }
83
84 @Benchmark
85 public void clear() {
86 // have to create a new instance on each execution
87 ((CopyOnWriteArrayList<?>) oneItemInstance.clone()).clear();
88 }
89
90 @Benchmark
91 public void clearEmpty() {
92 emptyInstance.clear();
93 }
94
95 @Benchmark
96 public CopyOnWriteArrayList<?> createInstanceArray() {
97 return new CopyOnWriteArrayList<>(oneItemArray);
98 }
99
100 @Benchmark
101 public CopyOnWriteArrayList<?> createInstanceArrayEmpty() {
102 return new CopyOnWriteArrayList<>(emptyArray);
103 }
104
105 @Benchmark
106 public CopyOnWriteArrayList<?> createInstanceCollection() {
107 return new CopyOnWriteArrayList<>(oneItemCollection);
108 }
109
110 @Benchmark
111 public CopyOnWriteArrayList<?> createInstanceCollectionEmpty() {
112 return new CopyOnWriteArrayList<>(emptyCollection);
113 }
114
115 @Benchmark
116 public CopyOnWriteArrayList<?> createInstanceDefault() {
117 return new CopyOnWriteArrayList<Object>();
118 }
119
120 @Benchmark
121 public CopyOnWriteArrayList<?> readInstance() throws IOException, ClassNotFoundException {
122 try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(oneInstanceBytes))) {
123 return (CopyOnWriteArrayList<?>) objIn.readObject();
124 }
125 }
126
127 @Benchmark
128 public CopyOnWriteArrayList<?> readInstanceEmpty() throws IOException, ClassNotFoundException {
129 try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(emptyInstanceBytes))) {
130 return (CopyOnWriteArrayList<?>) objIn.readObject();
131 }
132 }
133
134 @Benchmark
135 public CopyOnWriteArrayList<?> removeObjectLastRemaining() {
136 CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
137 list.add("");
138 list.remove("");
139 return list;
140 }
141
142 @Benchmark
143 public CopyOnWriteArrayList<?> removeIndexLastRemaining() {
144 CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
145 list.add("");
146 list.remove(0);
147 return list;
148 }
149 @Benchmark
150 public CopyOnWriteArrayList<?> removeObject() {
151 CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
152 list.add("");
153 list.add("a");
154 list.remove("");
155 return list;
156 }
157
158 @Benchmark
159 public CopyOnWriteArrayList<?> remove() {
160 CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
161 list.add("");
162 list.add("a");
163 list.remove(0);
164 return list;
165 }
166 }