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