ByteBufferBenchmark.java revision 5a7833b406bb2716b057d3ed923f22f1f86b2a20
1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package benchmarks.regression; 18 19import com.google.caliper.Param; 20import com.google.caliper.Runner; 21import com.google.caliper.SimpleBenchmark; 22 23import java.io.*; 24import java.nio.*; 25import java.nio.channels.*; 26import java.util.Arrays; 27import java.util.Collection; 28 29public class ByteBufferBenchmark extends SimpleBenchmark { 30 public enum MyByteOrder { 31 BIG(ByteOrder.BIG_ENDIAN), LITTLE(ByteOrder.LITTLE_ENDIAN); 32 final ByteOrder byteOrder; 33 MyByteOrder(ByteOrder byteOrder) { 34 this.byteOrder = byteOrder; 35 } 36 } 37 38 @Param private MyByteOrder byteOrder; 39 40 @Param({"true", "false"}) private boolean aligned; 41 42 enum MyBufferType { 43 DIRECT, HEAP, MAPPED; 44 } 45 @Param private MyBufferType bufferType; 46 47 public static ByteBuffer newBuffer(MyByteOrder byteOrder, boolean aligned, MyBufferType bufferType) throws IOException { 48 int size = aligned ? 8192 : 8192 + 8 + 1; 49 ByteBuffer result = null; 50 switch (bufferType) { 51 case DIRECT: 52 result = ByteBuffer.allocateDirect(size); 53 break; 54 case HEAP: 55 result = ByteBuffer.allocate(size); 56 break; 57 case MAPPED: 58 File tmpFile = new File("/sdcard/bm.tmp"); 59 if (new File("/tmp").isDirectory()) { 60 // We're running on the desktop. 61 tmpFile = File.createTempFile("MappedByteBufferTest", ".tmp"); 62 } 63 tmpFile.createNewFile(); 64 tmpFile.deleteOnExit(); 65 RandomAccessFile raf = new RandomAccessFile(tmpFile, "rw"); 66 raf.setLength(8192*8); 67 FileChannel fc = raf.getChannel(); 68 result = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()); 69 break; 70 } 71 result.order(byteOrder.byteOrder); 72 result.position(aligned ? 0 : 1); 73 return result; 74 } 75 76 // 77 // peeking 78 // 79 80 public void timeByteBuffer_getByte(int reps) throws Exception { 81 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 82 for (int rep = 0; rep < reps; ++rep) { 83 src.position(aligned ? 0 : 1); 84 for (int i = 0; i < 1024; ++i) { 85 src.get(); 86 } 87 } 88 } 89 90 public void timeByteBuffer_getByteArray(int reps) throws Exception { 91 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 92 byte[] dst = new byte[1024]; 93 for (int rep = 0; rep < reps; ++rep) { 94 for (int i = 0; i < 1024; ++i) { 95 src.position(aligned ? 0 : 1); 96 src.get(dst); 97 } 98 } 99 } 100 101 public void timeByteBuffer_getByte_indexed(int reps) throws Exception { 102 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 103 for (int rep = 0; rep < reps; ++rep) { 104 src.position(aligned ? 0 : 1); 105 for (int i = 0; i < 1024; ++i) { 106 src.get(i); 107 } 108 } 109 } 110 111 public void timeByteBuffer_getChar(int reps) throws Exception { 112 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 113 for (int rep = 0; rep < reps; ++rep) { 114 src.position(aligned ? 0 : 1); 115 for (int i = 0; i < 1024; ++i) { 116 src.getChar(); 117 } 118 } 119 } 120 121 public void timeCharBuffer_getCharArray(int reps) throws Exception { 122 CharBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asCharBuffer(); 123 char[] dst = new char[1024]; 124 for (int rep = 0; rep < reps; ++rep) { 125 for (int i = 0; i < 1024; ++i) { 126 src.position(0); 127 src.get(dst); 128 } 129 } 130 } 131 132 public void timeByteBuffer_getChar_indexed(int reps) throws Exception { 133 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 134 for (int rep = 0; rep < reps; ++rep) { 135 src.position(aligned ? 0 : 1); 136 for (int i = 0; i < 1024; ++i) { 137 src.getChar(i * 2); 138 } 139 } 140 } 141 142 public void timeByteBuffer_getDouble(int reps) throws Exception { 143 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 144 for (int rep = 0; rep < reps; ++rep) { 145 src.position(aligned ? 0 : 1); 146 for (int i = 0; i < 1024; ++i) { 147 src.getDouble(); 148 } 149 } 150 } 151 152 public void timeDoubleBuffer_getDoubleArray(int reps) throws Exception { 153 DoubleBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asDoubleBuffer(); 154 double[] dst = new double[1024]; 155 for (int rep = 0; rep < reps; ++rep) { 156 for (int i = 0; i < 1024; ++i) { 157 src.position(0); 158 src.get(dst); 159 } 160 } 161 } 162 163 public void timeByteBuffer_getFloat(int reps) throws Exception { 164 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 165 for (int rep = 0; rep < reps; ++rep) { 166 src.position(aligned ? 0 : 1); 167 for (int i = 0; i < 1024; ++i) { 168 src.getFloat(); 169 } 170 } 171 } 172 173 public void timeFloatBuffer_getFloatArray(int reps) throws Exception { 174 FloatBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asFloatBuffer(); 175 float[] dst = new float[1024]; 176 for (int rep = 0; rep < reps; ++rep) { 177 for (int i = 0; i < 1024; ++i) { 178 src.position(0); 179 src.get(dst); 180 } 181 } 182 } 183 184 public void timeByteBuffer_getInt(int reps) throws Exception { 185 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 186 for (int rep = 0; rep < reps; ++rep) { 187 src.position(aligned ? 0 : 1); 188 for (int i = 0; i < 1024; ++i) { 189 src.getInt(); 190 } 191 } 192 } 193 194 public void timeIntBuffer_getIntArray(int reps) throws Exception { 195 IntBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asIntBuffer(); 196 int[] dst = new int[1024]; 197 for (int rep = 0; rep < reps; ++rep) { 198 for (int i = 0; i < 1024; ++i) { 199 src.position(0); 200 src.get(dst); 201 } 202 } 203 } 204 205 public void timeByteBuffer_getLong(int reps) throws Exception { 206 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 207 for (int rep = 0; rep < reps; ++rep) { 208 src.position(aligned ? 0 : 1); 209 for (int i = 0; i < 1024; ++i) { 210 src.getLong(); 211 } 212 } 213 } 214 215 public void timeLongBuffer_getLongArray(int reps) throws Exception { 216 LongBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asLongBuffer(); 217 long[] dst = new long[1024]; 218 for (int rep = 0; rep < reps; ++rep) { 219 for (int i = 0; i < 1024; ++i) { 220 src.position(0); 221 src.get(dst); 222 } 223 } 224 } 225 226 public void timeByteBuffer_getShort(int reps) throws Exception { 227 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 228 for (int rep = 0; rep < reps; ++rep) { 229 src.position(aligned ? 0 : 1); 230 for (int i = 0; i < 1024; ++i) { 231 src.getShort(); 232 } 233 } 234 } 235 236 public void timeShortBuffer_getShortArray(int reps) throws Exception { 237 ShortBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asShortBuffer(); 238 short[] dst = new short[1024]; 239 for (int rep = 0; rep < reps; ++rep) { 240 for (int i = 0; i < 1024; ++i) { 241 src.position(0); 242 src.get(dst); 243 } 244 } 245 } 246 247 // 248 // poking 249 // 250 251 public void timeByteBuffer_putByte(int reps) throws Exception { 252 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 253 for (int rep = 0; rep < reps; ++rep) { 254 src.position(0); 255 for (int i = 0; i < 1024; ++i) { 256 src.put((byte) 0); 257 } 258 } 259 } 260 261 public void timeByteBuffer_putByteArray(int reps) throws Exception { 262 ByteBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 263 byte[] src = new byte[1024]; 264 for (int rep = 0; rep < reps; ++rep) { 265 for (int i = 0; i < 1024; ++i) { 266 dst.position(aligned ? 0 : 1); 267 dst.put(src); 268 } 269 } 270 } 271 272 public void timeByteBuffer_putChar(int reps) throws Exception { 273 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 274 for (int rep = 0; rep < reps; ++rep) { 275 src.position(aligned ? 0 : 1); 276 for (int i = 0; i < 1024; ++i) { 277 src.putChar(' '); 278 } 279 } 280 } 281 282 public void timeCharBuffer_putCharArray(int reps) throws Exception { 283 CharBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asCharBuffer(); 284 char[] src = new char[1024]; 285 for (int rep = 0; rep < reps; ++rep) { 286 for (int i = 0; i < 1024; ++i) { 287 dst.position(0); 288 dst.put(src); 289 } 290 } 291 } 292 293 public void timeByteBuffer_putDouble(int reps) throws Exception { 294 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 295 for (int rep = 0; rep < reps; ++rep) { 296 src.position(aligned ? 0 : 1); 297 for (int i = 0; i < 1024; ++i) { 298 src.putDouble(0.0); 299 } 300 } 301 } 302 303 public void timeDoubleBuffer_putDoubleArray(int reps) throws Exception { 304 DoubleBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asDoubleBuffer(); 305 double[] src = new double[1024]; 306 for (int rep = 0; rep < reps; ++rep) { 307 for (int i = 0; i < 1024; ++i) { 308 dst.position(0); 309 dst.put(src); 310 } 311 } 312 } 313 314 public void timeByteBuffer_putFloat(int reps) throws Exception { 315 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 316 for (int rep = 0; rep < reps; ++rep) { 317 src.position(aligned ? 0 : 1); 318 for (int i = 0; i < 1024; ++i) { 319 src.putFloat(0.0f); 320 } 321 } 322 } 323 324 public void timeFloatBuffer_putFloatArray(int reps) throws Exception { 325 FloatBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asFloatBuffer(); 326 float[] src = new float[1024]; 327 for (int rep = 0; rep < reps; ++rep) { 328 for (int i = 0; i < 1024; ++i) { 329 dst.position(0); 330 dst.put(src); 331 } 332 } 333 } 334 335 public void timeByteBuffer_putInt(int reps) throws Exception { 336 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 337 for (int rep = 0; rep < reps; ++rep) { 338 src.position(aligned ? 0 : 1); 339 for (int i = 0; i < 1024; ++i) { 340 src.putInt(0); 341 } 342 } 343 } 344 345 public void timeIntBuffer_putIntArray(int reps) throws Exception { 346 IntBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asIntBuffer(); 347 int[] src = new int[1024]; 348 for (int rep = 0; rep < reps; ++rep) { 349 for (int i = 0; i < 1024; ++i) { 350 dst.position(0); 351 dst.put(src); 352 } 353 } 354 } 355 356 public void timeByteBuffer_putLong(int reps) throws Exception { 357 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 358 for (int rep = 0; rep < reps; ++rep) { 359 src.position(aligned ? 0 : 1); 360 for (int i = 0; i < 1024; ++i) { 361 src.putLong(0L); 362 } 363 } 364 } 365 366 public void timeLongBuffer_putLongArray(int reps) throws Exception { 367 LongBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asLongBuffer(); 368 long[] src = new long[1024]; 369 for (int rep = 0; rep < reps; ++rep) { 370 for (int i = 0; i < 1024; ++i) { 371 dst.position(0); 372 dst.put(src); 373 } 374 } 375 } 376 377 public void timeByteBuffer_putShort(int reps) throws Exception { 378 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 379 for (int rep = 0; rep < reps; ++rep) { 380 src.position(aligned ? 0 : 1); 381 for (int i = 0; i < 1024; ++i) { 382 src.putShort((short) 0); 383 } 384 } 385 } 386 387 public void timeShortBuffer_putShortArray(int reps) throws Exception { 388 ShortBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asShortBuffer(); 389 short[] src = new short[1024]; 390 for (int rep = 0; rep < reps; ++rep) { 391 for (int i = 0; i < 1024; ++i) { 392 dst.position(0); 393 dst.put(src); 394 } 395 } 396 } 397 398/* 399 public void time_new_byteArray(int reps) throws Exception { 400 for (int rep = 0; rep < reps; ++rep) { 401 byte[] bs = new byte[8192]; 402 } 403 } 404 405 public void time_ByteBuffer_allocate(int reps) throws Exception { 406 for (int rep = 0; rep < reps; ++rep) { 407 ByteBuffer bs = ByteBuffer.allocate(8192); 408 } 409 } 410 */ 411} 412