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    static long RUN_LENGTH = 100000;
11    static long REFERENCE_BIPS = 9000;
12    
13    public MessageFormatterPerfTest(String name) {
14      super(name);
15    }
16  
17    protected void setUp() throws Exception {
18    }
19  
20    protected void tearDown() throws Exception {
21    }
22  
23    public void XtestJDKFormatterPerf() {
24      jdkMessageFormatter(RUN_LENGTH);
25      double duration = jdkMessageFormatter(RUN_LENGTH);
26      System.out.println("jdk duration = "+duration+" nanos");
27    }
28    
29    public void testSLF4JPerf() {
30      slf4jMessageFormatter(RUN_LENGTH);
31      double duration = slf4jMessageFormatter(RUN_LENGTH);
32      long referencePerf = 140;
33      BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
34    }
35  
36    public double slf4jMessageFormatter(long len) {
37      String s = ""; 
38      s += ""; // keep compiler happy
39      long start = System.currentTimeMillis();
40      for (int i = 0; i < len; i++) {
41        s = MessageFormatter.format("This is some rather short message {} ", i1);
42      }
43      long end = System.currentTimeMillis();
44      return (1.0*end - start);
45    }  
46    public double jdkMessageFormatter(long len) {
47      String s = ""; 
48      s += ""; // keep compiler happy
49      long start = System.currentTimeMillis();
50      Object[] oa = new Object[] {i1};
51      for (int i = 0; i < len; i++) {
52        s = MessageFormat.format("This is some rather short message {0}", oa);
53      }
54      long end = System.currentTimeMillis();
55      return (1.0*end - start);
56    }
57  
58  }