Skip to content

Instantly share code, notes, and snippets.

@rponte
Forked from garcia-jj/InvokerReflectionTest.java
Created October 8, 2013 14:31
Show Gist options
  • Select an option

  • Save rponte/6885647 to your computer and use it in GitHub Desktop.

Select an option

Save rponte/6885647 to your computer and use it in GitHub Desktop.
package br.com.caelum.vraptor.benchmark;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.logic.BlackHole;
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class InvokerReflectionTest {
private Method method;
private MethodHandle mhandle;
static class Dog {
public String run() {
return "runing";
}
}
@Setup
public void setup()
throws Exception {
method = Dog.class.getDeclaredMethod("run");
mhandle = new MethodHandleFactory().create(Dog.class, method);
}
@GenerateMicroBenchmark
public void withReflection(BlackHole bh)
throws Throwable {
bh.consume(method.invoke(new Dog()));
}
@GenerateMicroBenchmark
public void withInvoker(BlackHole bh)
throws Throwable {
bh.consume(mhandle.invokeExact((Object) new Dog(), new Object[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment