问题描述
我想使用 JMH (一种OpenJDK微基准工具),并带有 gradle 强>.但是,我正在编译时得到NPE.另一方面,从maven使用时,JMH可以工作.
I want to use JMH, an OpenJDK microbenchmark tool, with gradle. However, Im getting the NPE on compilation. On the other hand, JMH works when using from maven.
我没有发布任何 build.gradle
,因为它是基本的-应用Java插件并添加对JHM工具的依赖项( org.openjdk.jmh:jmh-core:0.2
).
I am not posting any build.gradle
as it is basic - apply java plugin and add dependency on JHM tool (org.openjdk.jmh:jmh-core:0.2
).
我尝试了此处写的内容,
我还需要做什么?我认为设置代理会有所帮助,但我仍然没有弄清楚.
What else I have to do? I think something with setting the agent, but I still didnt figure it out.
例外:
:compileJava
java.lang.NullPointerException
at org.openjdk.jmh.processor.internal.GenerateMicroBenchmarkProcessor.validMethodSignature(GenerateMicroBenchmarkProcessor.java:502)
推荐答案
刚刚完成了我的"杰作".没有uber-jars,没有插件,代码库与main&测试时,基准测试编译已挂接到主程序,但不会在主流生命周期中自动运行.简单,明确且易于破解的香草gradle.
Just finished my "masterpiece". No uber-jars, no plugins, code base separated from main & test, benchmarks compilation hooked to main, but does not run automatically in the mainstream lifecycle. Simple, explicit, and hackable, vanilla gradle.
我直接从IntelliJ运行它,要在一个盒子上运行,您可能需要uber-jar back:-)
I run it directly from IntelliJ, to run on a box you probably will need the uber-jar back :-)
在进行此操作之前,我花了很多时间来尝试使该插件正常工作,但是按照我的口味,它实在是太笨拙了.
Before doing it I have spent a fair amount of time trying to get that plugin work, but it's way too clunky for my taste.
下面的分步细分.
定义一个名为 jmh
的新 sourceSet
,其类路径与主 sourceSet
Define a new sourceSet
called jmh
with classpath hooked to that of the main sourceSet
sourceSets {
jmh {
java.srcDirs = ['src/jmh/java']
scala.srcDirs = ['src/jmh/scala']
resources.srcDirs = ['src/jmh/resources']
compileClasspath += sourceSets.main.runtimeClasspath
}
}
为其定义依赖项(至少JMH及其注释处理器).
dependencies {
...
jmhImplementation 'org.openjdk.jmh:jmh-core:1.21'
jmhImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.21'
}
定义类型为 JavaExec
Define a task jmh
of type JavaExec
task jmh(type: JavaExec, dependsOn: jmhClasses) {
main = 'org.openjdk.jmh.Main'
classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath
}
挂钩 jmhClasses
任务在 classes
之后运行,以确保基准与其他代码一起编译
Hook jmhClasses
task to run after classes
to make sure benchmarks are compiled with the rest of the code
classes.finalizedBy(jmhClasses)
这篇关于如何与gradle一起使用JMH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!