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.io; 26 27 import java.io.File; 28 import java.io.FileInputStream; 29 import java.io.FileNotFoundException; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.util.concurrent.TimeUnit; 33 34 import org.openjdk.jmh.annotations.*; 35 36 /** 37 * Tests the overheads of I/O API. 38 * This test is known to depend heavily on disk subsystem performance. 39 */ 40 @BenchmarkMode(Mode.Throughput) 41 @OutputTimeUnit(TimeUnit.MILLISECONDS) 42 @State(Scope.Thread) 43 public class FileRead { 44 45 @Param("1000000") 46 private int fileSize; 47 48 private File f; 49 private FileInputStream fis; 50 51 @Setup(Level.Trial) 52 public void beforeRun() throws IOException { 53 f = File.createTempFile("FileReadBench", ".bin"); 54 try (FileOutputStream fos = new FileOutputStream(f)) { 55 for (int i = 0; i < fileSize; i++) { 56 fos.write((byte) i); 57 } 58 } 59 } 60 61 @TearDown(Level.Trial) 62 public void afterRun() throws IOException { 63 f.delete(); 64 } 65 66 @Setup(Level.Iteration) 67 public void beforeIteration() throws FileNotFoundException { 68 fis = new FileInputStream(f); 69 } 70 71 @TearDown(Level.Iteration) 72 public void afterIteration() throws IOException { 73 fis.close(); 74 } 75 76 @Benchmark 77 public void test() throws IOException { 78 int ret = fis.read(); 79 if (ret == -1) { 80 // start over 81 fis.close(); 82 fis = new FileInputStream(f); 83 } 84 } 85 86 }