package com.amazon.engebretson.reflection;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(2)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
public class GetMethodBenchmark {

    private static final String METHOD_NAME = "toString";

    /**
     * Searches for Object.toString, on the current class.
     *
     * @return
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Benchmark
    public Method getObjectToString() throws NoSuchMethodException, SecurityException {
        return Object.class.getMethod(METHOD_NAME);
    }

    /**
     * Searches for AbstractMap.toString(), one level up.
     *
     * @return
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Benchmark
    public Method getHashMapToString() throws NoSuchMethodException, SecurityException {
        return HashMap.class.getMethod(METHOD_NAME);
    }
}
