Skip to content

Instantly share code, notes, and snippets.

@garcia-jj
Last active December 24, 2015 06:39
Show Gist options
  • Save garcia-jj/6758893 to your computer and use it in GitHub Desktop.
Save garcia-jj/6758893 to your computer and use it in GitHub Desktop.
Testes de performance entre Reflection e Invoker do Java 7
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