1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 public double slf4jMessageFormatter_OneArg(long len) {
73 long start = System.nanoTime();
74 for (int i = 0; i < len; i++) {
75 final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
76 tp.getMessage();
77 tp.getArgArray();
78 tp.getThrowable();
79
80 MessageFormatter.format("This is some rather short message {} ", i1);
81 }
82 long end = System.nanoTime();
83 return (end - start) / (1000 * 1000.0);
84 }
85
86 public double slf4jMessageFormatter_TwoArg(long len) {
87 long start = System.nanoTime();
88 for (int i = 0; i < len; i++) {
89 final FormattingTuple tp = MessageFormatter.format("This is some {} short message {} ", i1, i2);
90 tp.getMessage();
91 tp.getArgArray();
92 tp.getThrowable();
93 }
94 long end = System.nanoTime();
95 return (end - start) / (1000 * 1000.0);
96 }
97
98 public double jdkMessageFormatter(long len) {
99 @SuppressWarnings("unused")
100 String s = "";
101 s += "";
102 long start = System.currentTimeMillis();
103 Object[] oa = new Object[] { i1 };
104 for (int i = 0; i < len; i++) {
105 s = MessageFormat.format("This is some rather short message {0}", oa);
106 }
107 long end = System.currentTimeMillis();
108 return (1.0 * end - start);
109 }
110
111 }