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;
- }
- }