/*******************************************************************************
 * Copyright 2017 Albert Shun-Dat Chan
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 * the License for the specific language governing permissions and limitations under the License.
 *******************************************************************************/
package com.github.javalbert;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
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 com.github.javalbert.reflection.ClassAccessFactory;
import com.github.javalbert.reflection.FieldAccess;

public class FasterThanReflectionBenchmark {
	/* START Field access */
	
	@State(Scope.Thread)
	public static class FieldAccessDirectState {
		public Foo foo = new Foo();
	}
	
	@State(Scope.Thread)
	public static class FieldAccessFasterThanReflectionState {
		public Foo foo = new Foo();
		public FieldAccess<Foo> fieldAccess;
		public int fieldIndex;
		
		@Setup(Level.Trial)
        public void doSetup() {
			fieldAccess = ClassAccessFactory.get(Foo.class);
			fieldIndex = fieldAccess.fieldIndex("intVal");
        }
	}
	
	@State(Scope.Thread)
	public static class FieldAccessReflectionApiState {
		public Foo foo = new Foo();
		public Field intValField;
		
		@Setup(Level.Trial)
        public void doSetup() {
            try {
				intValField = Foo.class.getDeclaredField("intVal");
	            intValField.setAccessible(true);
			} catch (NoSuchFieldException | SecurityException e) {
				throw new RuntimeException(e);
			}
        }
	}
	
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@BenchmarkMode(Mode.AverageTime)
    @Benchmark
    public int testFieldAccessDirect(FieldAccessDirectState state) {
    	return state.foo.intVal;
    }
	
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@BenchmarkMode(Mode.AverageTime)
    @Benchmark
    public int testFieldAccessFasterThanReflection(FieldAccessFasterThanReflectionState state) {
    	return state.fieldAccess.getIntField(state.foo, state.fieldIndex);
    }
	
	@OutputTimeUnit(TimeUnit.NANOSECONDS)
	@BenchmarkMode(Mode.AverageTime)
    @Benchmark
    public int testFieldAccessReflectionApi(FieldAccessReflectionApiState state)
    		throws IllegalArgumentException, IllegalAccessException {
		return state.intValField.getInt(state.foo);
    }
	
	/* END Field access */
}

@@ -1684,32 +1684,26 @@
 evel.Trial)%0A
-        
+%09%09
 public void 
@@ -1816,24 +1816,18 @@
 tVal%22);%0A
-        
+%09%09
 %7D%0A%09%7D%0A%09%0A%09
@@ -1983,24 +1983,18 @@
 .Trial)%0A
-        
+%09%09
 public v
@@ -2009,28 +2009,19 @@
 tup() %7B%0A
-            
+%09%09%09
 try %7B%0A%09%09
@@ -2079,20 +2079,11 @@
 );%0A%09
-            
+%09%09%09
 intV
@@ -2213,16 +2213,10 @@
 %09%09%7D%0A
-        
+%09%09
 %7D%0A%09%7D
@@ -2283,36 +2283,33 @@
 de.AverageTime)%0A
-    
+%09
 @Benchmark%0A    p
@@ -2295,36 +2295,33 @@
 me)%0A%09@Benchmark%0A
-    
+%09
 public int testF
@@ -2361,36 +2361,33 @@
 tState state) %7B%0A
-    
+%09
 %09return state.fo
@@ -2396,20 +2396,17 @@
 intVal;%0A
-    
+%09
 %7D%0A%09%0A%09@Ou
@@ -2466,36 +2466,33 @@
 de.AverageTime)%0A
-    
+%09
 @Benchmark%0A    p
@@ -2478,36 +2478,33 @@
 me)%0A%09@Benchmark%0A
-    
+%09
 public int testF
@@ -2580,20 +2580,17 @@
 tate) %7B%0A
-    
+%09
 %09return 
@@ -2649,20 +2649,17 @@
 Index);%0A
-    
+%09
 %7D%0A%09%0A%09@Ou
@@ -2727,20 +2727,17 @@
 geTime)%0A
-    
+%09
 @Benchma
@@ -2739,20 +2739,17 @@
 nchmark%0A
-    
+%09
 public i
@@ -2817,20 +2817,17 @@
  state)%0A
-    
+%09
 %09%09throws
@@ -2928,12 +2928,9 @@
 o);%0A
-    
+%09
 %7D%0A%09%0A

Fix tabs