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