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.
   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.nio;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Param;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.State;
  32 
  33 import java.nio.ByteBuffer;
  34 import java.util.concurrent.TimeUnit;
  35 
  36 /**
  37  * Benchmark operations on java.nio.Buffer.
  38  */
  39 @BenchmarkMode(Mode.AverageTime)
  40 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  41 @State(Scope.Thread)
  42 public class ByteBuffers {
  43 
  44     @Param({"10", "1000", "100000"})
  45     private int size;
  46 
  47     public byte dummyByte;
  48     public char dummyChar;
  49     public short dummyShort;
  50     public int dummyInt;
  51     public long dummyLong;
  52     public float dummyFloat;
  53     public double dummyDouble;
  54 
  55     // ---------------- BULK GET TESTS
  56 
  57     @Benchmark
  58     public byte[] testBulkGet() {
  59         return innerBufferBulkGet(ByteBuffer.allocate(size));
  60     }
  61 
  62     @Benchmark
  63     public byte[] testDirectBulkGet() {
  64         return innerBufferBulkGet(ByteBuffer.allocateDirect(size));
  65     }
  66 
  67     // ---------------- BULK PUT TESTS
  68 
  69     @Benchmark
  70     public byte[] testBulkPut() {
  71         return innerBufferBulkPut(ByteBuffer.allocate(size));
  72     }
  73 
  74     @Benchmark
  75     public byte[] testDirectBulkPut() {
  76         return innerBufferBulkPut(ByteBuffer.allocateDirect(size));
  77     }
  78 
  79     // ---------------- SINGLE GET TESTS
  80 
  81     @Benchmark
  82     public int testSingleGetByte() {
  83         return innerSingleGetByte(ByteBuffer.allocate(1000));
  84     }
  85 
  86     @Benchmark
  87     public int testSingleGetChar() {
  88         return innerSingleGetChar(ByteBuffer.allocate(1000));
  89     }
  90 
  91     @Benchmark
  92     public int testSingleGetShort() {
  93         return innerSingleGetShort(ByteBuffer.allocate(1000));
  94     }
  95 
  96     @Benchmark
  97     public int testSingleGetInt() {
  98         return innerSingleGetInt(ByteBuffer.allocate(1000));
  99     }
 100 
 101     @Benchmark
 102     public long testSingleGetLong() {
 103         return innerSingleGetLong(ByteBuffer.allocate(1000));
 104     }
 105 
 106     @Benchmark
 107     public float testSingleGetFloat() {
 108         return innerSingleGetFloat(ByteBuffer.allocate(1000));
 109     }
 110 
 111     @Benchmark
 112     public double testSingleGetDouble() {
 113         return innerSingleGetDouble(ByteBuffer.allocate(1000));
 114     }
 115 
 116     @Benchmark
 117     public int testDirectSingleGetByte() {
 118         return innerSingleGetByte(ByteBuffer.allocateDirect(1000));
 119     }
 120 
 121     @Benchmark
 122     public int testDirectSingleGetChar() {
 123         return innerSingleGetChar(ByteBuffer.allocateDirect(1000));
 124     }
 125 
 126     @Benchmark
 127     public int testDirectSingleGetShort() {
 128         return innerSingleGetShort(ByteBuffer.allocateDirect(1000));
 129     }
 130 
 131     @Benchmark
 132     public int testDirectSingleGetInt() {
 133         return innerSingleGetInt(ByteBuffer.allocateDirect(1000));
 134     }
 135 
 136     @Benchmark
 137     public long testDirectSingleGetLong() {
 138         return innerSingleGetLong(ByteBuffer.allocateDirect(1000));
 139     }
 140 
 141     @Benchmark
 142     public float testDirectSingleGetFloat() {
 143         return innerSingleGetFloat(ByteBuffer.allocateDirect(1000));
 144     }
 145 
 146     @Benchmark
 147     public double testDirectSingleGetDouble() {
 148         return innerSingleGetDouble(ByteBuffer.allocateDirect(1000));
 149     }
 150 
 151     // ---------------- SINGLE PUT TESTS
 152 
 153     @Benchmark
 154     public void testSinglePutByte() {
 155         innerSinglePutByte(ByteBuffer.allocate(1000));
 156     }
 157 
 158     @Benchmark
 159     public void testSinglePutChar() {
 160         innerSinglePutChar(ByteBuffer.allocate(1000));
 161     }
 162 
 163     @Benchmark
 164     public void testSinglePutShort() {
 165         innerSinglePutShort(ByteBuffer.allocate(1000));
 166     }
 167 
 168     @Benchmark
 169     public void testSinglePutInt() {
 170         innerSinglePutInt(ByteBuffer.allocate(1000));
 171     }
 172 
 173     @Benchmark
 174     public void testSinglePutLong() {
 175         innerSinglePutLong(ByteBuffer.allocate(1000));
 176     }
 177 
 178     @Benchmark
 179     public void testSinglePutFloat() {
 180         innerSinglePutFloat(ByteBuffer.allocate(1000));
 181     }
 182 
 183     @Benchmark
 184     public void testSinglePutDouble() {
 185         innerSinglePutDouble(ByteBuffer.allocate(1000));
 186     }
 187 
 188     @Benchmark
 189     public void testDirectSinglePutByte() {
 190         innerSinglePutByte(ByteBuffer.allocateDirect(1000));
 191     }
 192 
 193     @Benchmark
 194     public void testDirectSinglePutChar() {
 195         innerSinglePutChar(ByteBuffer.allocateDirect(1000));
 196     }
 197 
 198     @Benchmark
 199     public void testDirectSinglePutShort() {
 200         innerSinglePutShort(ByteBuffer.allocateDirect(1000));
 201     }
 202 
 203     @Benchmark
 204     public void testDirectSinglePutInt() {
 205         innerSinglePutInt(ByteBuffer.allocateDirect(1000));
 206     }
 207 
 208     @Benchmark
 209     public void testDirectSinglePutLong() {
 210         innerSinglePutLong(ByteBuffer.allocateDirect(1000));
 211     }
 212 
 213     @Benchmark
 214     public void testDirectSinglePutFloat() {
 215         innerSinglePutFloat(ByteBuffer.allocateDirect(1000));
 216     }
 217 
 218     @Benchmark
 219     public void testDirectSinglePutDouble() {
 220         innerSinglePutDouble(ByteBuffer.allocateDirect(1000));
 221     }
 222 
 223     // ---------------- HELPER METHODS
 224 
 225     private byte[] innerBufferBulkGet(ByteBuffer bb) {
 226         byte[] dummyByteArray = new byte[bb.capacity()];
 227         bb.get(dummyByteArray);
 228         bb.flip();
 229         return dummyByteArray;
 230     }
 231 
 232     private byte[] innerBufferBulkPut(ByteBuffer bb) {
 233         byte[] dummyByteArray = new byte[bb.capacity()];
 234         bb.put(dummyByteArray);
 235         bb.flip();
 236         return dummyByteArray;
 237     }
 238 
 239     private int innerSingleGetByte(ByteBuffer bb) {
 240         int r = 0;
 241         for (int i = 0; i < bb.capacity(); i++) {
 242             r += bb.get(i);
 243         }
 244         return r;
 245     }
 246 
 247     private int innerSingleGetChar(ByteBuffer bb) {
 248         int r = 0;
 249         for (int i = 0; i < bb.capacity(); i += 2) {
 250             r += bb.getChar(i);
 251         }
 252         return r;
 253     }
 254 
 255     private int innerSingleGetShort(ByteBuffer bb) {
 256         int r = 0;
 257         for (int i = 0; i < bb.capacity(); i += 2) {
 258             r += bb.getShort(i);
 259         }
 260         return r;
 261     }
 262 
 263     private int innerSingleGetInt(ByteBuffer bb) {
 264         int r = 0;
 265         for (int i = 0; i < bb.capacity(); i += 4) {
 266             r += bb.getInt(i);
 267         }
 268         return r;
 269     }
 270 
 271     private long innerSingleGetLong(ByteBuffer bb) {
 272         long r = 0;
 273         for (int i = 0; i < bb.capacity(); i += 8) {
 274             r += bb.getLong(i);
 275         }
 276         return r;
 277     }
 278 
 279     private float innerSingleGetFloat(ByteBuffer bb) {
 280         float r = 0;
 281         for (int i = 0; i < bb.capacity(); i += 4) {
 282             r += bb.getFloat(i);
 283         }
 284         return r;
 285     }
 286 
 287     private double innerSingleGetDouble(ByteBuffer bb) {
 288         double d = 0;
 289         for (int i = 0; i < bb.capacity(); i += 8) {
 290             d += bb.getDouble(i);
 291         }
 292         return d;
 293     }
 294 
 295     private void innerSinglePutByte(ByteBuffer bb) {
 296         for (int i = 0; i < bb.capacity(); i++) {
 297             bb.put(i, dummyByte);
 298         }
 299     }
 300 
 301     private void innerSinglePutChar(ByteBuffer bb) {
 302         for (int i = 0; i < bb.capacity(); i += 2) {
 303             bb.putChar(i, dummyChar);
 304         }
 305     }
 306 
 307     private void innerSinglePutShort(ByteBuffer bb) {
 308         for (int i = 0; i < bb.capacity(); i += 2) {
 309             bb.putShort(i, dummyShort);
 310         }
 311     }
 312 
 313     private void innerSinglePutInt(ByteBuffer bb) {
 314         for (int i = 0; i < bb.capacity(); i += 4) {
 315             bb.putInt(i, dummyInt);
 316         }
 317     }
 318 
 319     private void innerSinglePutLong(ByteBuffer bb) {
 320         for (int i = 0; i < bb.capacity(); i += 8) {
 321             bb.putLong(i, dummyLong);
 322         }
 323     }
 324 
 325     private void innerSinglePutFloat(ByteBuffer bb) {
 326         for (int i = 0; i < bb.capacity(); i += 4) {
 327             bb.putFloat(i, dummyFloat);
 328         }
 329     }
 330 
 331     private void innerSinglePutDouble(ByteBuffer bb) {
 332         for (int i = 0; i < bb.capacity(); i += 8) {
 333             bb.putDouble(i, dummyDouble);
 334         }
 335     }
 336 }