1 /*
  2  * Copyright (c) 2021, 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 
 24 package org.openjdk.bench.jdk.incubator.foreign;
 25 
 26 import jdk.incubator.foreign.MemorySegment;
 27 import jdk.incubator.foreign.ResourceScope;
 28 import org.openjdk.jmh.annotations.Benchmark;
 29 import org.openjdk.jmh.annotations.BenchmarkMode;
 30 import org.openjdk.jmh.annotations.CompilerControl;
 31 import org.openjdk.jmh.annotations.Fork;
 32 import org.openjdk.jmh.annotations.Measurement;
 33 import org.openjdk.jmh.annotations.Mode;
 34 import org.openjdk.jmh.annotations.OutputTimeUnit;
 35 import org.openjdk.jmh.annotations.Param;
 36 import org.openjdk.jmh.annotations.Setup;
 37 import org.openjdk.jmh.annotations.State;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 
 40 import java.nio.ByteBuffer;
 41 import java.util.concurrent.TimeUnit;
 42 
 43 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 44 
 45 @BenchmarkMode(Mode.AverageTime)
 46 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @State(org.openjdk.jmh.annotations.Scope.Thread)
 49 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 50 @Fork(value = 1, jvmArgsAppend = {
 51         "--add-modules=jdk.incubator.foreign",
 52         "-Dforeign.restricted=permit",
 53         "--enable-native-access", "ALL-UNNAMED"})
 54 public class TestLoadBytes {
 55     @Param("1024")
 56     private int size;
 57 
 58     private byte[] srcArray;
 59     private ByteBuffer srcBufferNative;
 60     private MemorySegment srcSegmentImplicit;
 61 
 62     @Setup
 63     public void setup() {
 64         srcArray = new byte[size];
 65         for (int i = 0; i < srcArray.length; i++) {
 66             srcArray[i] = (byte) i;
 67         }
 68 
 69         srcBufferNative = ByteBuffer.allocateDirect(size);
 70         srcSegmentImplicit = MemorySegment.allocateNative(size, ResourceScope.newImplicitScope());
 71     }
 72 
 73     @Benchmark
 74     public int arrayScalar() {
 75         int size = 0;
 76         for (int i = 0; i < srcArray.length; i ++) {
 77             var v = srcArray[i];
 78             size += v;
 79         }
 80         return size;
 81     }
 82 
 83     @Benchmark
 84     public int arrayScalarConst() {
 85         int size = 0;
 86         for (int i = 0; i < 1024; i ++) {
 87             var v = srcArray[i];
 88             size += v;
 89         }
 90         return size;
 91     }
 92 
 93     @Benchmark
 94     public int bufferNativeScalar() {
 95         int size = 0;
 96         for (int i = 0; i < srcArray.length; i++) {
 97             var v = srcBufferNative.get(i);
 98             size += v;
 99         }
100         return size;
101     }
102 
103     @Benchmark
104     public int bufferNativeScalarConst() {
105         int size = 0;
106         for (int i = 0; i < 1024; i++) {
107             var v = srcBufferNative.get(i);
108             size += v;
109         }
110         return size;
111     }
112 
113     @Benchmark
114     public int segmentNativeScalar() {
115         int size = 0;
116         for (int i = 0; i < srcArray.length; i++) {
117             var v = srcSegmentImplicit.get(JAVA_BYTE, i);
118             size += v;
119         }
120         return size;
121     }
122 
123     @Benchmark
124     public int segmentNativeScalarConst() {
125         int size = 0;
126         for (int i = 0; i < 1024; i++) {
127             var v = srcSegmentImplicit.get(JAVA_BYTE, i);
128             size += v;
129         }
130         return size;
131     }
132 }