Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/io/RandomAccessRead.java
41161 views
1
/*
2
* Copyright (c) 2014, 2020, 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
@Param("8192")
55
private int buffer;
56
57
private RandomAccessFile raf;
58
private long offset;
59
private int deltaIndex;
60
private int[] deltas;
61
private File f;
62
private byte[] buf;
63
64
@Setup(Level.Trial)
65
public void beforeRun() throws IOException {
66
f = File.createTempFile("RandomAccessBench", ".bin");
67
try (FileOutputStream fos = new FileOutputStream(f)) {
68
for (int i = 0; i < fileSize; i++) {
69
fos.write((byte) i);
70
}
71
}
72
deltas = new int[]{1, 2, 3, 5, 7, 11, 13, 17, 19, 23};
73
buf = new byte[buffer];
74
}
75
76
@TearDown(Level.Trial)
77
public void afterRun() throws IOException {
78
f.delete();
79
}
80
81
@Setup(Level.Iteration)
82
public void beforeIteration() throws IOException {
83
raf = new RandomAccessFile(f, "rw");
84
offset = 0;
85
deltaIndex = 0;
86
}
87
88
@TearDown(Level.Iteration)
89
public void afterIteration() throws IOException {
90
raf.close();
91
}
92
93
@Benchmark
94
public int testBuffer() throws IOException {
95
offset = offset + deltas[deltaIndex];
96
if (offset >= fileSize) {
97
offset = 0;
98
}
99
deltaIndex++;
100
if (deltaIndex >= deltas.length) {
101
deltaIndex = 0;
102
}
103
raf.seek(offset);
104
return raf.read(buf);
105
}
106
107
@Benchmark
108
public int test() throws IOException {
109
offset = offset + deltas[deltaIndex];
110
if (offset >= fileSize) {
111
offset = 0;
112
}
113
deltaIndex++;
114
if (deltaIndex >= deltas.length) {
115
deltaIndex = 0;
116
}
117
raf.seek(offset);
118
return raf.read();
119
}
120
}
121
122