gpt4 book ai didi

java - 由于 JIT 编译,Caliper 基准测试失败

转载 作者:搜寻专家 更新时间:2023-11-01 01:26:15 25 4
gpt4 key购买 nike

我得到了一个如下所示的 Caliper 基准测试:

public Course timeCourseCreation( int i ) {

return createCourse( i );
}

public Course createCourse( int t ) {

Course course = new Course();

for ( int i = 0 + t; i < this.students + t; i++ ) {
Student student = new Student();
student.setAge( ( int ) ( 100 * Math.random() ) + t );
student.setName( UUID.randomUUID().toString() );
course.getStudents().add( student );
}

for ( int i = 0 + t; i < this.courses + t; i++ ) {
Topic topic = new Topic();
topic.setDescription( UUID.randomUUID().toString() );
topic.setDifficulty( ( int ) ( 10 * Math.random() ) + t );
topic.setName( UUID.randomUUID().toString() );
topic.setRating( ( int ) ( 10 * Math.random() ) + t );
course.getTopics().add( topic );
}

return course;
}

所以,我有一个创建数据结构的方法 (createCourse(...)),我想测量创建这个结构需要多长时间。

但是,当我运行基准测试时,出现以下异常:

Failed to execute java -cp C:\Users\tuhrig\workspace\XmlVsJson\target\classes;C:\Users\tuhrig\.m2\repository\javax\xml\bind\jaxb-api\2.2.11\jaxb-api-2.2.11.jar;C:\Users\tuhrig\.m2\repository\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;C:\Users\tuhrig\.m2\repository\com\google\caliper\caliper\0.5-rc1\caliper-0.5-rc1.jar;C:\Users\tuhrig\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\tuhrig\.m2\repository\com\google\guava\guava\11.0.1\guava-11.0.1.jar;C:\Users\tuhrig\.m2\repository\com\google\code\java-allocation-instrumenter\java-allocation-instrumenter\2.0\java-allocation-instrumenter-2.0.jar;C:\Users\tuhrig\.m2\repository\asm\asm\3.3.1\asm-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-analysis\3.3.1\asm-analysis-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-commons\3.3.1\asm-commons-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-tree\3.3.1\asm-tree-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-util\3.3.1\asm-util-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-xml\3.3.1\asm-xml-3.3.1.jar com.google.caliper.InProcessRunner --warmupMillis 3000 --runMillis 1000 --measurementType TIME --marker //ZxJ/ -Dbenchmark=CourseCreation de.tuhrig.Benchmark
starting Scenario{vm=java, trial=0, benchmark=CourseCreation}
[caliper] [starting warmup]
[caliper] [starting measured section]
[caliper] [done measured section]
[caliper] [starting measured section]
...
Error: Doing 2x as much work didn't take 2x as much time! Is the JIT optimizing away the body of your benchmark?
...
An exception was thrown from the benchmark code.
com.google.caliper.ConfigurationException: Failed to execute java -cp C:\Users\tuhrig\workspace\XmlVsJson\target\classes;C:\Users\tuhrig\.m2\repository\javax\xml\bind\jaxb-api\2.2.11\jaxb-api-2.2.11.jar;C:\Users\tuhrig\.m2\repository\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;C:\Users\tuhrig\.m2\repository\com\google\caliper\caliper\0.5-rc1\caliper-0.5-rc1.jar;C:\Users\tuhrig\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\tuhrig\.m2\repository\com\google\guava\guava\11.0.1\guava-11.0.1.jar;C:\Users\tuhrig\.m2\repository\com\google\code\java-allocation-instrumenter\java-allocation-instrumenter\2.0\java-allocation-instrumenter-2.0.jar;C:\Users\tuhrig\.m2\repository\asm\asm\3.3.1\asm-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-analysis\3.3.1\asm-analysis-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-commons\3.3.1\asm-commons-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-tree\3.3.1\asm-tree-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-util\3.3.1\asm-util-3.3.1.jar;C:\Users\tuhrig\.m2\repository\asm\asm-xml\3.3.1\asm-xml-3.3.1.jar com.google.caliper.InProcessRunner --warmupMillis 3000 --runMillis 1000 --measurementType TIME --marker //ZxJ/ -Dbenchmark=CourseCreation de.tuhrig.Benchmark
at com.google.caliper.Runner.measure(Runner.java:309)
at com.google.caliper.Runner.runScenario(Runner.java:229)
at com.google.caliper.Runner.runOutOfProcess(Runner.java:378)
at com.google.caliper.Runner.run(Runner.java:97)
at com.google.caliper.Runner.main(Runner.java:423)
at de.tuhrig.CaliperRunner.main(CaliperRunner.java:22)

异常告诉我,由于 JIT 编译,基准测试失败了。 那么,我怎样才能阻止这里的 JIT 编译呢?

我已经尝试用随机值初始化数据结构(如您在代码中看到的),我尝试在我的测试中返回一个值(如您在代码中看到的)并且我尝试使用 course 对象,例如在其上调用 course.getSize()。但没有任何效果。

最佳答案

众所周知,JIT 会以可怕的方式扰乱您的代码,从而使所有测量结果绝对不可靠。人们挣扎了很多,试图让事情至少在某种程度上是正确的,但最终写下了类似 JMH 的东西。 .这是一个很好的工具,可以帮助衡量绩效并实际获得有意义的结果。它甚至使您能够 avoid dead code elimination并且有大量samples , 看哪个肯定能帮你解决问题...

但这是错误的方式。它通常是保密的,但是既然你自己想出了正确的方法,我就告诉你。您可以使用 -Xint option 禁用 JIT .那里。不用谢我。

关于java - 由于 JIT 编译,Caliper 基准测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24408828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com