1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
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  //import org.apache.log4j.spi.Java14LocationInfoX;
28  import org.apache.log4j.spi.LocationInfo;
29  
30  import junit.framework.TestCase;
31  
32  /*
33   * Created on 20.11.2004
34   *
35   * Contributing Author(s):
36   *
37   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
38   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
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          // Worm up
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                  //"ToArraySocketAppenderLog",
85                  //"ToProfilerSocketAppenderLog",
86                  //"ToProfilerArraySocketAppenderLog",
87                  //"ToAsyncAppenderLog",
88                  //"ToProfilerAsyncAppenderLog"
89                  //"ToFile1",
90                  //"ToFile2"
91                  };
92          double times[] = new double[tests.length];
93  
94          final int RUNS = 3;
95          //RUNS = 1;
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         //log.debug("LocationInfo:" + LocationInfo.callCount);
113         log.debug("Java14LocationInfo:" + Java14LocationInfo.callCount);
114         LogManager.shutdown();
115     }
116 
117 }