package ch.qos.logback.classic.issue.lbclassic36; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.atomic.AtomicLong; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class DateFormatPerfTest { public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS"; static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L; static final DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN); static long cachedTime = 0; static long RUN_LENGTH = 1000 * 1000; static int THREAD_COUNT = 10; public static void main(String[] args) { for (int i = 0; i < 5; i++) { doRawJoda(); doRawSDF(); doJoda(); doGuardedSDF(); } print("Raw Joda: ", doRawJoda()); print("Raw SDF: ", doRawSDF()); print("Cached Joda: ", doJoda()); print("Guarded SDF: ", doGuardedSDF()); printElapsed("Threaded Joda: ", doThreadedJoda()); printElapsed("Threaded SDF: ", doThreadedSDF()); } static void print(String msg, double avg) { System.out.println(msg + " average tick " + avg + " nanoseconds"); } static void printElapsed(String msg, long elapsed) { System.out.println(msg + " elapsed " + elapsed + " ms"); } static double doRawJoda() { DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN); long timeInMillis = new Date().getTime(); long start = System.nanoTime(); for (int i = 0; i < RUN_LENGTH; ++i) { jodaFormat.print(timeInMillis); } return (System.nanoTime() - start) * 1.0d / RUN_LENGTH; } static double doJoda() { long start = System.nanoTime(); for (int i = 0; i < RUN_LENGTH; ++i) { long now = System.currentTimeMillis(); if (!isCached(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(); for (int i = 0; i < RUN_LENGTH; ++i) { simpleFormat.format(timeInMillis); } return (System.nanoTime() - start) * 1.0d / RUN_LENGTH; } static double doGuardedSDF() { SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN); long start = System.nanoTime(); long cache = 0; for (int i = 0; i < RUN_LENGTH; ++i) { synchronized (simpleFormat) { long now = System.currentTimeMillis(); if (cache != now) { cache = now; simpleFormat.format(now); } } } return (System.nanoTime() - start) * 1.0d / RUN_LENGTH; } static long doThreadedJoda() { Thread[] formatThreads = new Thread[THREAD_COUNT]; JodaFormatter formatter = new JodaFormatter(); cachedTime = 0; for (int i = 0; i < THREAD_COUNT; i++) { formatThreads[i] = new DateFormatThread(formatter); } long start = System.currentTimeMillis(); for (Thread thread : formatThreads) { thread.start(); } try { for (Thread thread : formatThreads) { thread.join(); } } catch (Exception ex) { throw new RuntimeException("join failed", ex); } return System.currentTimeMillis() - start; } static long doThreadedSDF() { Thread[] formatThreads = new Thread[THREAD_COUNT]; SDFFormatter formatter = new SDFFormatter(); cachedTime = 0; for (int i = 0; i < THREAD_COUNT; i++) { formatThreads[i] = new DateFormatThread(formatter); } long start = System.currentTimeMillis(); for (Thread thread : formatThreads) { thread.start(); } try { for (Thread thread : formatThreads) { thread.join(); } } catch (Exception ex) { throw new RuntimeException("join failed", ex); } return System.currentTimeMillis() - start; } public static boolean isCached(long now) { if (cachedTime != now) { synchronized(jodaFormat) { if (cachedTime != now) { cachedTime = now; return false; } } } return true; } public static interface Formatter { String format(long dateTime); } public static class JodaFormatter implements Formatter { public String format(long dateTime) { return jodaFormat.print(dateTime); } } public static class SDFFormatter implements Formatter { static SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN); public synchronized String format(long dateTime) { return simpleFormat.format(dateTime); } } public static class DateFormatThread extends Thread { Formatter formatter; public DateFormatThread(Formatter f) { this.formatter = f; } public void run() { for (int i = 0; i < RUN_LENGTH; ++i) { long now = System.currentTimeMillis(); if (!isCached(now)) { formatter.format(now); } } } } }