Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharBuffers.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)Char(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.CharBuffers.testDirect.*"48* Bulk only :- "org.openjdk.bench.java.nio.CharBuffers.test.*Bulk.*"49* Loop Put Swapped Views: -50* test(Direct|Heap)(Loop)(Put)Char(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 CharBuffers {5960static final int CARRIER_BYTE_WIDTH = 2;6162@Param({"16", "1024", "131072"})63private int size;6465public char charValue;66public char[] charArray;6768public CharBuffer heapCharBuffer;69public CharBuffer heapCharBufferRO;70public CharBuffer heapByteBufferAsCharBufferView;71public CharBuffer heapByteBufferAsCharBufferViewRO;72public CharBuffer heapByteBufferAsCharBufferViewSwap;73public CharBuffer heapByteBufferAsCharBufferViewSwapRO;74public CharBuffer directByteBufferAsCharBufferView;75public CharBuffer directByteBufferAsCharBufferViewRO;76public CharBuffer directByteBufferAsCharBufferViewSwap;77public CharBuffer directByteBufferAsCharBufferViewSwapRO;7879@Setup80public void setup() {81charArray = new char[size / CARRIER_BYTE_WIDTH];8283// explicitly allocated heap carrier buffer84heapCharBuffer = CharBuffer.allocate(size / CARRIER_BYTE_WIDTH);85heapCharBufferRO = CharBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();8687// ByteBuffer views88heapByteBufferAsCharBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asCharBuffer();89heapByteBufferAsCharBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asCharBuffer().asReadOnlyBuffer();90directByteBufferAsCharBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asCharBuffer();91directByteBufferAsCharBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asCharBuffer().asReadOnlyBuffer();9293// endianness swapped94ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;95heapByteBufferAsCharBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asCharBuffer();96heapByteBufferAsCharBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asCharBuffer().asReadOnlyBuffer();97directByteBufferAsCharBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asCharBuffer();98directByteBufferAsCharBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asCharBuffer().asReadOnlyBuffer();99}100101// ---------------- HELPER METHODS102103private int innerLoopGetChar(CharBuffer buf) {104int r = 0;105for (int i = 0; i < buf.capacity(); i++) {106r += buf.get(i);107}108return r;109}110111private void innerLoopPutChar(CharBuffer buf) {112for (int i = 0; i < buf.capacity(); i++) {113buf.put(i, charValue);114}115}116117// -- Heap___118119@Benchmark120public char[] testHeapBulkPutChar() {121heapCharBuffer.put(0, charArray);122return charArray;123}124125@Benchmark126public char[] testHeapBulkGetChar() {127heapCharBuffer.get(0, charArray);128return charArray;129}130131@Benchmark132public void testHeapLoopPutChar() {133innerLoopPutChar(heapCharBuffer);134}135136@Benchmark137public int testHeapLoopGetChar() {138return innerLoopGetChar(heapCharBuffer);139}140141// -- Heap_View_Swap_RO142143@Benchmark144public char[] testHeapBulkGetCharViewSwapRO() {145heapByteBufferAsCharBufferViewSwapRO.get(0, charArray);146return charArray;147}148149@Benchmark150public int testHeapLoopGetCharViewSwapRO() {151return innerLoopGetChar(heapByteBufferAsCharBufferViewSwapRO);152}153154// -- Heap_View_Swap_155156@Benchmark157public char[] testHeapBulkPutCharViewSwap() {158heapByteBufferAsCharBufferViewSwap.put(0, charArray);159return charArray;160}161162@Benchmark163public char[] testHeapBulkGetCharViewSwap() {164heapByteBufferAsCharBufferViewSwap.get(0, charArray);165return charArray;166}167168@Benchmark169public void testHeapLoopPutCharViewSwap() {170innerLoopPutChar(heapByteBufferAsCharBufferViewSwap);171}172173@Benchmark174public int testHeapLoopGetCharViewSwap() {175return innerLoopGetChar(heapByteBufferAsCharBufferViewSwap);176}177178// -- Heap_View__RO179180@Benchmark181public char[] testHeapBulkGetCharViewRO() {182heapByteBufferAsCharBufferViewRO.get(0, charArray);183return charArray;184}185186@Benchmark187public int testHeapLoopGetCharViewRO() {188return innerLoopGetChar(heapByteBufferAsCharBufferViewRO);189}190191// -- Heap_View__192193@Benchmark194public char[] testHeapBulkPutCharView() {195heapByteBufferAsCharBufferView.put(0, charArray);196return charArray;197}198199@Benchmark200public char[] testHeapBulkGetCharView() {201heapByteBufferAsCharBufferView.get(0, charArray);202return charArray;203}204205@Benchmark206public void testHeapLoopPutCharView() {207innerLoopPutChar(heapByteBufferAsCharBufferView);208}209210@Benchmark211public int testHeapLoopGetCharView() {212return innerLoopGetChar(heapByteBufferAsCharBufferView);213}214215// -- Direct_View_Swap_RO216217@Benchmark218public char[] testDirectBulkGetCharViewSwapRO() {219directByteBufferAsCharBufferViewSwapRO.get(0, charArray);220return charArray;221}222223@Benchmark224public int testDirectLoopGetCharViewSwapRO() {225return innerLoopGetChar(directByteBufferAsCharBufferViewSwapRO);226}227228// -- Direct_View_Swap_229230@Benchmark231public char[] testDirectBulkPutCharViewSwap() {232directByteBufferAsCharBufferViewSwap.put(0, charArray);233return charArray;234}235236@Benchmark237public char[] testDirectBulkGetCharViewSwap() {238directByteBufferAsCharBufferViewSwap.get(0, charArray);239return charArray;240}241242@Benchmark243public void testDirectLoopPutCharViewSwap() {244innerLoopPutChar(directByteBufferAsCharBufferViewSwap);245}246247@Benchmark248public int testDirectLoopGetCharViewSwap() {249return innerLoopGetChar(directByteBufferAsCharBufferViewSwap);250}251252// -- Direct_View__RO253254@Benchmark255public char[] testDirectBulkGetCharViewRO() {256directByteBufferAsCharBufferViewRO.get(0, charArray);257return charArray;258}259260@Benchmark261public int testDirectLoopGetCharViewRO() {262return innerLoopGetChar(directByteBufferAsCharBufferViewRO);263}264265// -- Direct_View__266267@Benchmark268public char[] testDirectBulkPutCharView() {269directByteBufferAsCharBufferView.put(0, charArray);270return charArray;271}272273@Benchmark274public char[] testDirectBulkGetCharView() {275directByteBufferAsCharBufferView.get(0, charArray);276return charArray;277}278279@Benchmark280public void testDirectLoopPutCharView() {281innerLoopPutChar(directByteBufferAsCharBufferView);282}283284@Benchmark285public int testDirectLoopGetCharView() {286return innerLoopGetChar(directByteBufferAsCharBufferView);287}288}289290291