
Author: ceki Date: Thu Oct 4 11:44:09 2007 New Revision: 1594 Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java Log: minor refactoring of the StopWatch class Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java ============================================================================== --- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java (original) +++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java Thu Oct 4 11:44:09 2007 @@ -4,30 +4,41 @@ public class StopWatch { - private static final int STARTED = 1; - private static final int STOPPED = 2; - private static final int NANOS_IN_ONE_MICROSECOND = 1000; - private static final int NANOS_IN_ONE_MILLISECOND = 1000*1000; - private static final int NANOS_IN_ONE_SECOND = 1000*1000*1000; private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.000"); + 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; + + enum Status { + STARTED, STOPPED; + } + + enum DurationUnit { + NANOSECOND, MICROSECOND, MILLISSECOND, SECOND; + } + final String name; final long startTime; long stopTime; - int status; + Status status; - StopWatch(String name) { + public StopWatch(String name) { this.name = name; this.startTime = System.nanoTime(); - this.status = STARTED; + this.status = Status.STARTED; } - StopWatch stop() { - this.status = STOPPED; - this.stopTime = System.nanoTime(); - return this; + public StopWatch stop() { + 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(); @@ -41,22 +52,24 @@ break; case STOPPED: buf.append("STOPPED at "); - if(getResultInNanos() < 10*NANOS_IN_ONE_MICROSECOND) { + switch (selectDurationUnitForDisplay(getResultInNanos())) { + case NANOSECOND: buf.append(getResultInNanos()); buf.append(" nanoseconds."); - } - else if (getResultInNanos() < 10*NANOS_IN_ONE_MILLISECOND) { + break; + case MICROSECOND: buf.append(getResultInMicros()); buf.append(" microseconds."); - } else if (getResultInNanos() < 5*NANOS_IN_ONE_SECOND) { - + break; + case MILLISSECOND: buf.append(getResultInMillis()); buf.append(" milliseconds."); - } else { + break; + case SECOND: double seconds = getResultInSeconds(); buf.append(DECIMAL_FORMAT.format(seconds)); buf.append(" seconds."); - + break; } break; default: @@ -65,8 +78,20 @@ return buf.toString(); } + DurationUnit selectDurationUnitForDisplay(long durationInNanos) { + if (durationInNanos < 10L * NANOS_IN_ONE_MICROSECOND) { + return DurationUnit.NANOSECOND; + } else if (durationInNanos < 10L * NANOS_IN_ONE_MILLISECOND) { + return DurationUnit.MICROSECOND; + } else if (durationInNanos < 5L * NANOS_IN_ONE_SECOND) { + return DurationUnit.MILLISSECOND; + } else { + return DurationUnit.SECOND; + } + } + public final long getResultInNanos() { - if (status == STARTED) { + if (status == Status.STARTED) { return 0; } else { return stopTime - startTime; @@ -80,7 +105,7 @@ public final long getResultInMillis() { return getResultInNanos() / NANOS_IN_ONE_MILLISECOND; } - + public final double getResultInSeconds() { return ((double) getResultInNanos() / NANOS_IN_ONE_SECOND); } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java Thu Oct 4 11:44:09 2007 @@ -18,11 +18,50 @@ public void testBasic() throws InterruptedException { StopWatch sw = new StopWatch("testBasic"); + + { + long d = sw.startTime+10; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("10 nanoseconds.")); + } - Thread.sleep(55100); - System.out.println(sw.stop().toString()); - - + { + long d = sw.startTime+9*StopWatch.NANOS_IN_ONE_MICROSECOND; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("9000 nanoseconds.")); + } + + { + long d = sw.startTime+11*StopWatch.NANOS_IN_ONE_MICROSECOND; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("11 microseconds.")); + } + { + long d = sw.startTime+9*StopWatch.NANOS_IN_ONE_MILLISECOND; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("9000 microseconds.")); + } + { + long d = sw.startTime+3*StopWatch.NANOS_IN_ONE_SECOND; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("3000 milliseconds.")); + } + { + long d = sw.startTime+6*StopWatch.NANOS_IN_ONE_SECOND; + String s = sw.stop(d).toString(); + assertTrue(s.endsWith("6.000 milliseconds.")); + } } + public void testSelectDurationUnitForDisplay() throws InterruptedException { + StopWatch sw = new StopWatch("testBasic"); + assertEquals(StopWatch.DurationUnit.NANOSECOND, sw.selectDurationUnitForDisplay(10)); + assertEquals(StopWatch.DurationUnit.NANOSECOND, sw.selectDurationUnitForDisplay(9*StopWatch.NANOS_IN_ONE_MICROSECOND)); + assertEquals(StopWatch.DurationUnit.MICROSECOND, sw.selectDurationUnitForDisplay(11*StopWatch.NANOS_IN_ONE_MICROSECOND)); + assertEquals(StopWatch.DurationUnit.MICROSECOND, sw.selectDurationUnitForDisplay(9*StopWatch.NANOS_IN_ONE_MILLISECOND)); + assertEquals(StopWatch.DurationUnit.MILLISSECOND, sw.selectDurationUnitForDisplay(11*StopWatch.NANOS_IN_ONE_MILLISECOND)); + assertEquals(StopWatch.DurationUnit.MILLISSECOND, sw.selectDurationUnitForDisplay(3*StopWatch.NANOS_IN_ONE_SECOND)); + assertEquals(StopWatch.DurationUnit.SECOND, sw.selectDurationUnitForDisplay(6*StopWatch.NANOS_IN_ONE_SECOND)); + } + }