View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.helpers;
26  
27  import java.text.MessageFormat;
28  
29  import junit.framework.TestCase;
30  import org.junit.Ignore;
31  
32  @Ignore
33  public class MessageFormatterPerfTest extends TestCase {
34  
35    Integer i1 = new Integer(1);
36    Integer i2 = new Integer(2);
37    static long RUN_LENGTH = 100 * 1000;
38    // 
39    static long REFERENCE_BIPS = 48416;
40  
41    public MessageFormatterPerfTest(String name) {
42      super(name);
43    }
44  
45    protected void setUp() throws Exception {
46    }
47  
48    protected void tearDown() throws Exception {
49    }
50  
51    public void XtestJDKFormatterPerf() {
52      jdkMessageFormatter(RUN_LENGTH);
53      double duration = jdkMessageFormatter(RUN_LENGTH);
54      System.out.println("jdk duration = " + duration + " nanos");
55    }
56  
57    public void testSLF4JPerf_OneArg() {
58      slf4jMessageFormatter_OneArg(RUN_LENGTH);
59      double duration = slf4jMessageFormatter_OneArg(RUN_LENGTH);
60      System.out.println("duration=" + duration);
61      long referencePerf = 36;
62      BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
63    }
64  
65    public void testSLF4JPerf_TwoArg() {
66      slf4jMessageFormatter_TwoArg(RUN_LENGTH);
67      double duration = slf4jMessageFormatter_TwoArg(RUN_LENGTH);
68      long referencePerf = 60;
69      BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
70    }
71  
72    
73    public double slf4jMessageFormatter_OneArg(long len) {
74      long start = System.nanoTime();
75      for (int i = 0; i < len; i++) {
76        final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
77        tp.getMessage();
78        tp.getArgArray();
79        tp.getThrowable();
80        
81        MessageFormatter.format("This is some rather short message {} ", i1);
82      }
83      long end = System.nanoTime();
84      return (end - start)/(1000*1000.0);
85    }
86    
87    public double slf4jMessageFormatter_TwoArg(long len) {
88      long start = System.nanoTime();
89      for (int i = 0; i < len; i++) {
90        final FormattingTuple tp = MessageFormatter.format(
91            "This is some {} short message {} ", i1, i2);
92        tp.getMessage();
93        tp.getArgArray();
94        tp.getThrowable();
95      }
96      long end = System.nanoTime();
97      return (end - start)/(1000*1000.0);
98    }
99  
100 
101 
102   public double jdkMessageFormatter(long len) {
103     @SuppressWarnings("unused")
104     String s = "";
105     s += ""; // keep compiler happy
106     long start = System.currentTimeMillis();
107     Object[] oa = new Object[] { i1 };
108     for (int i = 0; i < len; i++) {
109       s = MessageFormat.format("This is some rather short message {0}", oa);
110     }
111     long end = System.currentTimeMillis();
112     return (1.0 * end - start);
113   }
114 
115 }