[slf4j-dev] svn commit: r1372 - slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/profiler
ceki at slf4j.org
ceki at slf4j.org
Mon Jul 20 10:19:54 CEST 2009
Author: ceki
Date: Mon Jul 20 10:19:53 2009
New Revision: 1372
Modified:
slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/profiler/Profiler.java
Log:
- indentation changes only
Modified: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/profiler/Profiler.java
==============================================================================
--- slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/profiler/Profiler.java (original)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/profiler/Profiler.java Mon Jul 20 10:19:53 2009
@@ -30,6 +30,7 @@
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
+
// + Profiler [BAS]
// |-- elapsed time [doX] 0 milliseconds.
// |-- elapsed time [doYYYYY] 56 milliseconds.
@@ -40,9 +41,10 @@
// |-- elapsed time [doZ] 21 milliseconds.
// |-- Total elapsed time [BAS] 78 milliseconds.
+
/**
- * A poor man's profiler to measure the time elapsed performing
- * some lengthy task.
+ * A poor man's profiler to measure the time elapsed performing some lengthy
+ * task.
*
* @author Ceki Gülcü
*/
@@ -52,16 +54,16 @@
final static int MIN_SW_NAME_LENGTH = 24;
final static int MIN_SW_ELAPSED_TIME_NUMBER_LENGTH = 9;
-
+
final String name;
final StopWatch globalStopWatch;
- //List<StopWatch> stopwatchList = new ArrayList<StopWatch>();
+ // List<StopWatch> stopwatchList = new ArrayList<StopWatch>();
List<TimeInstrument> childTimeInstrumentList = new ArrayList<TimeInstrument>();
// optional field
ProfilerRegistry profilerRegistry;
-//optional field
+ // optional field
Logger logger;
public Profiler(String name) {
@@ -94,7 +96,8 @@
}
/**
- * Starts a child stop watch and stops any previously started time instruments.
+ * Starts a child stop watch and stops any previously started time
+ * instruments.
*/
public void start(String name) {
stopLastTimeInstrument();
@@ -126,17 +129,17 @@
}
}
-// void stopNestedProfilers() {
-// for (Object child : childTimeInstrumentList) {
-// if (child instanceof Profiler)
-// ((Profiler) child).stop();
-// }
-// }
+ // void stopNestedProfilers() {
+ // for (Object child : childTimeInstrumentList) {
+ // if (child instanceof Profiler)
+ // ((Profiler) child).stop();
+ // }
+ // }
public long elapsedTime() {
return globalStopWatch.elapsedTime();
}
-
+
public TimeInstrument stop() {
stopLastTimeInstrument();
globalStopWatch.stop();
@@ -146,63 +149,68 @@
public TimeInstrumentStatus getStatus() {
return globalStopWatch.status;
}
-
+
/**
* This method is used in tests.
*/
void sanityCheck() throws IllegalStateException {
- if(getStatus() != TimeInstrumentStatus.STOPPED) {
- throw new IllegalStateException("time instrument ["+getName()+" is not stopped");
+ if (getStatus() != TimeInstrumentStatus.STOPPED) {
+ throw new IllegalStateException("time instrument [" + getName()
+ + " is not stopped");
}
-
+
long totalElapsed = globalStopWatch.elapsedTime();
long childTotal = 0;
-
- for(TimeInstrument ti: childTimeInstrumentList) {
+
+ for (TimeInstrument ti : childTimeInstrumentList) {
childTotal += ti.elapsedTime();
- if(ti.getStatus() != TimeInstrumentStatus.STOPPED) {
- throw new IllegalStateException("time instrument ["+ti.getName()+" is not stopped");
+ if (ti.getStatus() != TimeInstrumentStatus.STOPPED) {
+ throw new IllegalStateException("time instrument [" + ti.getName()
+ + " is not stopped");
}
- if(ti instanceof Profiler) {
+ if (ti instanceof Profiler) {
Profiler nestedProfiler = (Profiler) ti;
nestedProfiler.sanityCheck();
}
}
- if(totalElapsed < childTotal) {
- throw new IllegalStateException("children have a higher accumulated elapsed time");
+ if (totalElapsed < childTotal) {
+ throw new IllegalStateException(
+ "children have a higher accumulated elapsed time");
}
}
static String TOP_PROFILER_FIRST_PREFIX = "+";
static String NESTED_PROFILER_FIRST_PREFIX = "|---+";
- static String TOTAL_ELAPSED = " Total ";
+ static String TOTAL_ELAPSED = " Total ";
static String SUBTOTAL_ELAPSED = " Subtotal ";
- static String ELAPSED_TIME = " elapsed time ";
-
+ static String ELAPSED_TIME = " elapsed time ";
public void print() {
System.out.println(toString());
}
-
+
@Override
public String toString() {
DurationUnit du = Util.selectDurationUnitForDisplay(globalStopWatch);
return buildProfilerString(du, TOP_PROFILER_FIRST_PREFIX, TOTAL_ELAPSED, "");
}
-
+
public void log() {
Marker profilerMarker = MarkerFactory.getMarker(PROFILER_MARKER_NAME);
- if(logger == null) {
- throw new NullPointerException("If you invoke the log() method, then you must associate a logger with this profiler.");
+ if (logger == null) {
+ throw new NullPointerException(
+ "If you invoke the log() method, then you must associate a logger with this profiler.");
}
if (logger.isDebugEnabled(profilerMarker)) {
DurationUnit du = Util.selectDurationUnitForDisplay(globalStopWatch);
- String r = buildProfilerString(du, TOP_PROFILER_FIRST_PREFIX, TOTAL_ELAPSED, "");
- logger.debug(profilerMarker, SpacePadder.LINE_SEP+r);
+ String r = buildProfilerString(du, TOP_PROFILER_FIRST_PREFIX,
+ TOTAL_ELAPSED, "");
+ logger.debug(profilerMarker, SpacePadder.LINE_SEP + r);
}
}
-
- private String buildProfilerString(DurationUnit du, String firstPrefix, String label, String indentation) {
+
+ private String buildProfilerString(DurationUnit du, String firstPrefix,
+ String label, String indentation) {
StringBuffer buf = new StringBuffer();
buf.append(firstPrefix);
@@ -212,13 +220,16 @@
buf.append(SpacePadder.LINE_SEP);
for (TimeInstrument child : childTimeInstrumentList) {
if (child instanceof StopWatch) {
- buildStopWatchString(buf, du, ELAPSED_TIME, indentation, (StopWatch) child);
+ buildStopWatchString(buf, du, ELAPSED_TIME, indentation,
+ (StopWatch) child);
} else if (child instanceof Profiler) {
Profiler profiler = (Profiler) child;
- String subString = profiler
- .buildProfilerString(du, NESTED_PROFILER_FIRST_PREFIX, SUBTOTAL_ELAPSED, indentation + " ");
+ String subString = profiler.buildProfilerString(du,
+ NESTED_PROFILER_FIRST_PREFIX, SUBTOTAL_ELAPSED, indentation
+ + " ");
buf.append(subString);
- buildStopWatchString(buf, du, ELAPSED_TIME, indentation, profiler.globalStopWatch);
+ buildStopWatchString(buf, du, ELAPSED_TIME, indentation,
+ profiler.globalStopWatch);
}
}
buildStopWatchString(buf, du, label, indentation, globalStopWatch);
@@ -233,8 +244,7 @@
buf.append(prefix);
SpacePadder.leftPad(buf, "[" + sw.getName() + "]", MIN_SW_NAME_LENGTH);
buf.append(" ");
- String timeStr = Util.durationInDurationUnitsAsStr(sw.elapsedTime(),
- du);
+ String timeStr = Util.durationInDurationUnitsAsStr(sw.elapsedTime(), du);
SpacePadder.leftPad(buf, timeStr, MIN_SW_ELAPSED_TIME_NUMBER_LENGTH);
buf.append(" ");
Util.appendDurationUnitAsStr(buf, du);
More information about the slf4j-dev
mailing list