TimeMeasure是我以前写的一个java 辅助类,开发人员可以用它们在控制台打印任意两个步骤之间的时间消耗。中途可以pause这个时间计数方法,然后再resume回来,pause的时间是不计入到实际方法消耗的时间的。使用方法见TimeMeasure的main方法,另外如果你愿意,可以拓展它用log4j或者其他方式输出到文件。
TimeMeasure.java
public class TimeMeasure {
// PTODO create junit test class
private static HashMap<String, TimeStack> timers;
private static int indent = 0;
/**
* measureActive is true by default. A true value means that all methods calls are processed else no one.
*/
public static boolean measureActive = true;
/**
* display is true by default. A true value means that all informations are displayed.
*/
public static boolean display = true;
public static boolean displaySteps = true;
/**
*
* DOC hywang Comment method "start".
*
* @param idTimer
*/
public static void begin(String idTimer) {
if (!measureActive) {
return;
}
init();
if (timers.containsKey(idTimer)) {
if (display) {
System.out.println(indent(indent) + "Warning (start): timer " + idTimer + " already exists"); //$NON-NLS-1$ //$NON-NLS-2$
}
} else {
indent++;
TimeStack times = new TimeStack();
timers.put(idTimer, times);
if (display) {
System.out.println(indent(indent) + "Start '" + idTimer + "' ..."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
/**
*
* DOC hywang Comment method "end".
*
* @param idTimer
* @return total elapsed time since start in ms
*/
public static long end(String idTimer) {
if (!measureActive) {
return 0;
}
init();
if (!timers.containsKey(idTimer)) {
if (display) {
System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " doesn't exist"); //$NON-NLS-1$ //$NON-NLS-2$
}
return -1;
} else {
TimeStack timeStack = timers.get(idTimer);
timers.remove(idTimer);
long elapsedTimeSinceLastRequest = timeStack.getLastStepElapsedTime();
if (display && displaySteps) {
System.out.println(indent(indent) + "End '" + idTimer + "', elapsed time since last request: " //$NON-NLS-1$ //$NON-NLS-2$
+ elapsedTimeSinceLastRequest + " ms "); //$NON-NLS-1$
}
long totalElapsedTime = timeStack.getTotalElapsedTime();
if (display) {
System.out.println(indent(indent) + "End '" + idTimer + "', total elapsed time: " + totalElapsedTime + " ms "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
indent--;
return totalElapsedTime;
}
}
/**
*
* DOC hywang Comment method "timeSinceBegin".
*
* @param idTimer
* @return total elapsed time since start in ms
*/
public static long timeSinceBegin(String idTimer) {
if (!measureActive) {
return 0;
}
init();
if (!timers.containsKey(idTimer)) {
if (display) {
System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " does'nt exist"); //$NON-NLS-1$ //$NON-NLS-2$
}
return -1;
} else {
long time = timers.get(idTimer).getTotalElapsedTime();
if (display) {
System.out.println(indent(indent) + "-> '" + idTimer + "', elapsed time since start: " + time + " ms "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return time;
}
}
/**
*
* DOC hywang Comment method "timeStep".
*
* @param idTimer
* @return elapsed time since previous step in ms
*/
public static long step(String idTimer, String stepName) {
if (!measureActive) {
return 0;
}
init();
if (!timers.containsKey(idTimer)) {
if (display) {
System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " does'nt exist"); //$NON-NLS-1$ //$NON-NLS-2$
}
return -1;
} else {
TimeStack timeStack = timers.get(idTimer);
timeStack.addStep();
/*
* trace the timeline of every step,problem is that the code below " Calendar ca = Calendar.getInstance();
* Date now = ca.getTime();" will cost almost 13ms~15ms
*/
long time = timeStack.getLastStepElapsedTime();
if (display && displaySteps) {
String timerStepName = idTimer + "', step name '" + stepName;
System.out.println(indent(indent)
+ "-> '" + timerStepName + "', elapsed time since previous step: " + time + " ms "); //$NON-NLS-1$ //$NON-NLS-2$
}
return time;
}
}
public static void pause(String idTimer) {
if (!measureActive) {
return;
}
init();
if (!timers.containsKey(idTimer)) {
if (display) {
System.out.println(indent(indent) + "Warning (end): timer " + idTimer + " does'nt exist"); //$NON-NLS-1$ //$NON-NLS-2$
}
return;
} else {
TimeStack time = timers.get(idTimer);
// long time = times.getElapsedTimeSinceLastRequest();
time.pause();
if (display) {
}
}
}
public static void resume(String idTimer) {
if (!measureActive) {
return;
}
init();
if (!timers.containsKey(idTimer)) {
begin(idTimer);
return;
} else {
TimeStack times = timers.get(idTimer);
long time = times.getLastStepElapsedTime();
times.resume();
if (display) {
}
}
}
/**
* DOC hywang Comment method "init".
*/
private static void init() {
if (timers == null) {
timers = new HashMap<String, TimeStack>();
}
}
public static String indent(final int i) {
StringBuilder stringBuilder = new StringBuilder();
for (int j = 0; j < i; j++) {
stringBuilder.append(" "); //$NON-NLS-1$
}
return stringBuilder.toString();
}
public static void main(String[] args) {
try {
TimeMeasure.begin("a"); //开始一个计时
Thread.sleep(500);
TimeMeasure.step("a", "1"); //开第一个步骤
Thread.sleep(800);
TimeMeasure.pause("a");//暂停计时
Thread.sleep(600);//由于暂停了计时,这600MS不计入a的总时间
TimeMeasure.step("a", "2");//开始第二步
TimeMeasure.resume("a"); //计时继续
Thread.sleep(2000);
TimeMeasure.end("a");//结束计时
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
TimeStack.java
class TimeStack {
long elapsedTime;
long previousTimeResume;
long previousElapsedTimeSinceLastStep;
long previousStepTime;
boolean isPaused = false;
private boolean testMode = false;
private int testModeIndex;
private long currentElapsedTimeSinceLastStep;
public TimeStack() {
previousStepTime = previousTimeResume = getCurrentTime();
}
public void setTestMode(boolean testMode) {
this.testMode = testMode;
}
public long getTotalElapsedTime() {
if (isPaused) {
return elapsedTime;
} else {
return elapsedTime + getCurrentTime() - previousTimeResume;
}
}
public long getLastStepElapsedTime() {
return previousElapsedTimeSinceLastStep;
}
public void pause() {
if (isPaused) {
new Exception("Pause can't be done").printStackTrace();
} else {
long currentTime = getCurrentTime();
elapsedTime += currentTime - previousTimeResume;
currentElapsedTimeSinceLastStep += currentTime - previousStepTime;
isPaused = true;
}
}
private long getCurrentTime() {
int[] times = { 0, 20, 50, 120, 230, 370, 390 };
if (testMode) {
int time = times[testModeIndex++];
return time;
} else {
return System.currentTimeMillis();
}
}
public void resume() {
long currentTime = getCurrentTime();
previousStepTime = previousTimeResume = currentTime;
isPaused = false;
}
public void addStep() {
long currentTime = getCurrentTime();
long tempElapsedTime = currentTime - previousStepTime;
if (isPaused) {
previousElapsedTimeSinceLastStep = currentElapsedTimeSinceLastStep;
} else {
previousElapsedTimeSinceLastStep = currentElapsedTimeSinceLastStep + tempElapsedTime;
}
currentElapsedTimeSinceLastStep = 0;
previousStepTime = currentTime;
}
}