Skip to content

Commit c8bc75e

Browse files
committed
Fix up JMH test, and improve existence test performance, especially for Windows.
Note that Windows C (system) drive seems to have some core protections enabled that will always make file access slower for it. Put your stuff on a separate partition or drive.
1 parent 49ffec9 commit c8bc75e

16 files changed

Lines changed: 288 additions & 29 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
/inventorysorter-1.16.1-18.0.0.jar
1616
/modlauncher-9.0.1.jar
1717
/test.jar
18+
/sjh-jmh/build/

build.gradle

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
1+
buildscript {
2+
dependencies {
3+
classpath('de.jjohannes.gradle:extra-java-module-info:0.14')
4+
}
5+
}
6+
17
plugins {
28
id 'com.github.ben-manes.versions' version '0.42.0'
39
id 'net.minecraftforge.gradleutils' version '2.+'
4-
id 'org.javamodularity.moduleplugin' version '1.8.11' apply false
510
}
611

7-
apply plugin: 'java-library'
812
apply plugin: 'maven-publish'
9-
apply plugin: 'eclipse'
10-
apply plugin: 'org.javamodularity.moduleplugin'
13+
14+
allprojects {
15+
apply plugin: 'java-library'
16+
apply plugin: 'eclipse'
17+
apply plugin: 'de.jjohannes.extra-java-module-info'
18+
19+
group 'cpw.mods'
20+
java {
21+
toolchain.languageVersion = JavaLanguageVersion.of(16)
22+
modularity.inferModulePath.set(true)
23+
}
24+
version = gradleutils.getTagOffsetVersion()
25+
26+
repositories {
27+
mavenLocal()
28+
maven {
29+
name = 'forge'
30+
url = 'https://maven.minecraftforge.net/'
31+
}
32+
}
33+
34+
dependencies.testRuntimeOnly('org.apiguardian:apiguardian-api:1.1.2') // No idea why, but windows needs this to not explode.
35+
36+
extraJavaModuleInfo {
37+
failOnMissingModuleInfo = false
38+
automaticModule('jmh-core-1.35.jar', 'jmh.core')
39+
}
40+
41+
def isNonStable = { String version ->
42+
def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { type -> version.toUpperCase().contains(type) }
43+
def regex = /^[0-9,.v-]+(-r)?$/
44+
return !stableKeyword && !(version ==~ regex)
45+
}
46+
47+
dependencyUpdates {
48+
rejectVersionIf {
49+
isNonStable(it.candidate.version)
50+
}
51+
}
52+
53+
// Hack eclipse into knowing that the gradle deps are modules
54+
eclipse {
55+
classpath {
56+
containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
57+
file {
58+
whenMerged {
59+
entries.findAll { it.kind == 'con' && it.path == 'org.eclipse.buildship.core.gradleclasspathcontainer' }.each {
60+
it.entryAttributes['module'] = 'true'
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
1167

1268

1369
repositories {
@@ -31,11 +87,27 @@ dependencyUpdates {
3187
compileJava {
3288
sourceCompatibility = JavaVersion.VERSION_16
3389
targetCompatibility = JavaVersion.VERSION_16
90+
options.compilerArgs += [
91+
'-Xlint:unchecked',
92+
'--add-exports=java.base/sun.security.util=cpw.mods.securejarhandler',
93+
]
3494
}
3595

3696
test {
3797
//exclude '**/*'
3898
useJUnitPlatform()
99+
jvmArgs += [
100+
'--add-opens=java.base/java.lang.invoke=ALL-UNNAMED'
101+
]
102+
}
103+
104+
compileTestJava {
105+
sourceCompatibility = JavaVersion.VERSION_16
106+
targetCompatibility = JavaVersion.VERSION_16
107+
options.compilerArgs += [
108+
'--add-modules=jdk.zipfs',
109+
'--add-exports=jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED'
110+
]
39111
}
40112

41113
group = 'cpw.mods'
@@ -61,13 +133,6 @@ task sourcesJar(type: Jar) {
61133
from sourceSets.main.allSource
62134
}
63135

64-
tasks.named('compileJava') {
65-
options.compilerArgs << '-Xlint:unchecked'
66-
moduleOptions {
67-
addExports = ['java.base/sun.security.util': 'cpw.mods.securejarhandler']
68-
}
69-
}
70-
71136
//jmh {
72137
// benchmarkMode = ['avgt' ]
73138
// profilers = [ 'stack', 'jfr:dir=./build/results/jmh/' ]

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ pluginManagement {
55
}
66
}
77
rootProject.name = 'securejarhandler'
8-
8+
include 'sjh-jmh'

sjh-jmh/build.gradle

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
configurations {
2+
jmhOnly
3+
}
4+
5+
dependencies {
6+
implementation(rootProject)
7+
implementation('org.junit.jupiter:junit-jupiter-engine:5.8.+')
8+
implementation('org.apache.logging.log4j:log4j-core:2.17.1')
9+
implementation('org.apache.logging.log4j:log4j-api:2.17.1')
10+
implementation('org.ow2.asm:asm:9.3')
11+
implementation('org.ow2.asm:asm-tree:9.3')
12+
implementation('org.ow2.asm:asm-commons:9.3')
13+
implementation('org.openjdk.jmh:jmh-core:1.35')
14+
jmhOnly('org.openjdk.jmh:jmh-core:1.35')
15+
jmhOnly('org.openjdk.jmh:jmh-generator-annprocess:1.35')
16+
jmhOnly(sourceSets.main.output)
17+
compileOnly('org.jetbrains:annotations:23.0.0')
18+
runtimeOnly('org.apiguardian:apiguardian-api:1.1.2')
19+
annotationProcessor('org.openjdk.jmh:jmh-generator-annprocess:1.35')
20+
}
21+
22+
task jmh(type: JavaExec, dependsOn: sourceSets.main.output) {
23+
javaLauncher = javaToolchains.launcherFor {
24+
languageVersion = JavaLanguageVersion.of(17)
25+
}
26+
jvmArgs= [
27+
'-p', sourceSets.main.runtimeClasspath.asPath,
28+
'--add-modules', 'ALL-MODULE-PATH',
29+
'--add-opens', 'java.base/java.lang.invoke=cpw.mods.securejarhandler',
30+
'--add-exports', 'cpw.mods.securejarhandler/cpw.mods.niofs.union=ALL-UNNAMED'
31+
]
32+
classpath = files(configurations.jmhOnly.asPath)
33+
mainClass = 'org.openjdk.jmh.Main'
34+
args '-bm', 'avgt' // benchmark mode
35+
args '-r', '5s' // iteration time
36+
args '-w', '5s' // warmup time
37+
args '-wi', '2' // warmup iterations
38+
args '-prof', 'stack' // profilers
39+
args '-tu', 'ns' // time unit
40+
args '-i', '2' // iterations
41+
args '-f', '1' // forks
42+
args '-rff', project.file("${rootProject.buildDir}/jmh_results.txt") // results file
43+
args 'cpw.mods.niofs.union.benchmarks.UnionFileSystemBenchmark'
44+
}

src/jmh/java/cpw/mods/cl/benchmarks/JarModuleFinderBenchmark.java renamed to sjh-jmh/src/main/java/cpw/mods/cl/benchmarks/JarModuleFinderBenchmark.java

File renamed without changes.

src/jmh/java/cpw/mods/niofs/union/benchmarks/UnionFileSystemBenchmark.java renamed to sjh-jmh/src/main/java/cpw/mods/niofs/union/benchmarks/UnionFileSystemBenchmark.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,85 @@
2121
public class UnionFileSystemBenchmark {
2222
private static final UnionFileSystemProvider UFSP = (UnionFileSystemProvider) FileSystemProvider.installedProviders().stream().filter(fsp->fsp.getScheme().equals("union")).findFirst().orElseThrow(()->new IllegalStateException("Couldn't find UnionFileSystemProvider"));
2323
private static UnionFileSystem fileSystem;
24+
private static UnionFileSystem dirFileSystem;
25+
private static Path rawdir;
2426

2527
@Setup
2628
public void setup() throws Exception {
27-
var path1 = Paths.get("./src/jmh/resources/testjar1.jar");
28-
var path2 = Paths.get("./src/jmh/resources/testjar2.jar");
29-
var path3 = Paths.get("./src/jmh/resources/testjar3.jar");
29+
var path1 = Paths.get("src","testjars","testjar1.jar").toAbsolutePath().normalize();
30+
var path2 = Paths.get("src","testjars","testjar2.jar").toAbsolutePath().normalize();
31+
var path3 = Paths.get("src","testjars","testjar3.jar").toAbsolutePath().normalize();
3032
Map<String, List<Path>> properties = new HashMap<>();
3133
var additionalPaths = List.of(path2, path3);
3234
properties.put("additional", additionalPaths);
3335

3436
fileSystem = (UnionFileSystem) UFSP.newFileSystem(path1, properties);
37+
rawdir = Paths.get("src","testrawdir").toAbsolutePath().normalize();
38+
var dir2= Paths.get("src", "testrawdir2").toAbsolutePath().normalize();
39+
dirFileSystem = (UnionFileSystem) UFSP.newFileSystem(rawdir, Map.of("additional", List.of(dir2)));
3540
}
3641

3742
@Benchmark
38-
public void testFileExists(Blackhole blackhole) throws Exception {
43+
public void testJarFileExists(Blackhole blackhole) throws Exception {
3944
runExists("cpw/mods/niofs/union/UnionPath.class", true); //jar 1
4045
runExists("net/minecraftforge/client/event/GuiOpenEvent.class", true); //jar 2
4146
runExists("cpw/mods/modlauncher/Launcher.class", true); //jar 3
47+
}
48+
@Benchmark
49+
public void testJarFileNotExists(Blackhole blackhole) throws Exception {
4250
runExists("cpw/mods/modlauncher/api/NoIDontExist.class", false);
4351
runExists("net/minecraftforge/client/nonexistent/Nope.class", false);
4452
runExists("Missing.class", false);
4553
}
4654

4755
@Benchmark
56+
public void testNativeFileExists(Blackhole blackhole) throws Exception {
57+
runNativeFileExists("ThisFileExists.txt", true);
58+
runNativeFileExists("ThisFileExists2.txt", true);
59+
runNativeFileExists("ThisFileExists3.txt", true);
60+
}
61+
@Benchmark
62+
public void testNativeFileNotExists(Blackhole blackhole) throws Exception {
63+
runNativeFileNotExists("ThisFileNotExists.txt", true);
64+
runNativeFileNotExists("ThisFileNotExists2.txt", true);
65+
runNativeFileNotExists("ThisFileNotExists3.txt", true);
66+
}
67+
68+
// @Benchmark
69+
public void testNativeFileExistsNegative(Blackhole blackhole) throws Exception {
70+
runNativeFileNotExists("ThisFileExists.txt", false);
71+
runNativeFileNotExists("ThisFileExists2.txt", false);
72+
runNativeFileNotExists("ThisFileExists3.txt", false);
73+
}
74+
// @Benchmark
75+
public void testNativeFileNotExistsNegative(Blackhole blackhole) throws Exception {
76+
runNativeFileExists("ThisFileNotExists.txt", false);
77+
runNativeFileExists("ThisFileNotExists2.txt", false);
78+
runNativeFileExists("ThisFileNotExists3.txt", false);
79+
}
80+
81+
// @Benchmark
4882
public void testDirectoryStream(Blackhole blackhole) throws Exception {
4983
runDirStream("cpw/mods/jarhandling", 5, blackhole); //jar 1
5084
runDirStream("net/minecraftforge/common", 72, blackhole); //jar 2
5185
runDirStream("cpw/mods/modlauncher/api", 34, blackhole); //jar 3
5286
}
5387

54-
@Benchmark
88+
// @Benchmark
5589
public void testByteChannel(Blackhole blackhole) throws Exception {
5690
runByteChannel("cpw/mods/niofs/union/UnionPath.class", blackhole); //jar 1
5791
runByteChannel("net/minecraftforge/client/event/GuiOpenEvent.class", blackhole); //jar 2
5892
runByteChannel("cpw/mods/modlauncher/Launcher.class", blackhole); //jar 3
5993
}
6094

61-
@Benchmark
95+
// @Benchmark
6296
public void testReadAttributes(Blackhole blackhole) throws Exception {
6397
runReadAttributes("cpw/mods/niofs/union/UnionPath.class", 9550, blackhole); //jar 1
6498
runReadAttributes("net/minecraftforge/client/event/GuiOpenEvent.class", 782, blackhole); //jar 2
6599
runReadAttributes("cpw/mods/modlauncher/Launcher.class", 12648, blackhole); //jar 3
66100
}
67101

68-
@Benchmark
102+
// @Benchmark
69103
public void testCommonPathUtilities(Blackhole blackhole) throws Exception {
70104
var path = fileSystem.getPath("net/minecraftforge/client/event/GuiOpenEvent.class");
71105
blackhole.consume(path.getFileName());
@@ -74,6 +108,18 @@ public void testCommonPathUtilities(Blackhole blackhole) throws Exception {
74108
blackhole.consume(path.subpath(0, 3));
75109
}
76110

111+
private static void runNativeFileExists(String pathString, boolean expected) throws Exception {
112+
if (Files.exists(dirFileSystem.getPath(pathString)) != expected) {
113+
throw new RuntimeException("Wrong exists status");
114+
}
115+
}
116+
117+
private static void runNativeFileNotExists(String pathString, boolean expected) throws Exception {
118+
if (Files.notExists(dirFileSystem.getPath(pathString)) != expected) {
119+
throw new RuntimeException("Wrong exists status");
120+
}
121+
}
122+
77123
private static void runExists(String pathString, boolean expected) throws Exception {
78124
if (Files.exists(fileSystem.getPath(pathString)) != expected) {
79125
throw new RuntimeException("Wrong exists status");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is empty

0 commit comments

Comments
 (0)