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
31 public class MessageFormatterPerfTest extends TestCase {
32
33 Integer i1 = new Integer(1);
34 Integer i2 = new Integer(2);
35 static long RUN_LENGTH = 100 * 1000;
36
37 static long REFERENCE_BIPS = 48416;
38
39 public MessageFormatterPerfTest(String name) {
40 super(name);
41 }
42
43 protected void setUp() throws Exception {
44 }
45
46 protected void tearDown() throws Exception {
47 }
48
49 public void XtestJDKFormatterPerf() {
50 jdkMessageFormatter(RUN_LENGTH);
51 double duration = jdkMessageFormatter(RUN_LENGTH);
52 System.out.println("jdk duration = " + duration + " nanos");
53 }
54
55 public void testSLF4JPerf_OneArg() {
56 slf4jMessageFormatter_OneArg(RUN_LENGTH);
57 double duration = slf4jMessageFormatter_OneArg(RUN_LENGTH);
58 System.out.println("duration=" + duration);
59 long referencePerf = 36;
60 BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
61 }
62
63 public void testSLF4JPerf_TwoArg() {
64 slf4jMessageFormatter_TwoArg(RUN_LENGTH);
65 double duration = slf4jMessageFormatter_TwoArg(RUN_LENGTH);
66 long referencePerf = 60;
67 BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
68 }
69
70
71 public double slf4jMessageFormatter_OneArg(long len) {
72 long start = System.nanoTime();
73 for (int i = 0; i < len; i++) {
74 final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
75 tp.getMessage();
76 tp.getArgArray();
77 tp.getThrowable();
78
79 MessageFormatter.format("This is some rather short message {} ", i1);
80 }
81 long end = System.nanoTime();
82 return (end - start)/(1000*1000.0);
83 }
84
85 public double slf4jMessageFormatter_TwoArg(long len) {
86 long start = System.nanoTime();
87 for (int i = 0; i < len; i++) {
88 final FormattingTuple tp = MessageFormatter.format(
89 "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
99
100 public double jdkMessageFormatter(long len) {
101 String s = "";
102 s += "";
103 long start = System.currentTimeMillis();
104 Object[] oa = new Object[] { i1 };
105 for (int i = 0; i < len; i++) {
106 s = MessageFormat.format("This is some rather short message {0}", oa);
107 }
108 long end = System.currentTimeMillis();
109 return (1.0 * end - start);
110 }
111
112 }