JMH error Annotation generator had thrown the exception. java.lang.ClassCastException
Vipin Sharma
vipinsharma85 at gmail.com
Mon Mar 2 19:05:27 UTC 2020
Hi Aleksey,
To rule out the possibility of the mistake I did and to share the minimum
code to reproduce this problem I followed steps from
https://openjdk.java.net/projects/code-tools/jmh/ and created a new project.
Created jmh maven project using below
mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=org.openjdk.jmh \
-DarchetypeArtifactId=jmh-java-benchmark-archetype \
-DgroupId=org.sample \
-DartifactId=test \
-Dversion=1.0
Following is the sample java class get from maven project.
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks.
Edit as needed.
// Put your benchmark code here.
}
}
I used command "mvn clean install" to prepare benchmarks.jar and then
executed that using "java -jar target/benchmarks.jar", below is output :
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.openjdk.jmh.util.Utils
(file:/home/vipin/IdeaProjects/jmh1/test/target/benchmarks.jar) to field
java.io.Console.cs
WARNING: Please consider reporting this to the maintainers of
org.openjdk.jmh.util.Utils
WARNING: Use --illegal-access=warn to enable warnings of further illegal
reflective access operations
WARNING: All illegal access operations will be denied in a future release
# JMH version: 1.23
# VM version: JDK 14, OpenJDK 64-Bit Server VM, 14+36-1461
# VM invoker: /usr/lib/jvm/java-14-openjdk-amd64/bin/java
# VM options: <none>
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testMethod
# Run progress: 0.00% complete, ETA 00:08:20
# Fork: 1 of 5
# Warmup Iteration 1: 824032288.673 ops/s
# Warmup Iteration 2: 818595435.995 ops/s
# Warmup Iteration 3: 815723037.473 ops/s
# Warmup Iteration 4: 822699099.164 ops/s
.................................................................
a few more lines ...
Above example doesn't have main method with Runner so a similar case I
picked up to report compilation error.
Following is one more example with the Runner, it works fine when it has
only one benchmark method test, it shows compilation error after adding
method testEnumSet. There is no error if I remove @Benchmark annotation
from testEnumSet.
package com.vipin.jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class EnumSetBenchmark {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(EnumSetBenchmark.class.getSimpleName())
.threads(1)
.forks(1)
.build();
new Runner(opt).run();
}
@Benchmark
public double test(){
return 0;
}
@Benchmark
public static <E extends Enum<E>> EnumSet<E> testEnumSet(E param) {
return null;
}
}
Regards,
Vipin
On Mon, Mar 2, 2020 at 11:15 PM Aleksey Shipilev <shade at redhat.com> wrote:
> On 3/2/20 6:28 PM, Vipin Sharma wrote:
> > package org.sample;import org.openjdk.jmh.annotations.Benchmark;import
> > java.util.EnumSet;public class MyBenchmark {
> > @Benchmark
> > public static <E extends Enum<E>> EnumSet<E> test(E param) {
> > return null;
> > }}
>
> > COMPILATION ERROR : [INFO]
> > -------------------------------------------------------------[ERROR]
> > Annotation generator had thrown the exception.
> > java.lang.ClassCastException: class
> > com.sun.tools.javac.code.Symbol$TypeVariableSymbol cannot be cast to
> > class javax.lang.model.element.TypeElement
> >
> > Is there anything wrong with the benchmark code?
>
> Plenty. Think about it for a minute: JMH would need to call @Benchmark
> method. What it should call
> that method *with*? JMH would normally complain "param" is something
> weird, but it crashed before
> that happened. Even after JMH would fix that part, the benchmark would
> still be incorrect.
>
> --
> Thanks,
> -Aleksey
>
>
More information about the jmh-dev
mailing list