Path: blob/master/test/micro/org/openjdk/bench/java/nio/FloatBuffers.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)Float(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.FloatBuffers.testDirect.*"48* Bulk only :- "org.openjdk.bench.java.nio.FloatBuffers.test.*Bulk.*"49* Loop Put Swapped Views: -50* test(Direct|Heap)(Loop)(Put)Float(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 FloatBuffers {5960static final int CARRIER_BYTE_WIDTH = 4;6162@Param({"16", "1024", "131072"})63private int size;6465public float floatValue;66public float[] floatArray;6768public FloatBuffer heapFloatBuffer;69public FloatBuffer heapFloatBufferRO;70public FloatBuffer heapByteBufferAsFloatBufferView;71public FloatBuffer heapByteBufferAsFloatBufferViewRO;72public FloatBuffer heapByteBufferAsFloatBufferViewSwap;73public FloatBuffer heapByteBufferAsFloatBufferViewSwapRO;74public FloatBuffer directByteBufferAsFloatBufferView;75public FloatBuffer directByteBufferAsFloatBufferViewRO;76public FloatBuffer directByteBufferAsFloatBufferViewSwap;77public FloatBuffer directByteBufferAsFloatBufferViewSwapRO;7879@Setup80public void setup() {81floatArray = new float[size / CARRIER_BYTE_WIDTH];8283// explicitly allocated heap carrier buffer84heapFloatBuffer = FloatBuffer.allocate(size / CARRIER_BYTE_WIDTH);85heapFloatBufferRO = FloatBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();8687// ByteBuffer views88heapByteBufferAsFloatBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asFloatBuffer();89heapByteBufferAsFloatBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asFloatBuffer().asReadOnlyBuffer();90directByteBufferAsFloatBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asFloatBuffer();91directByteBufferAsFloatBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asFloatBuffer().asReadOnlyBuffer();9293// endianness swapped94ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;95heapByteBufferAsFloatBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asFloatBuffer();96heapByteBufferAsFloatBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asFloatBuffer().asReadOnlyBuffer();97directByteBufferAsFloatBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asFloatBuffer();98directByteBufferAsFloatBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asFloatBuffer().asReadOnlyBuffer();99}100101// ---------------- HELPER METHODS102103private int innerLoopGetFloat(FloatBuffer buf) {104int r = 0;105for (int i = 0; i < buf.capacity(); i++) {106r += buf.get(i);107}108return r;109}110111private void innerLoopPutFloat(FloatBuffer buf) {112for (int i = 0; i < buf.capacity(); i++) {113buf.put(i, floatValue);114}115}116117// -- Heap___118119@Benchmark120public float[] testHeapBulkPutFloat() {121heapFloatBuffer.put(0, floatArray);122return floatArray;123}124125@Benchmark126public float[] testHeapBulkGetFloat() {127heapFloatBuffer.get(0, floatArray);128return floatArray;129}130131@Benchmark132public void testHeapLoopPutFloat() {133innerLoopPutFloat(heapFloatBuffer);134}135136@Benchmark137public int testHeapLoopGetFloat() {138return innerLoopGetFloat(heapFloatBuffer);139}140141// -- Heap_View_Swap_RO142143@Benchmark144public float[] testHeapBulkGetFloatViewSwapRO() {145heapByteBufferAsFloatBufferViewSwapRO.get(0, floatArray);146return floatArray;147}148149@Benchmark150public int testHeapLoopGetFloatViewSwapRO() {151return innerLoopGetFloat(heapByteBufferAsFloatBufferViewSwapRO);152}153154// -- Heap_View_Swap_155156@Benchmark157public float[] testHeapBulkPutFloatViewSwap() {158heapByteBufferAsFloatBufferViewSwap.put(0, floatArray);159return floatArray;160}161162@Benchmark163public float[] testHeapBulkGetFloatViewSwap() {164heapByteBufferAsFloatBufferViewSwap.get(0, floatArray);165return floatArray;166}167168@Benchmark169public void testHeapLoopPutFloatViewSwap() {170innerLoopPutFloat(heapByteBufferAsFloatBufferViewSwap);171}172173@Benchmark174public int testHeapLoopGetFloatViewSwap() {175return innerLoopGetFloat(heapByteBufferAsFloatBufferViewSwap);176}177178// -- Heap_View__RO179180@Benchmark181public float[] testHeapBulkGetFloatViewRO() {182heapByteBufferAsFloatBufferViewRO.get(0, floatArray);183return floatArray;184}185186@Benchmark187public int testHeapLoopGetFloatViewRO() {188return innerLoopGetFloat(heapByteBufferAsFloatBufferViewRO);189}190191// -- Heap_View__192193@Benchmark194public float[] testHeapBulkPutFloatView() {195heapByteBufferAsFloatBufferView.put(0, floatArray);196return floatArray;197}198199@Benchmark200public float[] testHeapBulkGetFloatView() {201heapByteBufferAsFloatBufferView.get(0, floatArray);202return floatArray;203}204205@Benchmark206public void testHeapLoopPutFloatView() {207innerLoopPutFloat(heapByteBufferAsFloatBufferView);208}209210@Benchmark211public int testHeapLoopGetFloatView() {212return innerLoopGetFloat(heapByteBufferAsFloatBufferView);213}214215// -- Direct_View_Swap_RO216217@Benchmark218public float[] testDirectBulkGetFloatViewSwapRO() {219directByteBufferAsFloatBufferViewSwapRO.get(0, floatArray);220return floatArray;221}222223@Benchmark224public int testDirectLoopGetFloatViewSwapRO() {225return innerLoopGetFloat(directByteBufferAsFloatBufferViewSwapRO);226}227228// -- Direct_View_Swap_229230@Benchmark231public float[] testDirectBulkPutFloatViewSwap() {232directByteBufferAsFloatBufferViewSwap.put(0, floatArray);233return floatArray;234}235236@Benchmark237public float[] testDirectBulkGetFloatViewSwap() {238directByteBufferAsFloatBufferViewSwap.get(0, floatArray);239return floatArray;240}241242@Benchmark243public void testDirectLoopPutFloatViewSwap() {244innerLoopPutFloat(directByteBufferAsFloatBufferViewSwap);245}246247@Benchmark248public int testDirectLoopGetFloatViewSwap() {249return innerLoopGetFloat(directByteBufferAsFloatBufferViewSwap);250}251252// -- Direct_View__RO253254@Benchmark255public float[] testDirectBulkGetFloatViewRO() {256directByteBufferAsFloatBufferViewRO.get(0, floatArray);257return floatArray;258}259260@Benchmark261public int testDirectLoopGetFloatViewRO() {262return innerLoopGetFloat(directByteBufferAsFloatBufferViewRO);263}264265// -- Direct_View__266267@Benchmark268public float[] testDirectBulkPutFloatView() {269directByteBufferAsFloatBufferView.put(0, floatArray);270return floatArray;271}272273@Benchmark274public float[] testDirectBulkGetFloatView() {275directByteBufferAsFloatBufferView.get(0, floatArray);276return floatArray;277}278279@Benchmark280public void testDirectLoopPutFloatView() {281innerLoopPutFloat(directByteBufferAsFloatBufferView);282}283284@Benchmark285public int testDirectLoopGetFloatView() {286return innerLoopGetFloat(directByteBufferAsFloatBufferView);287}288}289290291