Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/io/FileOpen.java
41161 views
1
/*
2
* Copyright (c) 2020, 2021, 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 org.openjdk.jmh.annotations.*;
26
import org.openjdk.jmh.infra.Blackhole;
27
28
import java.io.File;
29
import java.io.IOException;
30
import java.nio.file.Path;
31
import java.util.concurrent.TimeUnit;
32
33
/**
34
* Tests the overheads of creating File objects, and converting such objects to Paths.
35
*/
36
@BenchmarkMode(Mode.AverageTime)
37
@OutputTimeUnit(TimeUnit.NANOSECONDS)
38
@State(Scope.Thread)
39
@Warmup(time=2, iterations=5)
40
@Measurement(time=3, iterations=5)
41
@Fork(value=2, jvmArgs="-Xmx1g")
42
public class FileOpen {
43
44
private String normalFile = "/test/dir/file/name.txt";
45
private String root = "/";
46
private String trailingSlash = "/test/dir/file/name.txt/";
47
private String notNormalizedFile = "/test/dir/file//name.txt";
48
49
public File tmp;
50
51
@Setup
52
public void setup() throws IOException {
53
tmp = new File("FileOpen.tmp");
54
tmp.createNewFile();
55
tmp.deleteOnExit();
56
}
57
58
@Benchmark
59
public void mix(Blackhole bh) {
60
bh.consume(new File(normalFile));
61
bh.consume(new File(root));
62
bh.consume(new File(trailingSlash));
63
bh.consume(new File(notNormalizedFile));
64
}
65
66
@Benchmark
67
public File normalized() {
68
return new File(normalFile);
69
}
70
71
@Benchmark
72
public File root() {
73
return new File(root);
74
}
75
76
@Benchmark
77
public File trailingSlash() {
78
return new File(trailingSlash);
79
}
80
81
@Benchmark
82
public File notNormalized() {
83
return new File(notNormalizedFile);
84
}
85
86
@Benchmark
87
public boolean booleanAttributes() {
88
return tmp.exists()
89
&& tmp.isHidden()
90
&& tmp.isDirectory()
91
&& tmp.isFile();
92
}
93
94
@Benchmark
95
public void mixToPath(Blackhole bh) {
96
bh.consume(new File(normalFile).toPath());
97
bh.consume(new File(root).toPath());
98
bh.consume(new File(trailingSlash).toPath());
99
bh.consume(new File(notNormalizedFile).toPath());
100
}
101
102
@Benchmark
103
public Path normalizedToPath() {
104
return new File(normalFile).toPath();
105
}
106
107
@Benchmark
108
public Path rootToPath() {
109
return new File(root).toPath();
110
}
111
112
@Benchmark
113
public Path trailingSlashToPath() {
114
return new File(trailingSlash).toPath();
115
}
116
117
@Benchmark
118
public Path notNormalizedToPath() {
119
return new File(notNormalizedFile).toPath();
120
}
121
}
122
123