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 org.junit.Ignore;
30 import org.junit.Test;
31
32 @Ignore
33 public class MessageFormatterPerfTest {
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 void XtestJDKFormatterPerf() {
42 jdkMessageFormatter(RUN_LENGTH);
43 double duration = jdkMessageFormatter(RUN_LENGTH);
44 System.out.println("jdk duration = " + duration + " nanos");
45 }
46
47 @Test
48 public void testSLF4JPerf_OneArg() {
49 slf4jMessageFormatter_OneArg(RUN_LENGTH);
50 double duration = slf4jMessageFormatter_OneArg(RUN_LENGTH);
51 System.out.println("duration=" + duration);
52 long referencePerf = 36;
53 BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
54 }
55
56 @Test
57 public void testSLF4JPerf_TwoArg() {
58 slf4jMessageFormatter_TwoArg(RUN_LENGTH);
59 double duration = slf4jMessageFormatter_TwoArg(RUN_LENGTH);
60 long referencePerf = 60;
61 BogoPerf.assertDuration(duration, referencePerf, REFERENCE_BIPS);
62 }
63
64 public double slf4jMessageFormatter_OneArg(long len) {
65 long start = System.nanoTime();
66 for (int i = 0; i < len; i++) {
67 final FormattingTuple tp = MessageFormatter.format("This is some rather short message {} ", i1);
68 tp.getMessage();
69 tp.getArgArray();
70 tp.getThrowable();
71
72 MessageFormatter.format("This is some rather short message {} ", i1);
73 }
74 long end = System.nanoTime();
75 return (end - start) / (1000 * 1000.0);
76 }
77
78 public double slf4jMessageFormatter_TwoArg(long len) {
79 long start = System.nanoTime();
80 for (int i = 0; i < len; i++) {
81 final FormattingTuple tp = MessageFormatter.format("This is some {} short message {} ", i1, i2);
82 tp.getMessage();
83 tp.getArgArray();
84 tp.getThrowable();
85 }
86 long end = System.nanoTime();
87 return (end - start) / (1000 * 1000.0);
88 }
89
90 public double jdkMessageFormatter(long len) {
91 @SuppressWarnings("unused")
92 String s = "";
93 s += "";
94 long start = System.currentTimeMillis();
95 Object[] oa = new Object[] { i1 };
96 for (int i = 0; i < len; i++) {
97 s = MessageFormat.format("This is some rather short message {0}", oa);
98 }
99 long end = System.currentTimeMillis();
100 return (1.0 * end - start);
101 }
102
103 }