Path: blob/master/test/micro/org/openjdk/bench/java/nio/LongBuffers.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)Long(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.LongBuffers.testDirect.*"48* Bulk only :- "org.openjdk.bench.java.nio.LongBuffers.test.*Bulk.*"49* Loop Put Swapped Views: -50* test(Direct|Heap)(Loop)(Put)Long(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 LongBuffers {5960static final int CARRIER_BYTE_WIDTH = 8;6162@Param({"16", "1024", "131072"})63private int size;6465public long longValue;66public long[] longArray;6768public LongBuffer heapLongBuffer;69public LongBuffer heapLongBufferRO;70public LongBuffer heapByteBufferAsLongBufferView;71public LongBuffer heapByteBufferAsLongBufferViewRO;72public LongBuffer heapByteBufferAsLongBufferViewSwap;73public LongBuffer heapByteBufferAsLongBufferViewSwapRO;74public LongBuffer directByteBufferAsLongBufferView;75public LongBuffer directByteBufferAsLongBufferViewRO;76public LongBuffer directByteBufferAsLongBufferViewSwap;77public LongBuffer directByteBufferAsLongBufferViewSwapRO;7879@Setup80public void setup() {81longArray = new long[size / CARRIER_BYTE_WIDTH];8283// explicitly allocated heap carrier buffer84heapLongBuffer = LongBuffer.allocate(size / CARRIER_BYTE_WIDTH);85heapLongBufferRO = LongBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();8687// ByteBuffer views88heapByteBufferAsLongBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asLongBuffer();89heapByteBufferAsLongBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asLongBuffer().asReadOnlyBuffer();90directByteBufferAsLongBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asLongBuffer();91directByteBufferAsLongBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asLongBuffer().asReadOnlyBuffer();9293// endianness swapped94ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;95heapByteBufferAsLongBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asLongBuffer();96heapByteBufferAsLongBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asLongBuffer().asReadOnlyBuffer();97directByteBufferAsLongBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asLongBuffer();98directByteBufferAsLongBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asLongBuffer().asReadOnlyBuffer();99}100101// ---------------- HELPER METHODS102103private int innerLoopGetLong(LongBuffer buf) {104int r = 0;105for (int i = 0; i < buf.capacity(); i++) {106r += buf.get(i);107}108return r;109}110111private void innerLoopPutLong(LongBuffer buf) {112for (int i = 0; i < buf.capacity(); i++) {113buf.put(i, longValue);114}115}116117// -- Heap___118119@Benchmark120public long[] testHeapBulkPutLong() {121heapLongBuffer.put(0, longArray);122return longArray;123}124125@Benchmark126public long[] testHeapBulkGetLong() {127heapLongBuffer.get(0, longArray);128return longArray;129}130131@Benchmark132public void testHeapLoopPutLong() {133innerLoopPutLong(heapLongBuffer);134}135136@Benchmark137public int testHeapLoopGetLong() {138return innerLoopGetLong(heapLongBuffer);139}140141// -- Heap_View_Swap_RO142143@Benchmark144public long[] testHeapBulkGetLongViewSwapRO() {145heapByteBufferAsLongBufferViewSwapRO.get(0, longArray);146return longArray;147}148149@Benchmark150public int testHeapLoopGetLongViewSwapRO() {151return innerLoopGetLong(heapByteBufferAsLongBufferViewSwapRO);152}153154// -- Heap_View_Swap_155156@Benchmark157public long[] testHeapBulkPutLongViewSwap() {158heapByteBufferAsLongBufferViewSwap.put(0, longArray);159return longArray;160}161162@Benchmark163public long[] testHeapBulkGetLongViewSwap() {164heapByteBufferAsLongBufferViewSwap.get(0, longArray);165return longArray;166}167168@Benchmark169public void testHeapLoopPutLongViewSwap() {170innerLoopPutLong(heapByteBufferAsLongBufferViewSwap);171}172173@Benchmark174public int testHeapLoopGetLongViewSwap() {175return innerLoopGetLong(heapByteBufferAsLongBufferViewSwap);176}177178// -- Heap_View__RO179180@Benchmark181public long[] testHeapBulkGetLongViewRO() {182heapByteBufferAsLongBufferViewRO.get(0, longArray);183return longArray;184}185186@Benchmark187public int testHeapLoopGetLongViewRO() {188return innerLoopGetLong(heapByteBufferAsLongBufferViewRO);189}190191// -- Heap_View__192193@Benchmark194public long[] testHeapBulkPutLongView() {195heapByteBufferAsLongBufferView.put(0, longArray);196return longArray;197}198199@Benchmark200public long[] testHeapBulkGetLongView() {201heapByteBufferAsLongBufferView.get(0, longArray);202return longArray;203}204205@Benchmark206public void testHeapLoopPutLongView() {207innerLoopPutLong(heapByteBufferAsLongBufferView);208}209210@Benchmark211public int testHeapLoopGetLongView() {212return innerLoopGetLong(heapByteBufferAsLongBufferView);213}214215// -- Direct_View_Swap_RO216217@Benchmark218public long[] testDirectBulkGetLongViewSwapRO() {219directByteBufferAsLongBufferViewSwapRO.get(0, longArray);220return longArray;221}222223@Benchmark224public int testDirectLoopGetLongViewSwapRO() {225return innerLoopGetLong(directByteBufferAsLongBufferViewSwapRO);226}227228// -- Direct_View_Swap_229230@Benchmark231public long[] testDirectBulkPutLongViewSwap() {232directByteBufferAsLongBufferViewSwap.put(0, longArray);233return longArray;234}235236@Benchmark237public long[] testDirectBulkGetLongViewSwap() {238directByteBufferAsLongBufferViewSwap.get(0, longArray);239return longArray;240}241242@Benchmark243public void testDirectLoopPutLongViewSwap() {244innerLoopPutLong(directByteBufferAsLongBufferViewSwap);245}246247@Benchmark248public int testDirectLoopGetLongViewSwap() {249return innerLoopGetLong(directByteBufferAsLongBufferViewSwap);250}251252// -- Direct_View__RO253254@Benchmark255public long[] testDirectBulkGetLongViewRO() {256directByteBufferAsLongBufferViewRO.get(0, longArray);257return longArray;258}259260@Benchmark261public int testDirectLoopGetLongViewRO() {262return innerLoopGetLong(directByteBufferAsLongBufferViewRO);263}264265// -- Direct_View__266267@Benchmark268public long[] testDirectBulkPutLongView() {269directByteBufferAsLongBufferView.put(0, longArray);270return longArray;271}272273@Benchmark274public long[] testDirectBulkGetLongView() {275directByteBufferAsLongBufferView.get(0, longArray);276return longArray;277}278279@Benchmark280public void testDirectLoopPutLongView() {281innerLoopPutLong(directByteBufferAsLongBufferView);282}283284@Benchmark285public int testDirectLoopGetLongView() {286return innerLoopGetLong(directByteBufferAsLongBufferView);287}288}289290291