< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/BulkMismatchAcquire.java

Print this page

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 
 28 import jdk.incubator.foreign.ResourceScope;
 29 import jdk.incubator.foreign.SegmentAllocator;
 30 import org.openjdk.jmh.annotations.Benchmark;
 31 import org.openjdk.jmh.annotations.BenchmarkMode;
 32 import org.openjdk.jmh.annotations.Fork;
 33 import org.openjdk.jmh.annotations.Measurement;
 34 import org.openjdk.jmh.annotations.Mode;
 35 import org.openjdk.jmh.annotations.Param;
 36 import org.openjdk.jmh.annotations.OutputTimeUnit;
 37 import org.openjdk.jmh.annotations.Setup;
 38 import org.openjdk.jmh.annotations.State;
 39 import org.openjdk.jmh.annotations.TearDown;
 40 import org.openjdk.jmh.annotations.Warmup;
 41 import sun.misc.Unsafe;
 42 
 43 import jdk.incubator.foreign.MemorySegment;
 44 import java.nio.ByteBuffer;
 45 import java.util.concurrent.TimeUnit;
 46 import java.util.function.Supplier;
 47 
 48 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 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.MILLISECONDS)
 55 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 56 public class BulkMismatchAcquire {
 57 
 58     public enum ScopeKind {
 59         CONFINED(ResourceScope::newConfinedScope),
 60         SHARED(ResourceScope::newSharedScope),
 61         IMPLICIT(ResourceScope::newImplicitScope);
 62 
 63         final Supplier<ResourceScope> scopeFactory;
 64 
 65         ScopeKind(Supplier<ResourceScope> scopeFactory) {
 66             this.scopeFactory = scopeFactory;
 67         }
 68 
 69         ResourceScope makeScope() {
 70             return scopeFactory.get();
 71         }
 72     }
 73 
 74     @Param({"CONFINED", "SHARED", "IMPLICIT"})
 75     public ScopeKind scopeKind;
 76 
 77     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 78     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 79 
 80     ResourceScope scope;
 81     MemorySegment mismatchSegmentLarge1;
 82     MemorySegment mismatchSegmentLarge2;
 83     ByteBuffer mismatchBufferLarge1;
 84     ByteBuffer mismatchBufferLarge2;
 85     MemorySegment mismatchSegmentSmall1;
 86     MemorySegment mismatchSegmentSmall2;
 87     ByteBuffer mismatchBufferSmall1;
 88     ByteBuffer mismatchBufferSmall2;
 89 
 90     @Setup
 91     public void setup() {
 92         scope = scopeKind.makeScope();
 93         mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 94         mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);

104             mismatchSegmentSmall1.fill((byte) 0xFF);
105             mismatchBufferSmall1.put((byte) 0xFF).clear();
106             // verify expected mismatch indices
107             long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
108             if (si != -1)
109                 throw new AssertionError("Unexpected mismatch index:" + si);
110             int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
111             if (bi != -1)
112                 throw new AssertionError("Unexpected mismatch index:" + bi);
113             si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
114             if (si != 0)
115                 throw new AssertionError("Unexpected mismatch index:" + si);
116             bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
117             if (bi != 0)
118                 throw new AssertionError("Unexpected mismatch index:" + bi);
119         }
120     }
121 
122     @TearDown
123     public void tearDown() {
124         if (!scope.isImplicit())
125             scope.close();
126     }
127 
128     @Benchmark
129     @OutputTimeUnit(TimeUnit.NANOSECONDS)
130     public long mismatch_large_segment() {
131         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
132     }
133 
134     @Benchmark
135     @OutputTimeUnit(TimeUnit.NANOSECONDS)
136     public long mismatch_large_segment_acquire() {
137         var handle = mismatchSegmentLarge1.scope().acquire();
138         try {
139             return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
140         } finally {
141             mismatchSegmentLarge1.scope().release(handle);
142         }
143     }
144 
145     @Benchmark
146     @OutputTimeUnit(TimeUnit.NANOSECONDS)
147     public int mismatch_large_bytebuffer() {
148         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
149     }
150 
151     @Benchmark
152     @OutputTimeUnit(TimeUnit.NANOSECONDS)
153     public long mismatch_small_segment() {
154         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
155     }
156 
157     @Benchmark
158     @OutputTimeUnit(TimeUnit.NANOSECONDS)
159     public long mismatch_small_segment_acquire() {
160         var handle = mismatchSegmentLarge1.scope().acquire();
161         try {
162             return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
163         } finally {
164             mismatchSegmentLarge1.scope().release(handle);
165         }
166     }
167 
168     @Benchmark
169     @OutputTimeUnit(TimeUnit.NANOSECONDS)
170     public int mismatch_small_bytebuffer() {
171         return mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
172     }
173 }

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 
 28 import jdk.incubator.foreign.ResourceScope;

 29 import org.openjdk.jmh.annotations.Benchmark;
 30 import org.openjdk.jmh.annotations.BenchmarkMode;
 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.Param;
 35 import org.openjdk.jmh.annotations.OutputTimeUnit;
 36 import org.openjdk.jmh.annotations.Setup;
 37 import org.openjdk.jmh.annotations.State;
 38 import org.openjdk.jmh.annotations.TearDown;
 39 import org.openjdk.jmh.annotations.Warmup;

 40 
 41 import jdk.incubator.foreign.MemorySegment;
 42 import java.nio.ByteBuffer;
 43 import java.util.concurrent.TimeUnit;
 44 import java.util.function.Supplier;
 45 


 46 @BenchmarkMode(Mode.AverageTime)
 47 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @State(org.openjdk.jmh.annotations.Scope.Thread)
 50 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 51 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 52 public class BulkMismatchAcquire {
 53 
 54     public enum ScopeKind {
 55         CONFINED(ResourceScope::newConfinedScope),
 56         SHARED(ResourceScope::newSharedScope),
 57         IMPLICIT(ResourceScope::newImplicitScope);
 58 
 59         final Supplier<ResourceScope> scopeFactory;
 60 
 61         ScopeKind(Supplier<ResourceScope> scopeFactory) {
 62             this.scopeFactory = scopeFactory;
 63         }
 64 
 65         ResourceScope makeScope() {
 66             return scopeFactory.get();
 67         }
 68     }
 69 
 70     @Param({"CONFINED", "SHARED"})
 71     public ScopeKind scopeKind;
 72 
 73     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 74     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 75 
 76     ResourceScope scope;
 77     MemorySegment mismatchSegmentLarge1;
 78     MemorySegment mismatchSegmentLarge2;
 79     ByteBuffer mismatchBufferLarge1;
 80     ByteBuffer mismatchBufferLarge2;
 81     MemorySegment mismatchSegmentSmall1;
 82     MemorySegment mismatchSegmentSmall2;
 83     ByteBuffer mismatchBufferSmall1;
 84     ByteBuffer mismatchBufferSmall2;
 85 
 86     @Setup
 87     public void setup() {
 88         scope = scopeKind.makeScope();
 89         mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 90         mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);

100             mismatchSegmentSmall1.fill((byte) 0xFF);
101             mismatchBufferSmall1.put((byte) 0xFF).clear();
102             // verify expected mismatch indices
103             long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
104             if (si != -1)
105                 throw new AssertionError("Unexpected mismatch index:" + si);
106             int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
107             if (bi != -1)
108                 throw new AssertionError("Unexpected mismatch index:" + bi);
109             si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
110             if (si != 0)
111                 throw new AssertionError("Unexpected mismatch index:" + si);
112             bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
113             if (bi != 0)
114                 throw new AssertionError("Unexpected mismatch index:" + bi);
115         }
116     }
117 
118     @TearDown
119     public void tearDown() {
120         scope.close();

121     }
122 
123     @Benchmark
124     @OutputTimeUnit(TimeUnit.NANOSECONDS)
125     public long mismatch_large_segment() {
126         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
127     }
128 
129     @Benchmark
130     @OutputTimeUnit(TimeUnit.NANOSECONDS)
131     public long mismatch_large_segment_acquire() {
132         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
133             scope.keepAlive(mismatchSegmentLarge1.scope());
134             return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);


135         }
136     }
137 
138     @Benchmark
139     @OutputTimeUnit(TimeUnit.NANOSECONDS)
140     public int mismatch_large_bytebuffer() {
141         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
142     }
143 
144     @Benchmark
145     @OutputTimeUnit(TimeUnit.NANOSECONDS)
146     public long mismatch_small_segment() {
147         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
148     }
149 
150     @Benchmark
151     @OutputTimeUnit(TimeUnit.NANOSECONDS)
152     public long mismatch_small_segment_acquire() {
153         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
154             scope.keepAlive(mismatchSegmentLarge1.scope());
155             return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);


156         }
157     }
158 
159     @Benchmark
160     @OutputTimeUnit(TimeUnit.NANOSECONDS)
161     public int mismatch_small_bytebuffer() {
162         return mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
163     }
164 }
< prev index next >