Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharsetEncodeDecode.java
66646 views
/*1* Copyright (c) 2014, 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;3536import java.nio.ByteBuffer;37import java.nio.CharBuffer;38import java.nio.charset.CharacterCodingException;39import java.nio.charset.Charset;40import java.nio.charset.CharsetDecoder;41import java.nio.charset.CharsetEncoder;42import java.util.concurrent.TimeUnit;4344/**45* This benchmark tests the encode/decode loops on different Charsets. It was created from an adhoc benchmark addressing46* a performance issue which in the end boiled down to the encode/decode loops. This is the reason for the values on the47* char and byte arrays.48*/49@BenchmarkMode(Mode.AverageTime)50@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)51@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)52@OutputTimeUnit(TimeUnit.MICROSECONDS)53@State(Scope.Thread)54@Fork(3)55public class CharsetEncodeDecode {5657private byte[] BYTES;58private char[] CHARS;5960private CharsetEncoder encoder;61private CharsetDecoder decoder;6263@Param({"UTF-8", "BIG5", "ISO-8859-15", "ASCII", "UTF-16"})64private String type;6566@Param("16384")67private int size;6869@Setup70public void prepare() {71BYTES = new byte[size];72CHARS = new char[size];73for (int i = 0; i < size; ++i) {74int val = 48 + (i % 16);75BYTES[i] = (byte) val;76CHARS[i] = (char) val;77}7879encoder = Charset.forName(type).newEncoder();80decoder = Charset.forName(type).newDecoder();81}8283@Benchmark84public ByteBuffer encode() throws CharacterCodingException {85CharBuffer charBuffer = CharBuffer.wrap(CHARS);86return encoder.encode(charBuffer);87}8889@Benchmark90public CharBuffer decode() throws CharacterCodingException {91ByteBuffer byteBuffer = ByteBuffer.wrap(BYTES);92return decoder.decode(byteBuffer);93}9495}969798