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.IOException;
  29 import java.nio.ByteBuffer;
  30 import java.nio.channels.FileChannel;
  31 import java.nio.file.StandardOpenOption;
  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 FileChannelWrite {
  44 
  45     @Param("1000000")
  46     private int fileSize;
  47 
  48     private File f;
  49     private FileChannel fc;
  50     private ByteBuffer bb;
  51     private int count;
  52 
  53     @Setup(Level.Trial)
  54     public void beforeRun() throws IOException {
  55         f = File.createTempFile("FileChannelWriteBench", ".bin");
  56         bb = ByteBuffer.allocate(1);
  57         bb.put((byte) 47);
  58         bb.flip();
  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 IOException {
  68         fc = FileChannel.open(f.toPath(), StandardOpenOption.WRITE);
  69         count = 0;
  70     }
  71 
  72     @TearDown(Level.Iteration)
  73     public void afterIteration() throws IOException {
  74         fc.close();
  75     }
  76 
  77     @Benchmark
  78     public void test() throws IOException {
  79         fc.write(bb);
  80         bb.flip();
  81         count++;
  82         if (count >= fileSize) {
  83             // start over
  84             fc.position(0);
  85             count = 0;
  86         }
  87     }
  88 
  89 }