// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import java.util.Random;
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.Warmup;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 1)
@Warmup(iterations = 5, timeUnit = TimeUnit.MILLISECONDS, time = 1000)
@Measurement(iterations = 5, timeUnit = TimeUnit.MILLISECONDS, time = 1000)
public class PerfTestJMH {
    private static Random rng = new Random();
    @Benchmark
    public void testBuiltin() {
        double a = rng.nextDouble() * 1000.0;
        double sqrta = Work.sqrtBuiltin(a);
        if(Math.abs(a - sqrta * sqrta) > 0.0000001) throw new RuntimeException("Ooops");
    }
    @Benchmark
    public void testNewton() {
        double a = rng.nextDouble() * 1000.0;
        double sqrta = Work.sqrtNewton(a);
        if(Math.abs(a - sqrta * sqrta) > 0.0000001) throw new RuntimeException("Ooops");
    }
}
