[logback-dev] svn commit: r2291 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic36

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sun Jun 14 21:48:08 CEST 2009


Author: ceki
Date: Sun Jun 14 21:48:08 2009
New Revision: 2291

Modified:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic36/TestDateFormat.java

Log:
Guarded versions of the tests so as to resemble the code found the logback.

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic36/TestDateFormat.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic36/TestDateFormat.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic36/TestDateFormat.java	Sun Jun 14 21:48:08 2009
@@ -10,18 +10,21 @@
   public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
   static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
 
-  static long RUN_LENGTH = 1000*1000;
+  static long RUN_LENGTH = 1000 * 1000;
 
   public static void main(String[] args) {
     for (int i = 0; i < 5; i++) {
       doRawJoda();
-      doRawSimpleDateFormat();
-      doSyncronizedSimpleDateFormat();
+      doRawSDF();
+      doGuardedJoda();
+      doGuardedSDF();
     }
 
-    print("Raw Joda:              ", doRawJoda());
-    print("Sync SimpleDateFormat: ", doSyncronizedSimpleDateFormat());
-    print("Raw SimpleDateFormat:  ", doRawSimpleDateFormat());
+    print("Raw Joda:     ", doRawJoda());
+    print("Raw SDF:      ", doRawSDF());
+    print("Guarded Joda: ", doGuardedJoda());
+    print("Guarded SDF:  ", doGuardedSDF());
+
   }
 
   static void print(String msg, double avg) {
@@ -38,7 +41,23 @@
     return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
   }
 
-  static double doRawSimpleDateFormat() {
+  static double doGuardedJoda() {
+    DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN);
+    long start = System.nanoTime();
+    long cache = 0;
+    for (int i = 0; i < RUN_LENGTH; ++i) {
+      synchronized (jodaFormat) {
+        long now = System.currentTimeMillis();
+        if (cache != now) {
+          cache = now;
+          jodaFormat.print(now);
+        }
+      }
+    }
+    return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
+  }
+
+  static double doRawSDF() {
     SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
     long timeInMillis = new Date().getTime();
     long start = System.nanoTime();
@@ -48,16 +67,19 @@
     return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
   }
 
-  static double doSyncronizedSimpleDateFormat() {
+  static double doGuardedSDF() {
     SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
-    long timeInMillis = new Date().getTime();
     long start = System.nanoTime();
+    long cache = 0;
     for (int i = 0; i < RUN_LENGTH; ++i) {
       synchronized (simpleFormat) {
-        simpleFormat.format(timeInMillis);
+        long now = System.currentTimeMillis();
+        if (cache != now) {
+          cache = now;
+          simpleFormat.format(now);
+        }
       }
     }
     return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
   }
-
 }


More information about the logback-dev mailing list