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