001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.issue.lbclassic36;
015
016import junit.framework.TestCase;
017import junit.framework.Test;
018import junit.framework.TestSuite;
019
020import java.text.SimpleDateFormat;
021import java.util.Date;
022
023//import org.joda.time.format.DateTimeFormatter;
024//import org.joda.time.format.DateTimeFormat;
025//import org.joda.time.DateTime;
026
027public class DateFormatOriginal_tzest extends TestCase {
028    public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
029    static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
030
031    /**
032     * Create the test case
033     * 
034     * @param testName
035     *                name of the test case
036     */
037    public DateFormatOriginal_tzest(String testName) {
038        super(testName);
039    }
040
041    /**
042     * @return the suite of tests being tested
043     */
044    public static Test suite() {
045        return new TestSuite(DateFormatOriginal_tzest.class);
046    }
047
048    public static void main(String[] args) {
049        junit.textui.TestRunner.run(suite());
050    }
051
052    public void setUp() throws Exception {
053        super.setUp();
054    }
055
056    public void tearDown() throws Exception {
057        super.tearDown();
058    }
059
060    // public void testRaw() throws Exception {
061    // SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
062    // DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN);
063    //
064    // Date date = new Date();
065    // DateTime dateTime = new DateTime(date);
066    //
067    // long start = System.nanoTime();
068    // for (int i = 0; i < 100000; ++i) {
069    // jodaFormat.print(dateTime);
070    // }
071    // long jodaAvg = (System.nanoTime() - start) / 100000;
072    //
073    //
074    // start = System.nanoTime();
075    // for (int i = 0; i < 100000; ++i) {
076    // simpleFormat.format(date);
077    // }
078    // long simpleAvg = (System.nanoTime() - start) / 100000;
079    //
080    // float diff = (((float) (simpleAvg - jodaAvg)) / simpleAvg) * 100;
081    // System.out.println("Raw - JDK: " + simpleAvg + " ns Joda: " + jodaAvg
082    // + " ns - Difference: " + diff + "%");
083    // }
084
085    public void testSynchronized() throws Exception {
086        SynchronizedDateFormatter formatter = new SynchronizedDateFormatter();
087        int threads = 10;
088        int iterations = 10000;
089        Thread[] formatThreads = new Thread[threads];
090        Date date = new Date();
091
092        for (int i = 0; i < threads; i++) {
093            formatThreads[i] = new DateFormatThread(formatter, date, iterations);
094        }
095        long start = System.nanoTime();
096        for (Thread thread : formatThreads) {
097            thread.start();
098        }
099        for (Thread thread : formatThreads) {
100            thread.join();
101        }
102        long end = System.nanoTime();
103        double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
104        System.out.printf("Synchronized DateFormat: %,.4f seconds\n", actual);
105
106    }
107
108    public void testUnSynchronized() throws Exception {
109        UnsynchronizedDateFormatter formatter = new UnsynchronizedDateFormatter();
110        int threads = 10;
111        int iterations = 10000;
112        Thread[] formatThreads = new Thread[threads];
113        Date date = new Date();
114
115        for (int i = 0; i < threads; i++) {
116            formatThreads[i] = new DateFormatThread(formatter, date, iterations);
117        }
118        long start = System.nanoTime();
119        for (Thread thread : formatThreads) {
120            thread.start();
121        }
122        for (Thread thread : formatThreads) {
123            thread.join();
124        }
125        long end = System.nanoTime();
126        double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
127        System.out.printf("Unsynchronized DateFormat: %,.4f seconds\n", actual);
128
129    }
130
131    public void testThreadLocal() throws Exception {
132        ThreadLocalDateFormatter formatter = new ThreadLocalDateFormatter();
133        int threads = 10;
134        int iterations = 10000;
135        Thread[] formatThreads = new Thread[threads];
136        Date date = new Date();
137
138        for (int i = 0; i < threads; i++) {
139            formatThreads[i] = new DateFormatThread(formatter, date, iterations);
140        }
141        long start = System.nanoTime();
142        for (Thread thread : formatThreads) {
143            thread.start();
144        }
145        for (Thread thread : formatThreads) {
146            thread.join();
147        }
148        long end = System.nanoTime();
149        double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
150        System.out.printf("ThreadLocal DateFormat: %,.4f seconds\n", actual);
151
152    }
153
154    // public void testDateTimeFormatter() throws Exception {
155    // int threads = 10;
156    // int iterations = 10000;
157    // Thread[] formatThreads = new DateTimeFormatThread[threads];
158    // JodaFormatter formatter = new JodaFormatter();
159    // Date date = new Date();
160    // DateTime dateTime = new DateTime(date);
161    //
162    // for (int i = 0; i < threads; i++) {
163    // formatThreads[i] = new DateTimeFormatThread(formatter, dateTime,
164    // iterations);
165    // }
166    // long start = System.nanoTime();
167    // for (Thread thread : formatThreads) {
168    // thread.start();
169    // }
170    // for (Thread thread : formatThreads) {
171    // thread.join();
172    // }
173    // long end = System.nanoTime();
174    // double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
175    // System.out.printf("Joda DateTimeFormatter: %,.4f seconds\n", actual);
176    //
177    // }
178
179    public interface Formatter {
180        String format(Date date);
181    }
182
183    public static class SynchronizedDateFormatter implements Formatter {
184        SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
185
186        public synchronized String format(Date date) {
187            return simpleFormat.format(date);
188        }
189    }
190
191    public static class UnsynchronizedDateFormatter implements Formatter {
192        public synchronized String format(Date date) {
193            return new SimpleDateFormat(ISO8601_PATTERN).format(date);
194        }
195    }
196
197    public static class ThreadLocalDateFormatter implements Formatter {
198        ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() {
199            protected synchronized SimpleDateFormat initialValue() {
200                return new SimpleDateFormat(ISO8601_PATTERN);
201            }
202        };
203
204        public String format(Date date) {
205            return formatter.get().format(date);
206        }
207    }
208
209    // public static class JodaFormatter {
210    // DateTimeFormatter formatter = DateTimeFormat.forPattern(ISO8601_PATTERN);
211    //
212    // public String format(DateTime date) {
213    // return formatter.print(date);
214    // }
215    // }
216
217    public static class DateFormatThread extends Thread {
218        Formatter formatter;
219        Date date;
220        long iterCount;
221
222        public DateFormatThread(Formatter f, Date date, long iterations) {
223            this.formatter = f;
224            this.date = date;
225            this.iterCount = iterations;
226        }
227
228        public void run() {
229            for (int i = 0; i < iterCount; i++) {
230                formatter.format(this.date);
231            }
232        }
233    }
234
235    // public static class DateTimeFormatThread extends Thread {
236    // JodaFormatter formatter;
237    // DateTime date;
238    // long iterCount;
239    //
240    // public DateTimeFormatThread(JodaFormatter f, DateTime date, long iterations) {
241    // this.formatter = f;
242    // this.date = date;
243    // this.iterCount = iterations;
244    // }
245    //
246    // public void run() {
247    // for (int i = 0; i < iterCount; i++) {
248    // formatter.format(this.date);
249    // }
250    // }
251    // }
252}