View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.issue.lbclassic36;
15  
16  import junit.framework.TestCase;
17  import junit.framework.Test;
18  import junit.framework.TestSuite;
19  
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  //import org.joda.time.format.DateTimeFormatter;
24  //import org.joda.time.format.DateTimeFormat;
25  //import org.joda.time.DateTime;
26  
27  public class DateFormatOriginal_tzest extends TestCase {
28      public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
29      static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
30  
31      /**
32       * Create the test case
33       * 
34       * @param testName
35       *                name of the test case
36       */
37      public DateFormatOriginal_tzest(String testName) {
38          super(testName);
39      }
40  
41      /**
42       * @return the suite of tests being tested
43       */
44      public static Test suite() {
45          return new TestSuite(DateFormatOriginal_tzest.class);
46      }
47  
48      public static void main(String[] args) {
49          junit.textui.TestRunner.run(suite());
50      }
51  
52      public void setUp() throws Exception {
53          super.setUp();
54      }
55  
56      public void tearDown() throws Exception {
57          super.tearDown();
58      }
59  
60      // public void testRaw() throws Exception {
61      // SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
62      // DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN);
63      //
64      // Date date = new Date();
65      // DateTime dateTime = new DateTime(date);
66      //
67      // long start = System.nanoTime();
68      // for (int i = 0; i < 100000; ++i) {
69      // jodaFormat.print(dateTime);
70      // }
71      // long jodaAvg = (System.nanoTime() - start) / 100000;
72      //
73      //
74      // start = System.nanoTime();
75      // for (int i = 0; i < 100000; ++i) {
76      // simpleFormat.format(date);
77      // }
78      // long simpleAvg = (System.nanoTime() - start) / 100000;
79      //
80      // float diff = (((float) (simpleAvg - jodaAvg)) / simpleAvg) * 100;
81      // System.out.println("Raw - JDK: " + simpleAvg + " ns Joda: " + jodaAvg
82      // + " ns - Difference: " + diff + "%");
83      // }
84  
85      public void testSynchronized() throws Exception {
86          SynchronizedDateFormatter formatter = new SynchronizedDateFormatter();
87          int threads = 10;
88          int iterations = 10000;
89          Thread[] formatThreads = new Thread[threads];
90          Date date = new Date();
91  
92          for (int i = 0; i < threads; i++) {
93              formatThreads[i] = new DateFormatThread(formatter, date, iterations);
94          }
95          long start = System.nanoTime();
96          for (Thread thread : formatThreads) {
97              thread.start();
98          }
99          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 }