melix / jmh-gradle-plugin

Integrates the JMH benchmarking framework with Gradle
Apache License 2.0
662 stars 88 forks source link

Unable to use JDK preview features #190

Closed gwinstanley closed 3 years ago

gwinstanley commented 3 years ago

Describe the bug When trying to benchmark code making use of JDK preview features, the plugin fails due to the byte code generator being called without the appropriate JVM arguments being applied.

To Reproduce Steps to reproduce the behavior:

  1. Enable preview features on suitably recent JDK
  2. Write/run benchmark
  3. See error

The compile tasks & jmh DSL section configured including these:

tasks.withType(JavaCompile) {
  options.compilerArgs += [ '--enable-preview' ]
}
jmh {
    jvmArgs = [ '--enable-preview' ]
    //...
}

The resulting error is predictably relating to class version, which implies the JVM arguments configured in the DSL are not being applied to the byte code generator JVM when it is forked:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Preview features are not enabled for foo/Benchmark$Context (class file version 59.65535). Try running with '--enable-preview'
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:468)
        at org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator.main(JmhBytecodeGenerator.java:90)
SirYwell commented 3 years ago

You can add jvm arguments that way:

jmhRunBytecodeGenerator {
    getJvmArgs().add("--enable-preview")
}

(I didn't test that exactly, but that works for incubator modules at least)

gwinstanley commented 3 years ago

@SirYwell Problem solved; many thanks.

LeoFuso commented 1 year ago

The suggested answer didn't work for me, the problem was in the compileJmhJava task also,

compileJmhJava {
    options.compilerArgs += ['--enable-preview', '--add-modules', 'jdk.incubator.vector']
}

jmhRunBytecodeGenerator {
    jvmArgs.add '--add-modules=jdk.incubator.vector'
}