Path: blob/master/test/micro/org/openjdk/bench/java/nio/IntBuffers.java
41161 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.nio;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Fork;27import org.openjdk.jmh.annotations.Measurement;28import org.openjdk.jmh.annotations.Mode;29import org.openjdk.jmh.annotations.OutputTimeUnit;30import org.openjdk.jmh.annotations.Param;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.Setup;33import org.openjdk.jmh.annotations.State;34import org.openjdk.jmh.annotations.Warmup;35import java.nio.*;36import java.util.concurrent.TimeUnit;37import static java.nio.ByteOrder.*;3839/**40* Benchmark for memory access operations on java.nio.Buffer ( and its views )41*42* A large number of variants are covered. The individual benchmarks conform to43* the following convention:44* test(Direct|Heap)(Bulk|Loop)(Get|Put)Int(View)?(Swap)?(RO)?45*46* This allows to easily run a subset of particular interest. For example:47* Direct only :- "org.openjdk.bench.java.nio.IntBuffers.testDirect.*"48* Bulk only :- "org.openjdk.bench.java.nio.IntBuffers.test.*Bulk.*"49* Loop Put Swapped Views: -50* test(Direct|Heap)(Loop)(Put)Int(View)+(Swap)+"51*/52@BenchmarkMode(Mode.AverageTime)53@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)54@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)55@OutputTimeUnit(TimeUnit.NANOSECONDS)56@State(Scope.Thread)57@Fork(3)58public class IntBuffers {5960static final int CARRIER_BYTE_WIDTH = 4;6162@Param({"16", "1024", "131072"})63private int size;6465public int intValue;66public int[] intArray;6768public IntBuffer heapIntBuffer;69public IntBuffer heapIntBufferRO;70public IntBuffer heapByteBufferAsIntBufferView;71public IntBuffer heapByteBufferAsIntBufferViewRO;72public IntBuffer heapByteBufferAsIntBufferViewSwap;73public IntBuffer heapByteBufferAsIntBufferViewSwapRO;74public IntBuffer directByteBufferAsIntBufferView;75public IntBuffer directByteBufferAsIntBufferViewRO;76public IntBuffer directByteBufferAsIntBufferViewSwap;77public IntBuffer directByteBufferAsIntBufferViewSwapRO;7879@Setup80public void setup() {81intArray = new int[size / CARRIER_BYTE_WIDTH];8283// explicitly allocated heap carrier buffer84heapIntBuffer = IntBuffer.allocate(size / CARRIER_BYTE_WIDTH);85heapIntBufferRO = IntBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();8687// ByteBuffer views88heapByteBufferAsIntBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asIntBuffer();89heapByteBufferAsIntBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asIntBuffer().asReadOnlyBuffer();90directByteBufferAsIntBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asIntBuffer();91directByteBufferAsIntBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asIntBuffer().asReadOnlyBuffer();9293// endianness swapped94ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;95heapByteBufferAsIntBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asIntBuffer();96heapByteBufferAsIntBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asIntBuffer().asReadOnlyBuffer();97directByteBufferAsIntBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asIntBuffer();98directByteBufferAsIntBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asIntBuffer().asReadOnlyBuffer();99}100101// ---------------- HELPER METHODS102103private int innerLoopGetInt(IntBuffer buf) {104int r = 0;105for (int i = 0; i < buf.capacity(); i++) {106r += buf.get(i);107}108return r;109}110111private void innerLoopPutInt(IntBuffer buf) {112for (int i = 0; i < buf.capacity(); i++) {113buf.put(i, intValue);114}115}116117// -- Heap___118119@Benchmark120public int[] testHeapBulkPutInt() {121heapIntBuffer.put(0, intArray);122return intArray;123}124125@Benchmark126public int[] testHeapBulkGetInt() {127heapIntBuffer.get(0, intArray);128return intArray;129}130131@Benchmark132public void testHeapLoopPutInt() {133innerLoopPutInt(heapIntBuffer);134}135136@Benchmark137public int testHeapLoopGetInt() {138return innerLoopGetInt(heapIntBuffer);139}140141// -- Heap_View_Swap_RO142143@Benchmark144public int[] testHeapBulkGetIntViewSwapRO() {145heapByteBufferAsIntBufferViewSwapRO.get(0, intArray);146return intArray;147}148149@Benchmark150public int testHeapLoopGetIntViewSwapRO() {151return innerLoopGetInt(heapByteBufferAsIntBufferViewSwapRO);152}153154// -- Heap_View_Swap_155156@Benchmark157public int[] testHeapBulkPutIntViewSwap() {158heapByteBufferAsIntBufferViewSwap.put(0, intArray);159return intArray;160}161162@Benchmark163public int[] testHeapBulkGetIntViewSwap() {164heapByteBufferAsIntBufferViewSwap.get(0, intArray);165return intArray;166}167168@Benchmark169public void testHeapLoopPutIntViewSwap() {170innerLoopPutInt(heapByteBufferAsIntBufferViewSwap);171}172173@Benchmark174public int testHeapLoopGetIntViewSwap() {175return innerLoopGetInt(heapByteBufferAsIntBufferViewSwap);176}177178// -- Heap_View__RO179180@Benchmark181public int[] testHeapBulkGetIntViewRO() {182heapByteBufferAsIntBufferViewRO.get(0, intArray);183return intArray;184}185186@Benchmark187public int testHeapLoopGetIntViewRO() {188return innerLoopGetInt(heapByteBufferAsIntBufferViewRO);189}190191// -- Heap_View__192193@Benchmark194public int[] testHeapBulkPutIntView() {195heapByteBufferAsIntBufferView.put(0, intArray);196return intArray;197}198199@Benchmark200public int[] testHeapBulkGetIntView() {201heapByteBufferAsIntBufferView.get(0, intArray);202return intArray;203}204205@Benchmark206public void testHeapLoopPutIntView() {207innerLoopPutInt(heapByteBufferAsIntBufferView);208}209210@Benchmark211public int testHeapLoopGetIntView() {212return innerLoopGetInt(heapByteBufferAsIntBufferView);213}214215// -- Direct_View_Swap_RO216217@Benchmark218public int[] testDirectBulkGetIntViewSwapRO() {219directByteBufferAsIntBufferViewSwapRO.get(0, intArray);220return intArray;221}222223@Benchmark224public int testDirectLoopGetIntViewSwapRO() {225return innerLoopGetInt(directByteBufferAsIntBufferViewSwapRO);226}227228// -- Direct_View_Swap_229230@Benchmark231public int[] testDirectBulkPutIntViewSwap() {232directByteBufferAsIntBufferViewSwap.put(0, intArray);233return intArray;234}235236@Benchmark237public int[] testDirectBulkGetIntViewSwap() {238directByteBufferAsIntBufferViewSwap.get(0, intArray);239return intArray;240}241242@Benchmark243public void testDirectLoopPutIntViewSwap() {244innerLoopPutInt(directByteBufferAsIntBufferViewSwap);245}246247@Benchmark248public int testDirectLoopGetIntViewSwap() {249return innerLoopGetInt(directByteBufferAsIntBufferViewSwap);250}251252// -- Direct_View__RO253254@Benchmark255public int[] testDirectBulkGetIntViewRO() {256directByteBufferAsIntBufferViewRO.get(0, intArray);257return intArray;258}259260@Benchmark261public int testDirectLoopGetIntViewRO() {262return innerLoopGetInt(directByteBufferAsIntBufferViewRO);263}264265// -- Direct_View__266267@Benchmark268public int[] testDirectBulkPutIntView() {269directByteBufferAsIntBufferView.put(0, intArray);270return intArray;271}272273@Benchmark274public int[] testDirectBulkGetIntView() {275directByteBufferAsIntBufferView.get(0, intArray);276return intArray;277}278279@Benchmark280public void testDirectLoopPutIntView() {281innerLoopPutInt(directByteBufferAsIntBufferView);282}283284@Benchmark285public int testDirectLoopGetIntView() {286return innerLoopGetInt(directByteBufferAsIntBufferView);287}288}289290291