Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharsetEncodeDecode.java
41161 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.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Param;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;3233import java.nio.ByteBuffer;34import java.nio.CharBuffer;35import java.nio.charset.CharacterCodingException;36import java.nio.charset.Charset;37import java.nio.charset.CharsetDecoder;38import java.nio.charset.CharsetEncoder;39import java.util.concurrent.TimeUnit;4041/**42* This benchmark tests the encode/decode loops on different Charsets. It was created from an adhoc benchmark addressing43* a performance issue which in the end boiled down to the encode/decode loops. This is the reason for the values on the44* char and byte arrays.45*/46@BenchmarkMode(Mode.AverageTime)47@OutputTimeUnit(TimeUnit.MICROSECONDS)48@State(Scope.Thread)49public class CharsetEncodeDecode {5051private byte[] BYTES;52private char[] CHARS;5354private CharsetEncoder encoder;55private CharsetDecoder decoder;5657@Param({"BIG5", "ISO-8859-15", "ASCII", "UTF-16"})58private String type;5960@Param("16384")61private int size;6263@Setup64public void prepare() {65BYTES = new byte[size];66CHARS = new char[size];67for (int i = 0; i < size; ++i) {68int val = 48 + (i % 16);69BYTES[i] = (byte) val;70CHARS[i] = (char) val;71}7273encoder = Charset.forName(type).newEncoder();74decoder = Charset.forName(type).newDecoder();75}7677@Benchmark78public ByteBuffer encode() throws CharacterCodingException {79CharBuffer charBuffer = CharBuffer.wrap(CHARS);80return encoder.encode(charBuffer);81}8283@Benchmark84public CharBuffer decode() throws CharacterCodingException {85ByteBuffer byteBuffer = ByteBuffer.wrap(BYTES);86return decoder.decode(byteBuffer);87}8889}909192