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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.io;
  24 
  25 import java.io.File;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;
  28 import java.io.RandomAccessFile;
  29 import java.util.concurrent.TimeUnit;
  30 
  31 import org.openjdk.jmh.annotations.Benchmark;
  32 import org.openjdk.jmh.annotations.BenchmarkMode;
  33 import org.openjdk.jmh.annotations.Level;
  34 import org.openjdk.jmh.annotations.Mode;
  35 import org.openjdk.jmh.annotations.OutputTimeUnit;
  36 import org.openjdk.jmh.annotations.Param;
  37 import org.openjdk.jmh.annotations.Scope;
  38 import org.openjdk.jmh.annotations.Setup;
  39 import org.openjdk.jmh.annotations.State;
  40 import org.openjdk.jmh.annotations.TearDown;
  41 
  42 /**
  43  * Tests the overheads of I/O API.
  44  * This test is known to depend heavily on disk subsystem performance.
  45  */
  46 @BenchmarkMode(Mode.Throughput)
  47 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  48 @State(Scope.Thread)
  49 public class RandomAccessRead {
  50 
  51     @Param("1000000")
  52     private int fileSize;
  53 
  54     private RandomAccessFile raf;
  55     private long offset;
  56     private int deltaIndex;
  57     private int[] deltas;
  58     private File f;
  59 
  60     @Setup(Level.Trial)
  61     public void beforeRun() throws IOException {
  62         f = File.createTempFile("RandomAccessBench", ".bin");
  63         try (FileOutputStream fos = new FileOutputStream(f)) {
  64             for (int i = 0; i < fileSize; i++) {
  65                 fos.write((byte) i);
  66             }
  67         }
  68         deltas = new int[]{1, 2, 3, 5, 7, 11, 13, 17, 19, 23};
  69     }
  70 
  71     @TearDown(Level.Trial)
  72     public void afterRun() throws IOException {
  73         f.delete();
  74     }
  75 
  76     @Setup(Level.Iteration)
  77     public void beforeIteration() throws IOException {
  78         raf = new RandomAccessFile(f, "rw");
  79         offset = 0;
  80         deltaIndex = 0;
  81     }
  82 
  83     @TearDown(Level.Iteration)
  84     public void afterIteration() throws IOException {
  85         raf.close();
  86     }
  87 
  88     @Benchmark
  89     public int test() throws IOException {
  90         offset = offset + deltas[deltaIndex];
  91         if (offset >= fileSize) {
  92             offset = 0;
  93         }
  94         deltaIndex++;
  95         if (deltaIndex >= deltas.length) {
  96             deltaIndex = 0;
  97         }
  98         raf.seek(offset);
  99         return raf.read();
 100     }
 101 
 102 }