|
1 /* |
|
2 * Copyright (c) 2014 Oracle and/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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package org.openjdk.bench.java.lang; |
|
26 |
|
27 import org.openjdk.jmh.annotations.Benchmark; |
|
28 import org.openjdk.jmh.annotations.BenchmarkMode; |
|
29 import org.openjdk.jmh.annotations.Mode; |
|
30 import org.openjdk.jmh.annotations.OperationsPerInvocation; |
|
31 import org.openjdk.jmh.annotations.OutputTimeUnit; |
|
32 import org.openjdk.jmh.annotations.Scope; |
|
33 import org.openjdk.jmh.annotations.Setup; |
|
34 import org.openjdk.jmh.annotations.State; |
|
35 |
|
36 import java.util.concurrent.TimeUnit; |
|
37 |
|
38 /** |
|
39 * Benchmark measuring System.arraycopy in different ways. |
|
40 */ |
|
41 @BenchmarkMode(Mode.AverageTime) |
|
42 @OutputTimeUnit(TimeUnit.NANOSECONDS) |
|
43 @State(Scope.Thread) |
|
44 public class ArrayCopy { |
|
45 |
|
46 private static final byte[] TEST_BYTES = "HTTP/1.0".getBytes(); |
|
47 private static final char[] TEST_CHARS = new char[46]; |
|
48 private static final Object[] TEST_OBJECTS = new Object[200]; // Uses a minimum of 160 internal positions for internal copying |
|
49 |
|
50 // a length which the compiler cannot prove is a constant |
|
51 public static int nonConstCharLength = TEST_CHARS.length; |
|
52 public static int nonConstByteLength = TEST_BYTES.length; |
|
53 public static int nonConstObjectLength = TEST_OBJECTS.length; |
|
54 |
|
55 // Use this array to copy objects in. |
|
56 public char[] dummyCharArray = new char[TEST_CHARS.length]; |
|
57 public byte[] dummyByteArray = new byte[TEST_BYTES.length]; |
|
58 public Object[] dummyObjectArray = new Object[TEST_OBJECTS.length]; |
|
59 |
|
60 @Setup |
|
61 public void setup() { |
|
62 for (int i = 0; i < TEST_OBJECTS.length; i++) { |
|
63 TEST_OBJECTS[i] = new Object(); |
|
64 dummyObjectArray[i] = new Object(); |
|
65 } |
|
66 } |
|
67 |
|
68 /** |
|
69 * This test case do the same work as testArrayCopy. We should make sure |
|
70 * testArrayCopy is equally fast or better. Compare the two and you measure |
|
71 * the system call versus explicit copy for-loop. |
|
72 */ |
|
73 @Benchmark |
|
74 public void copyLoop() { |
|
75 for (int j = 0; j < dummyByteArray.length; j++) { |
|
76 dummyByteArray[j] = TEST_BYTES[j]; |
|
77 } |
|
78 } |
|
79 |
|
80 /** |
|
81 * Test that we can optimize away the code since it should not have any side |
|
82 * effects |
|
83 */ |
|
84 @Benchmark |
|
85 public void copyLoopLocalArray() { |
|
86 byte[] localDummyByteArray = new byte[TEST_BYTES.length]; |
|
87 for (int j = 0; j < localDummyByteArray.length; j++) { |
|
88 localDummyByteArray[j] = TEST_BYTES[j]; |
|
89 } |
|
90 } |
|
91 |
|
92 /** |
|
93 * This test case do the same work as testArrayCopy. We should make sure |
|
94 * testArrayCopy is equally fast or better. Compare the two and you measure |
|
95 * the system call versus explicit copy for-loop. |
|
96 * <p/> |
|
97 * Uses non-provable constant length. |
|
98 */ |
|
99 @Benchmark |
|
100 public void copyLoopNonConst() { |
|
101 for (int i = 0; i < nonConstByteLength; i++) { |
|
102 dummyByteArray[i] = TEST_BYTES[i]; |
|
103 } |
|
104 } |
|
105 |
|
106 /** |
|
107 * This test case do the same work as testCopyLoop. We should make sure |
|
108 * testArrayCopy is equally fast or better. Compare the two and you measure |
|
109 * the system call versus explicit copy for-loop. |
|
110 */ |
|
111 @Benchmark |
|
112 public void arrayCopy() { |
|
113 System.arraycopy(TEST_BYTES, 0, dummyByteArray, 0, dummyByteArray.length); |
|
114 } |
|
115 |
|
116 /** |
|
117 * Test that we can optimize away the code since it should not have any side |
|
118 * effects |
|
119 */ |
|
120 @Benchmark |
|
121 public void arrayCopyLocalArray() { |
|
122 byte[] localDummyByteArray = new byte[TEST_BYTES.length]; |
|
123 System.arraycopy(TEST_BYTES, 0, localDummyByteArray, 0, localDummyByteArray.length); |
|
124 } |
|
125 |
|
126 /** |
|
127 * This test case do the same work as testCopyLoop. We should make sure |
|
128 * testArrayCopy is equally fast or better. Compare the two and you measure |
|
129 * the system call versus explicit copy for-loop. |
|
130 * <p/> |
|
131 * Uses non-provable constant length. |
|
132 */ |
|
133 @Benchmark |
|
134 public void arrayCopyNonConst() { |
|
135 System.arraycopy(TEST_BYTES, 0, dummyByteArray, 0, nonConstByteLength); |
|
136 } |
|
137 |
|
138 @Benchmark |
|
139 public void arrayCopyChar() { |
|
140 System.arraycopy(TEST_CHARS, 0, dummyCharArray, 0, dummyCharArray.length); |
|
141 } |
|
142 |
|
143 @Benchmark |
|
144 public void arrayCopyCharNonConst() { |
|
145 System.arraycopy(TEST_CHARS, 0, dummyCharArray, 0, nonConstCharLength); |
|
146 } |
|
147 |
|
148 @Benchmark |
|
149 public void arrayCopyObject() { |
|
150 System.arraycopy(TEST_OBJECTS, 0, dummyObjectArray, 0, dummyObjectArray.length); |
|
151 } |
|
152 |
|
153 @Benchmark |
|
154 public void arrayCopyObjectNonConst() { |
|
155 System.arraycopy(TEST_OBJECTS, 0, dummyObjectArray, 0, nonConstObjectLength); |
|
156 } |
|
157 |
|
158 /** |
|
159 * This test copies inside a object array, that is same source array as dest |
|
160 * array. Copies backwards in the array. |
|
161 */ |
|
162 @Benchmark |
|
163 @OperationsPerInvocation(40) |
|
164 public void arrayCopyObjectSameArraysBackward() { |
|
165 for (int i = 0; i < 40; i++) { |
|
166 System.arraycopy(dummyObjectArray, i, dummyObjectArray, i + 40, 80); |
|
167 } |
|
168 } |
|
169 |
|
170 /** |
|
171 * This test copies inside a object array, that is same source array as dest |
|
172 * array. Copies forward in the array. There is a special version for this |
|
173 * in JRockit. |
|
174 */ |
|
175 @Benchmark |
|
176 @OperationsPerInvocation(40) |
|
177 public void arrayCopyObjectSameArraysForward() { |
|
178 for (int i = 0; i < 40; i++) { |
|
179 System.arraycopy(dummyObjectArray, i + 40, dummyObjectArray, i, 80); |
|
180 } |
|
181 } |
|
182 } |