[logback-dev] svn commit: r1685 - logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sun May 4 20:20:11 CEST 2008


Author: ceki
Date: Sun May  4 20:20:11 2008
New Revision: 1685

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/DurationUnit.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Profiler.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/StopWatch.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Util.java

Log:
renaming folder

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/DurationUnit.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/DurationUnit.java	Sun May  4 20:20:11 2008
@@ -0,0 +1,5 @@
+package ch.qos.logback.classic.stopwatch;
+
+public enum DurationUnit {
+  NANOSECOND, MICROSECOND, MILLISSECOND, SECOND;
+}
\ No newline at end of file

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Profiler.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Profiler.java	Sun May  4 20:20:11 2008
@@ -0,0 +1,137 @@
+package ch.qos.logback.classic.stopwatch;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.pattern.SpacePadder;
+
+// +  Profiler [BAS]
+// |-- elapsed time            [doX]     0 milliseconds.
+// |-- elapsed time        [doYYYYY]    56 milliseconds.
+// |--+ Profiler Y
+//    |-- elapsed time            [doZ]    21 milliseconds.
+//    |-- elapsed time            [doZ]    21 milliseconds.
+//    |-- Total elapsed time        [Y]    78 milliseconds.
+// |-- elapsed time            [doZ]    21 milliseconds.
+// |-- Total elapsed time      [BAS]    78 milliseconds.
+
+public class Profiler {
+
+  final static int MIN_SW_NAME_LENGTH = 10;
+  final static int MIN_SW_ELAPSED_TIME_NUMBER_LENGTH = 6;
+
+  final String name;
+  final StopWatch globalStopWatch;
+
+  List<StopWatch> stopwatchList = new ArrayList<StopWatch>();
+  List<Object> childList = new ArrayList<Object>();
+
+  public Profiler(String name) {
+    this.name = name;
+    this.globalStopWatch = new StopWatch(name);
+  }
+
+  public void start(String name) {
+    stopLastStopWatch();
+    StopWatch childSW = new StopWatch(name);
+    stopwatchList.add(childSW);
+    childList.add(childSW);
+  }
+
+  public Profiler startNested(String name) {
+    Profiler nestedProfiler = new Profiler(name);
+    childList.add(nestedProfiler);
+    return nestedProfiler;
+  }
+
+  StopWatch getLastStopWatch() {
+    if (stopwatchList.size() > 0) {
+      return stopwatchList.get(stopwatchList.size() - 1);
+    } else {
+      return null;
+    }
+  }
+
+  void stopLastStopWatch() {
+    StopWatch last = getLastStopWatch();
+    if (last != null) {
+      last.stop();
+    }
+  }
+
+  void stopNestedProfilers() {
+    for (Object child : childList) {
+      if (child instanceof Profiler)
+        ((Profiler) child).stop();
+    }
+  }
+
+  public Profiler stop() {
+    stopLastStopWatch();
+    stopNestedProfilers();
+    globalStopWatch.stop();
+    return this;
+  }
+
+  public void print() {
+    DurationUnit du = Util.selectDurationUnitForDisplay(globalStopWatch);
+    String r = buildString(du, "+", "");
+    System.out.println(r);
+  }
+
+  private String buildString(DurationUnit du, String prefix, String indentation) {
+    StringBuffer buf = new StringBuffer();
+
+    
+    buf.append(prefix);
+    buf.append(" Profiler [");
+    buf.append(name);
+    buf.append("]");
+    buf.append(Layout.LINE_SEP);
+    for (Object child : childList) {
+      if(child instanceof StopWatch) {
+       buildStringForChildStopWatch(buf, indentation, (StopWatch) child, du);
+      } else if(child instanceof Profiler) {
+        Profiler profiler = (Profiler) child;
+        profiler.stop();
+        String subString = profiler.buildString(du, "|--+", indentation + "   ");
+        buf.append(subString);
+      }
+    }
+    buildStringForGlobalStopWatch(buf, indentation, globalStopWatch, du);
+    return buf.toString();
+  }
+
+  private static void buildStringForChildStopWatch(StringBuffer buf,
+      String indentation, StopWatch sw, DurationUnit du) {
+
+    buf.append(indentation);
+    buf.append("|--");
+    buf.append(" elapsed time       ");
+    SpacePadder.leftPad(buf, "[" + sw.getName() + "]", MIN_SW_NAME_LENGTH);
+    buf.append(" ");
+    String timeStr = Util.durationInDunrationUnitsAsStr(sw.getResultInNanos(),
+        du);
+    SpacePadder.leftPad(buf, timeStr, MIN_SW_ELAPSED_TIME_NUMBER_LENGTH);
+    buf.append(" ");
+    Util.appendDurationUnitAsStr(buf, du);
+    buf.append(Layout.LINE_SEP);
+  }
+
+  private static void buildStringForGlobalStopWatch(StringBuffer buf,
+      String indentation, StopWatch sw, DurationUnit du) {
+    buf.append(indentation);
+    buf.append("|--");
+    buf.append(" Total elapsed time ");
+    SpacePadder.leftPad(buf, "[" + sw.getName() + "]", MIN_SW_NAME_LENGTH);
+    buf.append(" ");
+    String timeStr = Util.durationInDunrationUnitsAsStr(sw.getResultInNanos(),
+        du);
+    SpacePadder.leftPad(buf, timeStr, MIN_SW_ELAPSED_TIME_NUMBER_LENGTH);
+    buf.append(" ");
+    Util.appendDurationUnitAsStr(buf, du);
+    buf.append(Layout.LINE_SEP);
+  }
+
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/StopWatch.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/StopWatch.java	Sun May  4 20:20:11 2008
@@ -0,0 +1,68 @@
+package ch.qos.logback.classic.stopwatch;
+
+
+public class StopWatch {
+
+
+  enum Status {
+    STARTED, STOPPED;
+  }
+  
+  final String name;
+  final long startTime;
+  long stopTime;
+  Status status;
+
+  public StopWatch(String name) {
+    this.name = name;
+    this.startTime = System.nanoTime();
+    this.status = Status.STARTED;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public StopWatch stop() {
+    if(status == Status.STOPPED) {
+      return this;
+    } 
+    return stop(System.nanoTime());
+  }
+
+  public StopWatch stop(long stopTime) {
+    this.status = Status.STOPPED;
+    this.stopTime = stopTime;
+    return this;
+  }
+  
+  @Override
+  public String toString() {
+    StringBuffer buf = new StringBuffer();
+    buf.append("StopWatch [");
+    buf.append(name);
+    buf.append("] ");
+
+    switch (status) {
+    case STARTED:
+      buf.append("STARTED");
+      break;
+    case STOPPED:
+      buf.append("elapsed time: ");
+      buf.append(Util.durationInDunrationUnitsAsStr(getResultInNanos(), DurationUnit.MICROSECOND));
+      break;
+    default:
+      new IllegalStateException("Status " + status + " is not expected");
+    }
+    return buf.toString();
+  }
+
+  public final long getResultInNanos() {
+    if (status == Status.STARTED) {
+      return 0;
+    } else {
+      return stopTime - startTime;
+    }
+  }
+
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Util.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/stopwatch/Util.java	Sun May  4 20:20:11 2008
@@ -0,0 +1,83 @@
+package ch.qos.logback.classic.stopwatch;
+
+import java.text.DecimalFormat;
+
+class Util {
+
+  static final long NANOS_IN_ONE_MICROSECOND = 1000;
+  static final long NANOS_IN_ONE_MILLISECOND = NANOS_IN_ONE_MICROSECOND * 1000;
+  static final long NANOS_IN_ONE_SECOND =NANOS_IN_ONE_MILLISECOND * 1000;
+  private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.000");
+  
+  static DurationUnit selectDurationUnitForDisplay(StopWatch sw) {
+    return selectDurationUnitForDisplay(sw.getResultInNanos());
+  }
+  
+  static DurationUnit selectDurationUnitForDisplay(long durationInNanos) {
+    if (durationInNanos < 10*NANOS_IN_ONE_MICROSECOND) {
+      return DurationUnit.NANOSECOND;
+    } else if (durationInNanos < 10*NANOS_IN_ONE_MILLISECOND) {
+      return DurationUnit.MICROSECOND;
+    } else if (durationInNanos < 10*NANOS_IN_ONE_SECOND) {
+      return DurationUnit.MILLISSECOND;
+    } else {
+      return DurationUnit.SECOND;
+    }
+  }
+  
+  static public double convertToMicros(long nanos) {
+    return (double) nanos / NANOS_IN_ONE_MICROSECOND;
+  }
+
+  static public double convertToMillis(long nanos) {
+    return (double) nanos / NANOS_IN_ONE_MILLISECOND;
+  }
+
+  static public double convertToSeconds(long nanos) {
+    return ((double) nanos / NANOS_IN_ONE_SECOND);
+  }
+  
+  static String durationInDunrationUnitsAsStr(StringBuffer buf, StopWatch sw) {
+    DurationUnit du = selectDurationUnitForDisplay(sw);
+    return durationInDunrationUnitsAsStr(sw.getResultInNanos(), du);
+  }
+  
+  static String durationInDunrationUnitsAsStr(long nanos, DurationUnit durationUnit) {
+    StringBuffer buf = new StringBuffer();
+    switch (durationUnit) {
+    case NANOSECOND:
+      buf.append(nanos);
+      break;
+    case MICROSECOND:
+      double micros = convertToMicros(nanos);
+      buf.append(DECIMAL_FORMAT.format(micros));
+      break;
+    case MILLISSECOND:
+      double millis = convertToMillis(nanos);
+      buf.append(DECIMAL_FORMAT.format(millis));
+      break;
+    case SECOND:
+      double seconds = convertToSeconds(nanos);
+      buf.append(DECIMAL_FORMAT.format(seconds));
+      break;
+    }
+    return buf.toString();
+  }
+  
+  static void appendDurationUnitAsStr(StringBuffer buf, DurationUnit durationUnit) {
+    switch (durationUnit) {
+    case NANOSECOND:
+      buf.append("nanoseconds.");
+      break;
+    case MICROSECOND:
+      buf.append("microseconds.");
+      break;
+    case MILLISSECOND:
+      buf.append("milliseconds.");
+      break;
+    case SECOND:
+      buf.append(" seconds.");
+      break;
+    }
+  }
+}



More information about the logback-dev mailing list