melix / jmh-gradle-plugin

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

How To Configure --enable-preview? #143

Closed jpe42 closed 3 years ago

jpe42 commented 5 years ago

I'm trying to setup some benchmarks against Java 12 code which use preview features. I have the following tasks configured to compile with --enable-preview, but I still get the runtime exception below indicating that the JMH classes were not compiled with the --enable-preview flag. Is there any task I can configure to make sure the compiler picks up that argument?

Config Snippet

jmh {
  include = ['BenchStyles']
  jmhVersion = '+'
  jvmArgs = ['--enable-preview']

  resultFormat = 'JSON'
  includeTests = false
}

jmhCompileGeneratedClasses {
  options.compilerArgs = ['--enable-preview']
}

compileJmhJava {
  options.compilerArgs = ['--enable-preview']
}

compileJava {
  options.compilerArgs = ['--enable-preview']
}

compileTestJava {
  options.compilerArgs = ['--enable-preview']
}

"./gradlew jmh" Error

Execution failed for task ':json-iterator:jmhRunBytecodeGenerator'.
> A failure occurred while executing me.champeau.gradle.JmhBytecodeGeneratorRunnable
   > Preview features are not enabled for systems/comodal/jsoniter/ExchangeInfoVal$ExchangeInfoBuilder (class file version 56.65535). Try running with '--enable-preview'
BenTels commented 3 years ago

Two and a half years and still not fixed...

I was finally able to work around this by setting _JAVA_OPTIONS='--enable-preview' (which is a seriously BAD option). But it's totally hopeless, because then you run into

Task :jmh FAILED

FAILURE: Build failed with an exception.

This plugin seems totally incompatible with OpenJDK 14+ (I'm on build 15.0.2+7-27, no reason to think 16 will be better).

melix commented 3 years ago

Two and a half years and still not fixed...

I don't think this kind of comment is anywhere close to appropriate. This is open source software. I work on this on personal time and do what I can.

I don't have a computer at hand to check this now, but since this ticket is about enabling preview, I wrote this blog post a few months ago : https://melix.github.io/blog/2020/06/java-feature-previews-gradle.html

It may help.

BenTels commented 3 years ago

I don't think this kind of comment is anywhere close to appropriate. This is open source software. I work on this on personal time and do what I can.

Perhaps not. And I will fully admit it was borne from frustration in trying to deal with this issue.

That said, your issue is clearly that the --enable-preview option is not reaching the JmhBytecodeGeneratorRunnable (after all, forcing it with the _JAVA_OPTTIONS env var makes the issue go away). Sorry to be abrasive, but the simple fix here seems to be a combination of adding a jvmArgs property to that task and possibly pass-through support for when the task invoked is jmh.

I don't have a computer at hand to check this now, but since this ticket is about enabling preview, I wrote this blog post a few months ago : https://melix.github.io/blog/2020/06/java-feature-previews-gradle.html

It may help.

It doesn't. I tried your suggestion about trying to force the --enable-preview flag globally for compile and test and it did nothing in my case (which was pretty basic). Like I said, you are losing the flag somewhere.

FYI, I am stopping at this point. As stated, circumventing the problem leads to a different problem for which there is also an open issue. A problem stemming from the fact that you are using pre-Java 11 reflection rules as I understand from that issue, so I guess that really will require serious rework to fix. So for me it is a move to maven and a wish of good luck to you in getting your plugin working again.

Ben.

melix commented 3 years ago

Interestingly, most of the issues reported on this plugin are a consequence of following an advice from the JMH authors: in the beginning it was delegating to the CLI. But then I was told "that's bad, the CLI flags are not stable, you should use the API". So I swtiched to using the API and then hell started freezing over (because of assumptions about the classloader layout, because of the worker API, ...).

So I do plan to rewrite this plugin and switch back to the CLI, which should help in this case. In the meantime, if the Maven plugin works for you, go ahead. A small note though: the jmh task which runs the benchmarks is a convenience task. If you want to get accurate numbers, you shouldn't run it and don't let Gradle (or Maven) execute them because it means you have 2 concurrent Java processes which may interfere with the measurements (therefore, generating the jar and running yourself from CLI is better).

melix commented 3 years ago

Release 0.6.0 is out. It requires Gradle 6.8+ and supports feature previews. Take a look at this test to see how to enable feature previews:

https://github.com/melix/jmh-gradle-plugin/blob/master/src/funcTest/resources/java-project-with-feature-previews/build.gradle

Version 0.6 has changed plugin id, it's now me.champeau.jmh.