1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.log4j;
22
23 import net.sf.jour.timer.SimpleTimer;
24
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.LogManager;
27
28 import org.apache.log4j.spi.LocationInfo;
29
30 import junit.framework.TestCase;
31
32
33
34
35
36
37
38
39
40 /***
41 * Do Log4j preformance tests
42 * @author root
43 * @version $Revision: 1.7 $ ($Author: vlads $)
44 */
45 public class TestPerformance extends TestCase {
46 /***
47 * logger.
48 */
49 protected final Logger log = Logger.getLogger(this.getClass());
50
51
52 static public void main(String[] args) throws Exception {
53 TestPerformance t = new TestPerformance();
54 t.testLog();
55 }
56
57 long totalCallCount = 0;
58
59 public double mesureLog(String logName) throws Exception {
60 Logger aLog = Logger.getLogger(logName);
61
62 for (int i = 0; i < 1000; ++ i) {
63 aLog.debug("bob");
64 totalCallCount++;
65 }
66 Thread.sleep(1000);
67 log.debug("StartTest " + logName);
68 SimpleTimer tr = new SimpleTimer();
69 long cnt = 10000;
70 for (int i = 0; i < cnt; ++ i) {
71 aLog.debug("bob");
72 totalCallCount++;
73 }
74 tr.end();
75 log.debug(logName + " call duration = " + tr.text2(cnt) + " ms");
76 totalCallCount++;
77 return tr.duration()/cnt;
78 }
79
80 public void testLog() throws Exception {
81 String tests[] = new String[]{
82 "ToNullAppenderLog",
83 "ToSocketAppenderLog",
84
85
86
87
88
89
90
91 };
92 double times[] = new double[tests.length];
93
94 final int RUNS = 3;
95
96 for(int i = 0; i < RUNS; i++) {
97 for(int t = 0; t < tests.length; t++) {
98 double r = mesureLog(tests[t]);
99 if (i == 0) {
100 times[t] = r;
101 } else {
102 times[t] += r;
103 }
104 }
105 }
106
107 for(int t = 0; t < tests.length; t++) {
108 log.debug("AVG " + tests[t] + " call duration = " + SimpleTimer.text(times[t]/RUNS) + " ms");
109 totalCallCount++;
110 }
111 log.debug("totalCallCount:" + totalCallCount);
112
113 log.debug("Java14LocationInfo:" + Java14LocationInfo.callCount);
114 LogManager.shutdown();
115 }
116
117 }