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     @SuppressWarnings("initialization")
 71     private CopyOnWriteArrayList<?> oneItemInstance = new CopyOnWriteArrayList<>(oneItemArray);
 72 
 73     private byte[] emptyInstanceBytes;
 74     private byte[] oneInstanceBytes;
 75 
 76     @SuppressWarnings("initialization")
 77     public CopyOnWriteArrayListBenchmark() {
 78         try {
 79             emptyInstanceBytes = getSerializedBytes(emptyInstance);
 80             oneInstanceBytes = getSerializedBytes(oneItemInstance);
 81         } catch (IOException e) {
 82             throw new RuntimeException(e);
 83         }
 84     }
 85 
 86     @Benchmark
 87     public void clear() {
 88         // have to create a new instance on each execution
 89         ((CopyOnWriteArrayList<?>) oneItemInstance.clone()).clear();
 90     }
 91 
 92     @Benchmark
 93     public void clearEmpty() {
 94         emptyInstance.clear();
 95     }
 96 
 97     @Benchmark
 98     public CopyOnWriteArrayList<?> createInstanceArray() {
 99         return new CopyOnWriteArrayList<>(oneItemArray);
100     }
101 
102     @Benchmark
103     public CopyOnWriteArrayList<?> createInstanceArrayEmpty() {
104         return new CopyOnWriteArrayList<>(emptyArray);
105     }
106 
107     @Benchmark
108     public CopyOnWriteArrayList<?> createInstanceCollection() {
109         return new CopyOnWriteArrayList<>(oneItemCollection);
110     }
111 
112     @Benchmark
113     public CopyOnWriteArrayList<?> createInstanceCollectionEmpty() {
114         return new CopyOnWriteArrayList<>(emptyCollection);
115     }
116 
117     @Benchmark
118     public CopyOnWriteArrayList<?> createInstanceDefault() {
119         return new CopyOnWriteArrayList<Object>();
120     }
121 
122     @Benchmark
123     public CopyOnWriteArrayList<?> readInstance() throws IOException, ClassNotFoundException {
124         try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(oneInstanceBytes))) {
125             return (CopyOnWriteArrayList<?>) objIn.readObject();
126         }
127     }
128 
129     @Benchmark
130     public CopyOnWriteArrayList<?> readInstanceEmpty() throws IOException, ClassNotFoundException {
131         try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(emptyInstanceBytes))) {
132             return (CopyOnWriteArrayList<?>) objIn.readObject();
133         }
134     }
135 
136     @Benchmark
137     public CopyOnWriteArrayList<?> removeObjectLastRemaining() {
138         CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
139         list.add("");
140         list.remove("");
141         return list;
142     }
143 
144     @Benchmark
145     public CopyOnWriteArrayList<?> removeIndexLastRemaining() {
146         CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
147         list.add("");
148         list.remove(0);
149         return list;
150     }
151     @Benchmark
152     public CopyOnWriteArrayList<?> removeObject() {
153         CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
154         list.add("");
155         list.add("a");
156         list.remove("");
157         return list;
158     }
159 
160     @Benchmark
161     public CopyOnWriteArrayList<?> remove() {
162         CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
163         list.add("");
164         list.add("a");
165         list.remove(0);
166         return list;
167     }
168 }