Path: blob/master/test/micro/org/openjdk/bench/java/io/ByteStreamDecoder.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.io;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.io.ByteArrayInputStream;37import java.io.IOException;38import java.io.InputStreamReader;39import java.nio.charset.Charset;40import java.util.concurrent.TimeUnit;4142/**43* Tests the overheads of reading encoded byte arrays via StreamDecoder44*/45@BenchmarkMode(Mode.AverageTime)46@OutputTimeUnit(TimeUnit.NANOSECONDS)47@State(Scope.Thread)48@Warmup(time=2, iterations=5)49@Measurement(time=3, iterations=5)50@Fork(value=2, jvmArgs="-Xmx1g")51public class ByteStreamDecoder {5253@Param({"US-ASCII", "ISO-8859-1", "UTF-8", "ISO-8859-6", "MS932"})54private String charsetName;5556@Param({"256", "4096", "25000"})57private int length;5859private byte[] bytes;6061private byte[] nonASCIIBytesEnd;6263private byte[] nonASCIIBytesStart;6465private byte[] nonASCIIBytesEveryOther;6667private char[] chars;6869private Charset cs;7071@Setup72public void setup() throws IOException {73var s = """74Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non75magna augue. Sed tristique ante id maximus interdum. Suspendisse76potenti. Aliquam molestie metus vitae magna gravida egestas.77Phasellus eleifend tortor sit amet neque euismod, vitae luctus78ante viverra. Sed quis justo ultrices, eleifend dui sed, egestas79lorem. Mauris ipsum ex, interdum eu turpis sed, fermentum efficitur80lorem. Sed vel imperdiet libero, eget ullamcorper sem. Praesent81gravida arcu quis ipsum viverra tristique. Quisque maximus82elit nec nisi vulputate tempor. Integer aliquet tortor vel83vehicula efficitur. Sed neque felis, ultricies eu leo ultricies,84egestas placerat dolor. Etiam iaculis magna quis lacinia85tincidunt. Donec in tellus volutpat, semper nunc ornare,86tempus erat. Donec volutpat mauris in arcu mattis sollicitudin.87Morbi vestibulum ipsum sed erat porta, mollis commodo nisi88gravida.89""";90int n = length / s.length();91String full = "";92if (n > 0) {93full = s.repeat(n);94}95n = length % s.length();96if (n > 0) {97full += s.substring(0, n);98}99cs = Charset.forName(charsetName);100bytes = full.getBytes(cs);101nonASCIIBytesEnd = (full + "\u00e5").getBytes(cs);102nonASCIIBytesStart = ("\u00e5" + full).getBytes(cs);103104// string hostile to ASCII fast-path optimizations: every other char is ASCII105StringBuilder sb = new StringBuilder();106for (int i = 0; i < full.length(); i++) {107sb.append(i % 2 == 0 ? full.charAt(i) : '\u00e5');108}109nonASCIIBytesEveryOther = sb.toString().getBytes(cs);110chars = new char[full.length() + 2];111112try {113if (!readStringDirect().equals(readStringReader())) {114System.out.println("direct: " + readStringDirect());115System.out.println("reader: " + readStringReader());116throw new RuntimeException("Unexpectedly different");117}118if (!readStringDirect_NonASCIIEnd().equals(readStringReader_NonASCIIEnd())) {119throw new RuntimeException("Unexpectedly different");120}121if (!readStringDirect_NonASCIIStart().equals(readStringReader_NonASCIIStart())) {122throw new RuntimeException("Unexpectedly different");123}124if (!readStringDirect_NonASCIIEveryOther().equals(readStringReader_NonASCIIEveryOther())) {125throw new RuntimeException("Unexpectedly different");126}127} catch (Exception e) {128e.printStackTrace();129}130}131132@Benchmark133public String readStringReader() throws Exception {134int len = new InputStreamReader(new ByteArrayInputStream(bytes), cs).read(chars);135return new String(chars, 0, len);136}137138@Benchmark139public String readStringReader_NonASCIIEnd() throws Exception {140int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesEnd), cs).read(chars);141return new String(chars, 0, len);142}143144@Benchmark145public String readStringReader_NonASCIIStart() throws Exception {146int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesStart), cs).read(chars);147return new String(chars, 0, len);148}149150@Benchmark151public String readStringReader_NonASCIIEveryOther() throws Exception {152int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesEveryOther), cs).read(chars);153return new String(chars, 0, len);154}155156@Benchmark157public String readStringDirect() throws Exception {158return new String(bytes, cs);159}160161@Benchmark162public String readStringDirect_NonASCIIEnd() throws Exception {163return new String(nonASCIIBytesEnd, cs);164}165166@Benchmark167public String readStringDirect_NonASCIIStart() throws Exception {168return new String(nonASCIIBytesStart, cs);169}170171@Benchmark172public String readStringDirect_NonASCIIEveryOther() throws Exception {173return new String(nonASCIIBytesEveryOther, cs);174}175}176177178