Path: blob/master/test/micro/org/openjdk/bench/java/io/DataOutputStreamTest.java
41161 views
/*1* Copyright (c) 2020, 2021, Red Hat Inc. 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*/2223package org.openjdk.bench.java.io;2425import org.openjdk.jmh.annotations.*;2627import java.io.*;28import java.util.concurrent.TimeUnit;2930@BenchmarkMode(Mode.AverageTime)31@OutputTimeUnit(TimeUnit.MICROSECONDS)32@Fork(value = 1, warmups = 0)33@Measurement(iterations = 6, time = 1)34@Warmup(iterations=2, time = 2)35@State(Scope.Benchmark)36public class DataOutputStreamTest {3738public enum BasicType {CHAR, SHORT, INT, STRING}39@Param({"CHAR", "SHORT", "INT", "STRING"}) BasicType basicType;4041@Param({"4096"}) int size;42final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size);43File f;44String outputString;45FileOutputStream fileOutputStream;46DataOutput bufferedFileStream, rawFileStream, byteArrayStream;4748@Setup(Level.Trial)49public void setup() throws Exception {50f = File.createTempFile("DataOutputStreamTest","out");51fileOutputStream = new FileOutputStream(f);52byteArrayStream = new DataOutputStream(byteArrayOutputStream);53rawFileStream = new DataOutputStream(fileOutputStream);54bufferedFileStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream));55outputString = new String(new byte[size]);56}5758public void writeChars(DataOutput dataOutput)59throws Exception {60for (int i = 0; i < size; i += 2) {61dataOutput.writeChar(i);62}63}6465public void writeShorts(DataOutput dataOutput)66throws Exception {67for (int i = 0; i < size; i += 2) {68dataOutput.writeShort(i);69}70}7172public void writeInts(DataOutput dataOutput)73throws Exception {74for (int i = 0; i < size; i += 4) {75dataOutput.writeInt(i);76}77}7879public void writeString(DataOutput dataOutput)80throws Exception {81dataOutput.writeChars(outputString);82}8384public void write(DataOutput dataOutput)85throws Exception {86switch (basicType) {87case CHAR:88writeChars(dataOutput);89break;90case SHORT:91writeShorts(dataOutput);92break;93case INT:94writeInts(dataOutput);95break;96case STRING:97writeString(dataOutput);98break;99}100}101102@Benchmark103public void dataOutputStreamOverByteArray() throws Exception {104byteArrayOutputStream.reset();105write(byteArrayStream);106byteArrayOutputStream.flush();107}108109@Benchmark110public void dataOutputStreamOverRawFileStream() throws Exception {111fileOutputStream.getChannel().position(0);112write(rawFileStream);113fileOutputStream.flush();114}115116@Benchmark117public void dataOutputStreamOverBufferedFileStream() throws Exception{118fileOutputStream.getChannel().position(0);119write(bufferedFileStream);120fileOutputStream.flush();121}122}123124125