org.openxma.dsl.common.logging.TimeLogger Maven / Gradle / Ivy
package org.openxma.dsl.common.logging;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Simple Utility to measure execution-times in Java and Extend
*
*/
public class TimeLogger {
/**
* Static instance to be used for static methods which are
* invoked from Extend
*/
private static TimeLogger timeLogger;
private static TimeLogger getInstance() {
if (timeLogger == null) {
timeLogger = new TimeLogger();
}
return timeLogger;
}
/**
* Static start-Method which can be invoked from Extend
*/
public static void startTimeLogger() {
getInstance().clear();
getInstance().add("");
getInstance().root = new TimeMeasurement("All");
getInstance().root.begin();
}
public static void closeTimeLogger() {
getInstance().root.end();
}
/**
* Static addTime-Method which can be invoked from Extend
*/
// public static void addTime(String eventName) {
// getInstance().add(eventName);
// }
/**
* Static print-Method which can be invoked from Extend
*/
public static void printTimeDurations(int stackDepth) {
getInstance().printDurations(stackDepth);
}
List logEntries = new ArrayList();
List measurements = new ArrayList();
TimeMeasurement root = null;
TimeMeasurement activeMeasurement = null;
static HashMap sums = new HashMap();
class TimeLog {
private String eventName;
private long time;
TimeLog(String eventName, long time) {
this.eventName = eventName;
this.time = time;
}
}
/**
* Remove all logged events
*/
public void clear() {
logEntries = new ArrayList();
root = null;
sums = new HashMap();
}
static HashMap getSums() {
return sums;
}
/**
* Add new event to be measured
* @param eventName
*/
public void add(String eventName) {
long t = System.currentTimeMillis();
TimeLog timeLog = new TimeLog(eventName,t);
logEntries.add(timeLog);
}
/**
* Print all logged events
*/
public void printLogEntries() {
for (TimeLog timeLog:logEntries) {
print("Time: "+timeLog.eventName+" = "+timeLog.time);
}
}
/**
* Print the duration of all logged events
*/
public void printDurations(int stackDepth) {
// if (logEntries.size()==0) {
// print("Timelog: no entries");
// return;
// }
// Time begin = new Time(logEntries.get(0).time);
// Time end = new Time(logEntries.get(logEntries.size()-1).time);
// print("Timelog - begin: "+begin.toString());
// for (int i=1;i