1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.java.nio;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Mode;
  30 import org.openjdk.jmh.annotations.OutputTimeUnit;
  31 import org.openjdk.jmh.annotations.Param;
  32 import org.openjdk.jmh.annotations.Scope;
  33 import org.openjdk.jmh.annotations.Setup;
  34 import org.openjdk.jmh.annotations.State;
  35 
  36 import java.nio.ByteBuffer;
  37 import java.nio.CharBuffer;
  38 import java.nio.charset.CharacterCodingException;
  39 import java.nio.charset.Charset;
  40 import java.nio.charset.CharsetDecoder;
  41 import java.nio.charset.CharsetEncoder;
  42 import java.util.concurrent.TimeUnit;
  43 
  44 /**
  45  * This benchmark tests the encode/decode loops on different Charsets. It was created from an adhoc benchmark addressing
  46  * a performance issue which in the end boiled down to the encode/decode loops. This is the reason for the values on the
  47  * char and byte arrays.
  48  */
  49 @BenchmarkMode(Mode.AverageTime)
  50 @OutputTimeUnit(TimeUnit.MICROSECONDS)
  51 @State(Scope.Thread)
  52 public class CharsetEncodeDecode {
  53 
  54     private byte[] BYTES;
  55     private char[] CHARS;
  56 
  57     private CharsetEncoder encoder;
  58     private CharsetDecoder decoder;
  59 
  60     @Param({"BIG5", "ISO-8859-15", "ASCII", "UTF-16"})
  61     private String type;
  62 
  63     @Param("16384")
  64     private int size;
  65 
  66     @Setup
  67     public void prepare() {
  68         BYTES = new byte[size];
  69         CHARS = new char[size];
  70         for (int i = 0; i < size; ++i) {
  71             int val = 48 + (i % 16);
  72             BYTES[i] = (byte) val;
  73             CHARS[i] = (char) val;
  74         }
  75 
  76         encoder = Charset.forName(type).newEncoder();
  77         decoder = Charset.forName(type).newDecoder();
  78     }
  79 
  80     @Benchmark
  81     public ByteBuffer encode() throws CharacterCodingException {
  82         CharBuffer charBuffer = CharBuffer.wrap(CHARS);
  83         return encoder.encode(charBuffer);
  84     }
  85 
  86     @Benchmark
  87     public CharBuffer decode() throws CharacterCodingException {
  88         ByteBuffer byteBuffer = ByteBuffer.wrap(BYTES);
  89         return decoder.decode(byteBuffer);
  90     }
  91 
  92 }