package bpsm.edn.performance;

import bpsm.edn.parser.IOUtil;
import bpsm.edn.parser.Parser;
import bpsm.edn.parser.Parsers;
import bpsm.edn.printer.Printers;


/**
 * This is not exactly a full benchmarking suite
 * 
 * But enough to test efficiency of various operations
 * 
 * @author Mike
 *
 */
public class PerformanceTest {
	private static final int REPEATS=5;
	private static final int BURN_IN=100;
	
	private static final boolean PRINT_INDIVIDUAL_EXECUTIONS = true;
	
	public static void main(String[] args) {
		runBenchmark(testA);
		runBenchmark(testB);
	}
	
	private static void runBenchmark(Benchmark b) {
		for (int i=0; i<BURN_IN; i++) {
			b.benchmark();
		}
		double total=0.0;
		System.out.println("Running benchmark: "+b);
		for (int i=0; i<REPEATS; i++) {
			double d=b.benchmark();
			total+=d;
			if (PRINT_INDIVIDUAL_EXECUTIONS) {
				System.out.println ("Time per iteration: " + (long)d + " ns");
			}
		}
		System.out.println ("AVERAGE time per iteration: " + (long)(total/REPEATS) + " ns");
		System.out.println ();

	}
	
	private static abstract class Benchmark implements Runnable {
		private double benchmark() {
			long time=System.nanoTime();
			run();	
			double ns= (System.nanoTime()-time)/(1.0*getIterations()*REPEATS);	
			return ns;
		}		
		
		public final void run() {
			int max=getIterations();
			for (int i=0; i<max; i++) {
				runIteration();
			}
		}
		
		public abstract void runIteration();

		public int getIterations() {
			return 1;
		}
	}
	
	static final String ednString = IOUtil.stringFromResource("bpsm/edn/edn-sample.txt");
	static final Object ednData = Parsers.newParser(Parsers.defaultConfiguration(), ednString).nextValue();
	
	public static Benchmark testA = new Benchmark() {
		public String toString() {return "Parse edn from String";}
		public int getIterations() {return 100;}
		
		@SuppressWarnings("unused")
		public void runIteration() {
			Parser p=Parsers.newParser(Parsers.defaultConfiguration(), ednString);
			Object o=p.nextValue();
		}
	};
	
	public static Benchmark testB = new Benchmark() {
		public String toString() {return "Print edn to String";}
		public int getIterations() {return 100;}
		
		@SuppressWarnings("unused")
		public void runIteration() {
			String s=Printers.printString(ednData);
		}
	};
	
}

@@ -207,20 +207,16 @@
 g suite%0A
- * %0A
  * But e
@@ -266,16 +266,107 @@
 ons%0A * %0A
+ * TODO: Consider switching to Google's Caliper if we don't mind an eternal dependency%0A * %0A
  * @auth
@@ -377,11 +377,8 @@
 ike%0A
- *%0A
  */%0A
@@ -550,11 +550,12 @@
 S = 
-tru
+fals
 e;%0A%09
@@ -616,17 +616,29 @@
 ark(test
-A
+StringParsing
 );%0A%09%09run
@@ -651,17 +651,29 @@
 ark(test
-B
+StringWriting
 );%0A%09%7D%0A%09%0A
@@ -1816,17 +1816,29 @@
 ark test
-A
+StringParsing
  = new B
@@ -2158,17 +2158,29 @@
 ark test
-B
+StringWriting
  = new B

Tidy code and output