1 /* 2 * Copyright (C) 2012 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 17 package benchmarks.regression; 18 19 import com.google.caliper.Param; 20 import com.google.caliper.Runner; 21 import com.google.caliper.SimpleBenchmark; 22 23 import java.io.*; 24 import java.nio.*; 25 import java.nio.channels.*; 26 import java.util.Arrays; 27 import java.util.Collection; 28 29 public class ByteBufferScalarVersusVectorBenchmark extends SimpleBenchmark { 30 @Param private ByteBufferBenchmark.MyByteOrder byteOrder; 31 @Param({"true", "false"}) private boolean aligned; 32 @Param private ByteBufferBenchmark.MyBufferType bufferType; 33 timeManualByteBufferCopy(int reps)34 public void timeManualByteBufferCopy(int reps) throws Exception { 35 ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 36 ByteBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType); 37 for (int rep = 0; rep < reps; ++rep) { 38 src.position(0); 39 dst.position(0); 40 for (int i = 0; i < 8192; ++i) { 41 dst.put(src.get()); 42 } 43 } 44 } 45 timeByteBufferBulkGet(int reps)46 public void timeByteBufferBulkGet(int reps) throws Exception { 47 ByteBuffer src = ByteBuffer.allocate(aligned ? 8192 : 8192 + 1); 48 byte[] dst = new byte[8192]; 49 for (int rep = 0; rep < reps; ++rep) { 50 src.position(aligned ? 0 : 1); 51 src.get(dst, 0, dst.length); 52 } 53 } 54 timeDirectByteBufferBulkGet(int reps)55 public void timeDirectByteBufferBulkGet(int reps) throws Exception { 56 ByteBuffer src = ByteBuffer.allocateDirect(aligned ? 8192 : 8192 + 1); 57 byte[] dst = new byte[8192]; 58 for (int rep = 0; rep < reps; ++rep) { 59 src.position(aligned ? 0 : 1); 60 src.get(dst, 0, dst.length); 61 } 62 } 63 } 64