[logback-dev] svn commit: r1592 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core test/java/ch/qos/logback/core
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Fri Sep 14 09:20:03 CEST 2007
Author: ceki
Date: Fri Sep 14 09:20:03 2007
New Revision: 1592
Added:
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:
- Adding StopWatch functionaliry to logback core
Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/StopWatch.java Fri Sep 14 09:20:03 2007
@@ -0,0 +1,87 @@
+package ch.qos.logback.core;
+
+import java.text.DecimalFormat;
+
+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");
+
+ final String name;
+ final long startTime;
+ long stopTime;
+ int status;
+
+ StopWatch(String name) {
+ this.name = name;
+ this.startTime = System.nanoTime();
+ this.status = STARTED;
+ }
+
+ StopWatch stop() {
+ this.status = STOPPED;
+ this.stopTime = System.nanoTime();
+ 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("STOPPED at ");
+ if(getResultInNanos() < 10*NANOS_IN_ONE_MICROSECOND) {
+ buf.append(getResultInNanos());
+ buf.append(" nanoseconds.");
+ }
+ else if (getResultInNanos() < 10*NANOS_IN_ONE_MILLISECOND) {
+ buf.append(getResultInMicros());
+ buf.append(" microseconds.");
+ } else if (getResultInNanos() < 5*NANOS_IN_ONE_SECOND) {
+
+ buf.append(getResultInMillis());
+ buf.append(" milliseconds.");
+ } else {
+ double seconds = getResultInSeconds();
+ buf.append(DECIMAL_FORMAT.format(seconds));
+ buf.append(" seconds.");
+
+ }
+ break;
+ default:
+ new IllegalStateException("Status " + status + " is not expected");
+ }
+ return buf.toString();
+ }
+
+ public final long getResultInNanos() {
+ if (status == STARTED) {
+ return 0;
+ } else {
+ return stopTime - startTime;
+ }
+ }
+
+ public final long getResultInMicros() {
+ return getResultInNanos() / NANOS_IN_ONE_MICROSECOND;
+ }
+
+ public final long getResultInMillis() {
+ return getResultInNanos() / NANOS_IN_ONE_MILLISECOND;
+ }
+
+ public final double getResultInSeconds() {
+ return ((double) getResultInNanos() / NANOS_IN_ONE_SECOND);
+ }
+}
Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/StopWatchTest.java Fri Sep 14 09:20:03 2007
@@ -0,0 +1,28 @@
+package ch.qos.logback.core;
+
+import junit.framework.TestCase;
+
+public class StopWatchTest extends TestCase {
+
+ public StopWatchTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testBasic() throws InterruptedException {
+ StopWatch sw = new StopWatch("testBasic");
+
+ Thread.sleep(55100);
+ System.out.println(sw.stop().toString());
+
+
+ }
+
+}
More information about the logback-dev
mailing list