On 19/06/2009, kkoli...@apache.org <kkoli...@apache.org> wrote:
> Author: kkolinko
>  Date: Fri Jun 19 18:57:59 2009
>  New Revision: 786631
>
>  URL: http://svn.apache.org/viewvc?rev=786631&view=rev
>  Log:
>  Add two more implementations for the second test.
>  a) using a single ThreadLocal instead of multiple ones
>  b) also using StringBuilder instead of StringBuffer
>  Also, replaced class.getName() with class.getSimpleName() in the status 
> message.
>
>  Modified:
>     tomcat/trunk/test/org/apache/catalina/valves/Benchmarks.java
>
>  Modified: tomcat/trunk/test/org/apache/catalina/valves/Benchmarks.java
>  URL: 
> http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/valves/Benchmarks.java?rev=786631&r1=786630&r2=786631&view=diff
>  
> ==============================================================================
>  --- tomcat/trunk/test/org/apache/catalina/valves/Benchmarks.java (original)
>  +++ tomcat/trunk/test/org/apache/catalina/valves/Benchmarks.java Fri Jun 19 
> 18:57:59 2009
>  @@ -135,8 +135,8 @@
>
>          // note, that we can avoid (long -> Long) conversion
>          private static class Struct {
>  -            long currentMillis = 0;
>  -            Date currentDate;
>  +            public long currentMillis = 0;
>  +            public Date currentDate;
>          }
>
>          private ThreadLocal<Struct> currentStruct = new 
> ThreadLocal<Struct>() {
>  @@ -165,7 +165,9 @@
>          BenchmarkTest benchmark = new BenchmarkTest();
>          Runnable[] tests = new Runnable[] {
>                  new TimeDateElementBenchmarkTest_Sync(),
>  -                new TimeDateElementBenchmarkTest_Local() };
>  +                new TimeDateElementBenchmarkTest_Local(),
>  +                new TimeDateElementBenchmarkTest_LocalStruct(),
>  +                new TimeDateElementBenchmarkTest_LocalStruct_SBuilder() };
>          benchmark.doTest(5, tests);
>      }
>
>  @@ -285,13 +287,17 @@
>              if (currentDateStringLocal.get() == null) {
>                  StringBuffer current = new StringBuffer(32);
>                  current.append('[');
>  -                
> current.append(dayFormatterLocal.get().format(currentDateLocal.get())); // Day
>  +                current.append(dayFormatterLocal.get().format(
>  +                        currentDateLocal.get())); // Day
>                  current.append('/');
>  -                
> current.append(lookup(monthFormatterLocal.get().format(currentDateLocal.get())));
>  // Month
>  +                current.append(lookup(monthFormatterLocal.get().format(
>  +                        currentDateLocal.get()))); // Month
>                  current.append('/');
>  -                
> current.append(yearFormatterLocal.get().format(currentDateLocal.get())); // 
> Year
>  +                current.append(yearFormatterLocal.get().format(
>  +                        currentDateLocal.get())); // Year
>                  current.append(':');
>  -                
> current.append(timeFormatterLocal.get().format(currentDateLocal.get())); // 
> Time
>  +                current.append(timeFormatterLocal.get().format(
>  +                        currentDateLocal.get())); // Time
>                  current.append(']');
>                  currentDateStringLocal.set(current.toString());
>              }
>  @@ -308,6 +314,122 @@
>          }
>      }
>
>  +    private static class TimeDateElementBenchmarkTest_LocalStruct extends
>  +            TimeDateElementBenchmarkTestBase implements Runnable {
>  +
>  +        public String toString() {
>  +            return "single ThreadLocal";
>  +        }
>  +
>  +        private static class Struct {
>  +            public String currentDateString;
>  +            public Date currentDate = new Date();
>  +            public SimpleDateFormat dayFormatter = new 
> SimpleDateFormat("dd");
>  +            public SimpleDateFormat monthFormatter = new 
> SimpleDateFormat("MM");
>  +            public SimpleDateFormat yearFormatter = new 
> SimpleDateFormat("yyyy");
>  +            public SimpleDateFormat timeFormatter = new SimpleDateFormat(
>  +                    "hh:mm:ss");
>  +        }
>  +
>  +        private ThreadLocal<Struct> structLocal = new ThreadLocal<Struct>() 
> {
>  +            protected Struct initialValue() {
>  +                return new Struct();
>  +            }
>  +        };
>  +
>  +        public void run() {
>  +            printDate();
>  +        }
>  +
>  +        public String printDate() {
>  +            getDateLocal();
>  +            Struct struct = structLocal.get();
>  +            if (struct.currentDateString == null) {
>  +                StringBuffer current = new StringBuffer(32);
>  +                current.append('[');
>  +                
> current.append(struct.dayFormatter.format(struct.currentDate)); // Day
>  +                current.append('/');
>  +                current.append(lookup(struct.monthFormatter
>  +                        .format(struct.currentDate))); // Month
>  +                current.append('/');
>  +                
> current.append(struct.yearFormatter.format(struct.currentDate)); // Year
>  +                current.append(':');
>  +                
> current.append(struct.timeFormatter.format(struct.currentDate)); // Time
>  +                current.append(']');
>  +                struct.currentDateString = current.toString();
>  +            }
>  +            return struct.currentDateString;
>  +        }
>  +
>  +        private Date getDateLocal() {
>  +            Struct struct = structLocal.get();
>  +            long systime = System.currentTimeMillis();
>  +            if ((systime - struct.currentDate.getTime()) > 1000) {
>  +                struct.currentDate.setTime(systime);
>  +                struct.currentDateString = null;
>  +            }
>  +            return struct.currentDate;
>  +        }
>  +    }
>  +
>  +    private static class TimeDateElementBenchmarkTest_LocalStruct_SBuilder 
> extends
>  +            TimeDateElementBenchmarkTestBase implements Runnable {
>  +
>  +        public String toString() {
>  +            return "single ThreadLocal, with StringBuilder";
>  +        }
>  +
>  +        private static class Struct {
>  +            public String currentDateString;
>  +            public Date currentDate = new Date();
>  +            public SimpleDateFormat dayFormatter = new 
> SimpleDateFormat("dd");
>  +            public SimpleDateFormat monthFormatter = new 
> SimpleDateFormat("MM");
>  +            public SimpleDateFormat yearFormatter = new 
> SimpleDateFormat("yyyy");
>  +            public SimpleDateFormat timeFormatter = new SimpleDateFormat(
>  +                    "hh:mm:ss");
>  +        }
>  +
>  +        private ThreadLocal<Struct> structLocal = new ThreadLocal<Struct>() 
> {
>  +            protected Struct initialValue() {
>  +                return new Struct();
>  +            }
>  +        };
>  +
>  +        public void run() {
>  +            printDate();
>  +        }
>  +
>  +        public String printDate() {
>  +            getDateLocal();
>  +            Struct struct = structLocal.get();
>  +            if (struct.currentDateString == null) {
>  +                StringBuilder current = new StringBuilder(32);
>  +                current.append('[');
>  +                
> current.append(struct.dayFormatter.format(struct.currentDate)); // Day
>  +                current.append('/');
>  +                current.append(lookup(struct.monthFormatter
>  +                        .format(struct.currentDate))); // Month
>  +                current.append('/');
>  +                
> current.append(struct.yearFormatter.format(struct.currentDate)); // Year
>  +                current.append(':');
>  +                
> current.append(struct.timeFormatter.format(struct.currentDate)); // Time
>  +                current.append(']');
>  +                struct.currentDateString = current.toString();
>  +            }
>  +            return struct.currentDateString;
>  +        }
>  +
>  +        private Date getDateLocal() {
>  +            Struct struct = structLocal.get();
>  +            long systime = System.currentTimeMillis();
>  +            if ((systime - struct.currentDate.getTime()) > 1000) {
>  +                struct.currentDate.setTime(systime);
>  +                struct.currentDateString = null;
>  +            }
>  +            return struct.currentDate;
>  +        }
>  +    }
>  +
>      private static class BenchmarkTest {
>          public void doTest(int threadCount, Runnable[] tests) throws 
> Exception {
>              for (int iterations = 1000000; iterations < 10000001; iterations 
> += 1000000) {
>  @@ -332,9 +454,10 @@
>              }
>              long end = System.currentTimeMillis();
>
>  -            System.out.println(test.getClass().getName() + ": " + 
> threadCount
>  -                    + " threads and " + iterations + " iterations using "
>  -                    + test + " took " + (end - start) + "ms");
>  +            System.out.println(test.getClass().getSimpleName() + ": "
>  +                    + threadCount + " threads and " + iterations
>  +                    + " iterations using " + test + " took " + (end - start)
>  +                    + "ms");

IMO it would be easier to compare the figures if the output was
arranged as follows:

            System.out.println(threadCount + " threads and " + iterations
                    + " took " + (end - start) + " ms"
                    + " using "
                    + test.getClass().getSimpleName()
                    + " : "
                    + test
                    );

especially if the max iterations was set to 9000000 rather than 10000000

>          }
>      }
>
>
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
>  For additional commands, e-mail: dev-h...@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to