1   package org.slf4j.helpers;
2   
3   import java.text.MessageFormat;
4   
5   import junit.framework.TestCase;
6   
7   public class MessageFormatterPerfTest extends TestCase {
8   
9     Integer i1 = new Integer(1);
10    Integer i2 = new Integer(2);
11    static long RUN_LENGTH = 100 * 1000;
12    // 
13    static long REFERENCE_BIPS = 48416;
14  
15    public MessageFormatterPerfTest(String name) {
16      super(name);
17    }
18  
19    protected void setUp() throws Exception {
20    }
21  
22    protected void tearDown() throws Exception {
23    }
24  
25    public void XtestJDKFormatterPerf() {
26      jdkMessageFormatter(RUN_LENGTH);
27      double duration = jdkMessageFormatter(RUN_LENGTH);
28      System.out.println("jdk duration = " + duration + " nanos");
29    }
30  
31    public void testSLF4JPerf_OneArg() {
32      slf4jMessageFormatter_OneArg(RUN_LENGTH);
33      double duration = slf4jMessageFormatter_OneArg(RUN_LENGTH);
34      System.out.println("duration=" + duration);
35      long referencePerf = 36;
36      BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
37    }
38  
39    public void testSLF4JPerf_TwoArg() {
40      slf4jMessageFormatter_TwoArg(RUN_LENGTH);
41      double duration = slf4jMessageFormatter_TwoArg(RUN_LENGTH);
42      long referencePerf = 60;
43      BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
44    }
45  
46    
47    public double slf4jMessageFormatter_OneArg(long len) {
48      long start = System.nanoTime();
49      for (int i = 0; i < len; i++) {
50        final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
51        tp.getMessage();
52        tp.getArgArray();
53        tp.getThrowable();
54        
55        MessageFormatter.format("This is some rather short message {} ", i1);
56      }
57      long end = System.nanoTime();
58      return (end - start)/(1000*1000.0);
59    }
60    
61    public double slf4jMessageFormatter_TwoArg(long len) {
62      long start = System.nanoTime();
63      for (int i = 0; i < len; i++) {
64        final FormattingTuple tp = MessageFormatter.format(
65            "This is some {} short message {} ", i1, i2);
66        tp.getMessage();
67        tp.getArgArray();
68        tp.getThrowable();
69      }
70      long end = System.nanoTime();
71      return (end - start)/(1000*1000.0);
72    }
73  
74  
75  
76    public double jdkMessageFormatter(long len) {
77      String s = "";
78      s += ""; // keep compiler happy
79      long start = System.currentTimeMillis();
80      Object[] oa = new Object[] { i1 };
81      for (int i = 0; i < len; i++) {
82        s = MessageFormat.format("This is some rather short message {0}", oa);
83      }
84      long end = System.currentTimeMillis();
85      return (1.0 * end - start);
86    }
87  
88  }