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