Path: blob/master/test/micro/org/openjdk/bench/java/io/RandomAccessWrite.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.io;2324import java.io.File;25import java.io.FileOutputStream;26import java.io.IOException;27import java.io.RandomAccessFile;28import java.util.concurrent.TimeUnit;2930import org.openjdk.jmh.annotations.Benchmark;31import org.openjdk.jmh.annotations.BenchmarkMode;32import org.openjdk.jmh.annotations.Level;33import org.openjdk.jmh.annotations.Mode;34import org.openjdk.jmh.annotations.OutputTimeUnit;35import org.openjdk.jmh.annotations.Param;36import org.openjdk.jmh.annotations.Scope;37import org.openjdk.jmh.annotations.Setup;38import org.openjdk.jmh.annotations.State;39import org.openjdk.jmh.annotations.TearDown;4041/**42* Tests the overheads of I/O API.43* This test is known to depend heavily on disk subsystem performance.44*/45@BenchmarkMode(Mode.Throughput)46@OutputTimeUnit(TimeUnit.MILLISECONDS)47@State(Scope.Thread)48public class RandomAccessWrite {4950@Param("1000000")51private int fileSize;5253private File f;54private RandomAccessFile raf;55private long offset;56private int deltaIndex;57private int[] deltas;5859@Setup(Level.Trial)60public void beforeRun() throws IOException {61f = File.createTempFile("RandomAccessBench", ".bin");62try (FileOutputStream fos = new FileOutputStream(f)) {63for (int i = 0; i < fileSize; i++) {64fos.write((byte) i);65}66}67deltas = new int[]{1, 2, 3, 5, 7, 11, 13, 17, 19, 23};68}6970@TearDown(Level.Trial)71public void afterRun() throws IOException {72f.delete();73}7475@Setup(Level.Iteration)76public void beforeIteration() throws IOException {77raf = new RandomAccessFile(f, "rw");78offset = 0;79deltaIndex = 0;80}8182@TearDown(Level.Iteration)83public void afterIteration() throws IOException {84raf.close();85}8687@Benchmark88public void test() throws IOException {89offset = offset + deltas[deltaIndex];90if (offset >= fileSize) {91offset = 0;92}93deltaIndex++;94if (deltaIndex >= deltas.length) {95deltaIndex = 0;96}97raf.seek(offset);98raf.write((byte) offset);99}100101}102103104