/*
 * Copyright (C) 2008  Reto Schuettel, Robin Stocker
 *
 * IFS Institute for Software, HSR Rapperswil, Switzerland
 *
 */

package ch.hsr.ifs.pystructure.typeinference.inferencer.logger;

import ch.hsr.ifs.pystructure.typeinference.evaluators.base.AbstractEvaluator;
import ch.hsr.ifs.pystructure.typeinference.goals.base.IGoal;

public class StatsLogger implements IGoalEngineLogger {

	private IGoal currentGoal;
	private int currentSubGoalsCounter;
	private StringBuilder out;
	private int rootGoalsCounter;
	private int subGoalsCounter;
	private long start;
	private boolean showRootGoalStats;
	
	public StatsLogger(boolean showRootGoalStatsl) {
		this.showRootGoalStats = showRootGoalStatsl;
		this.out = new StringBuilder();
		this.rootGoalsCounter = 0;
		this.subGoalsCounter = 0;
		
		this.start = System.currentTimeMillis();
	}
	
	public StatsLogger() {
		this(true);
	}

	public void evaluationStarted(IGoal rootGoal) {
		assert currentGoal == null;
		this.currentGoal = rootGoal;
		this.currentSubGoalsCounter = 0;
		this.rootGoalsCounter++;
	}

	public void evaluationFinished(IGoal rootGoal) {
		assert rootGoal == currentGoal;
		out.append("" + currentGoal + "." + currentSubGoalsCounter + "\n");
		this.currentGoal = null;
		this.currentSubGoalsCounter = 0;

	}

	public void goalCreated(IGoal goal, AbstractEvaluator creator, AbstractEvaluator evaluator) {
		currentSubGoalsCounter++;
		subGoalsCounter++;
	}

	public void goalFinished(IGoal goal, AbstractEvaluator evaluator) {
	}

	public void shutdown() {
		long delta = System.currentTimeMillis() - start;
		
		System.out.println("");
		System.out.println("Statistics");
		System.out.println("");
		
		if (showRootGoalStats) {
			System.out.println(out);
		}
		
		System.out.println("Total root goals: " + rootGoalsCounter);
		System.out.println("Total subgoals: " + subGoalsCounter);
		if (rootGoalsCounter != 0) {
			System.out.println("Average subgoals / goal: " + subGoalsCounter / rootGoalsCounter);
		}
		System.out.println();
		System.out.println("Time: " + delta / 1000.0 + "s");
		if (subGoalsCounter != 0) {
			System.out.println("Average time / subgoal: " + delta / subGoalsCounter);
		}
		if  (rootGoalsCounter != 0) {
			System.out.println("Average time / root goal: " + delta / rootGoalsCounter);
		}
		System.out.println("Average subgoal / s: " + subGoalsCounter * 1000L / delta);
		System.out.println("Average rootgoal / s: " + rootGoalsCounter * 1000L / delta);
		System.out.println("(Note: Benchmarking might be broken on Windows due to some problems with its timer.)");
		
	}
	
}

@@ -184,16 +184,45 @@
 ogger;%0A%0A
+import java.io.PrintStream;%0A%0A
 import c
@@ -412,16 +412,69 @@
 gger %7B%0A%0A
+%09private static final PrintStream OUT = System.err;%0A%0A
 %09private
@@ -553,19 +553,29 @@
 Builder 
-out
+rootGoalStats
 ;%0A%09priva
@@ -792,19 +792,29 @@
 %0A%09%09this.
-out
+rootGoalStats
  = new S
@@ -1241,19 +1241,29 @@
 Goal;%0A%09%09
-out
+rootGoalStats
 .append(
@@ -1685,34 +1685,27 @@
 start;%0A%09%09%0A%09%09
-System.out
+OUT
 .println(%22%22)
@@ -1704,34 +1704,27 @@
 ntln(%22%22);%0A%09%09
-System.out
+OUT
 .println(%22St
@@ -1733,34 +1733,27 @@
 istics%22);%0A%09%09
-System.out
+OUT
 .println(%22%22)
@@ -1791,30 +1791,33 @@
 %0A%09%09%09
-System.out.println(out
+OUT.println(rootGoalStats
 );%0A%09
@@ -1824,26 +1824,19 @@
 %09%7D%0A%09%09%0A%09%09
-System.out
+OUT
 .println
@@ -1876,34 +1876,27 @@
 Counter);%0A%09%09
-System.out
+OUT
 .println(%22To
@@ -1961,34 +1961,27 @@
  != 0) %7B%0A%09%09%09
-System.out
+OUT
 .println(%22Av
@@ -2046,34 +2046,27 @@
 ter);%0A%09%09%7D%0A%09%09
-System.out
+OUT
 .println();%0A
@@ -2063,34 +2063,27 @@
 rintln();%0A%09%09
-System.out
+OUT
 .println(%22Ti
@@ -2142,34 +2142,27 @@
  != 0) %7B%0A%09%09%09
-System.out
+OUT
 .println(%22Av
@@ -2202,32 +2202,39 @@
  subGoalsCounter
+ + %22ms%22
 );%0A%09%09%7D%0A%09%09if  (ro
@@ -2259,26 +2259,19 @@
 0) %7B%0A%09%09%09
-System.out
+OUT
 .println
@@ -2318,32 +2318,39 @@
 rootGoalsCounter
+ + %22ms%22
 );%0A%09%09%7D%0A%09%09System.
@@ -2342,26 +2342,19 @@
 ;%0A%09%09%7D%0A%09%09
-System.out
+OUT
 .println
@@ -2394,33 +2394,32 @@
 lsCounter * 1000
-L
  / delta);%0A%09%09Sys
@@ -2411,34 +2411,27 @@
 / delta);%0A%09%09
-System.out
+OUT
 .println(%22Av
@@ -2493,121 +2493,8 @@
 a);%0A
-%09%09System.out.println(%22(Note: Benchmarking might be broken on Windows due to some problems with its timer.)%22);%0A%09%09%0A
 %09%7D%0A%09

Add time unit.