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 org.junit.jupiter.api.Test;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.Date;
20  
21  //import org.joda.time.format.DateTimeFormatter;
22  //import org.joda.time.format.DateTimeFormat;
23  //import org.joda.time.DateTime;
24  
25  public class DateFormatOriginal_tzest {
26      public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
27      static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
28  
29  
30  
31      // public void testRaw() throws Exception {
32      // SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
33      // DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN);
34      //
35      // Date date = new Date();
36      // DateTime dateTime = new DateTime(date);
37      //
38      // long start = System.nanoTime();
39      // for (int i = 0; i < 100000; ++i) {
40      // jodaFormat.print(dateTime);
41      // }
42      // long jodaAvg = (System.nanoTime() - start) / 100000;
43      //
44      //
45      // start = System.nanoTime();
46      // for (int i = 0; i < 100000; ++i) {
47      // simpleFormat.format(date);
48      // }
49      // long simpleAvg = (System.nanoTime() - start) / 100000;
50      //
51      // float diff = (((float) (simpleAvg - jodaAvg)) / simpleAvg) * 100;
52      // System.out.println("Raw - JDK: " + simpleAvg + " ns Joda: " + jodaAvg
53      // + " ns - Difference: " + diff + "%");
54      // }
55  
56      @Test
57      public void testSynchronized() throws Exception {
58          SynchronizedDateFormatter formatter = new SynchronizedDateFormatter();
59          int threads = 10;
60          int iterations = 10000;
61          Thread[] formatThreads = new Thread[threads];
62          Date date = new Date();
63  
64          for (int i = 0; i < threads; i++) {
65              formatThreads[i] = new DateFormatThread(formatter, date, iterations);
66          }
67          long start = System.nanoTime();
68          for (Thread thread : formatThreads) {
69              thread.start();
70          }
71          for (Thread thread : formatThreads) {
72              thread.join();
73          }
74          long end = System.nanoTime();
75          double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
76          System.out.printf("Synchronized DateFormat: %,.4f seconds\n", actual);
77  
78      }
79      @Test
80      public void testUnSynchronized() throws Exception {
81          UnsynchronizedDateFormatter formatter = new UnsynchronizedDateFormatter();
82          int threads = 10;
83          int iterations = 10000;
84          Thread[] formatThreads = new Thread[threads];
85          Date date = new Date();
86  
87          for (int i = 0; i < threads; i++) {
88              formatThreads[i] = new DateFormatThread(formatter, date, iterations);
89          }
90          long start = System.nanoTime();
91          for (Thread thread : formatThreads) {
92              thread.start();
93          }
94          for (Thread thread : formatThreads) {
95              thread.join();
96          }
97          long end = System.nanoTime();
98          double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
99          System.out.printf("Unsynchronized DateFormat: %,.4f seconds\n", actual);
100 
101     }
102     @Test
103     public void testThreadLocal() throws Exception {
104         ThreadLocalDateFormatter formatter = new ThreadLocalDateFormatter();
105         int threads = 10;
106         int iterations = 10000;
107         Thread[] formatThreads = new Thread[threads];
108         Date date = new Date();
109 
110         for (int i = 0; i < threads; i++) {
111             formatThreads[i] = new DateFormatThread(formatter, date, iterations);
112         }
113         long start = System.nanoTime();
114         for (Thread thread : formatThreads) {
115             thread.start();
116         }
117         for (Thread thread : formatThreads) {
118             thread.join();
119         }
120         long end = System.nanoTime();
121         double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
122         System.out.printf("ThreadLocal DateFormat: %,.4f seconds\n", actual);
123 
124     }
125 
126     // public void testDateTimeFormatter() throws Exception {
127     // int threads = 10;
128     // int iterations = 10000;
129     // Thread[] formatThreads = new DateTimeFormatThread[threads];
130     // JodaFormatter formatter = new JodaFormatter();
131     // Date date = new Date();
132     // DateTime dateTime = new DateTime(date);
133     //
134     // for (int i = 0; i < threads; i++) {
135     // formatThreads[i] = new DateTimeFormatThread(formatter, dateTime,
136     // iterations);
137     // }
138     // long start = System.nanoTime();
139     // for (Thread thread : formatThreads) {
140     // thread.start();
141     // }
142     // for (Thread thread : formatThreads) {
143     // thread.join();
144     // }
145     // long end = System.nanoTime();
146     // double actual = ((double) (end - start)) / NANOS_IN_ONE_SEC;
147     // System.out.printf("Joda DateTimeFormatter: %,.4f seconds\n", actual);
148     //
149     // }
150 
151     public interface Formatter {
152         String format(Date date);
153     }
154 
155     public static class SynchronizedDateFormatter implements Formatter {
156         SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
157 
158         public synchronized String format(Date date) {
159             return simpleFormat.format(date);
160         }
161     }
162 
163     public static class UnsynchronizedDateFormatter implements Formatter {
164         public synchronized String format(Date date) {
165             return new SimpleDateFormat(ISO8601_PATTERN).format(date);
166         }
167     }
168 
169     public static class ThreadLocalDateFormatter implements Formatter {
170         ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() {
171             protected synchronized SimpleDateFormat initialValue() {
172                 return new SimpleDateFormat(ISO8601_PATTERN);
173             }
174         };
175 
176         public String format(Date date) {
177             return formatter.get().format(date);
178         }
179     }
180 
181     // public static class JodaFormatter {
182     // DateTimeFormatter formatter = DateTimeFormat.forPattern(ISO8601_PATTERN);
183     //
184     // public String format(DateTime date) {
185     // return formatter.print(date);
186     // }
187     // }
188 
189     public static class DateFormatThread extends Thread {
190         Formatter formatter;
191         Date date;
192         long iterCount;
193 
194         public DateFormatThread(Formatter f, Date date, long iterations) {
195             this.formatter = f;
196             this.date = date;
197             this.iterCount = iterations;
198         }
199 
200         public void run() {
201             for (int i = 0; i < iterCount; i++) {
202                 formatter.format(this.date);
203             }
204         }
205     }
206 
207     // public static class DateTimeFormatThread extends Thread {
208     // JodaFormatter formatter;
209     // DateTime date;
210     // long iterCount;
211     //
212     // public DateTimeFormatThread(JodaFormatter f, DateTime date, long iterations)
213     // {
214     // this.formatter = f;
215     // this.date = date;
216     // this.iterCount = iterations;
217     // }
218     //
219     // public void run() {
220     // for (int i = 0; i < iterCount; i++) {
221     // formatter.format(this.date);
222     // }
223     // }
224     // }
225 }