View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.bridge;
26  
27  import java.util.logging.Handler;
28  import java.util.logging.LogManager;
29  
30  import junit.framework.TestCase;
31  
32  import org.apache.log4j.FileAppender;
33  import org.apache.log4j.PatternLayout;
34  import org.slf4j.LoggerFactory;
35  
36  public class SLF4JBridgeHandlerPerfTest extends TestCase {
37  
38    static String LOGGER_NAME = "yay";
39    static int RUN_LENGTH = 100*1000;
40  
41  
42    // set to false to test enabled logging performance
43    boolean disabledLogger = true;
44    
45    FileAppender fileAppender; 
46    org.apache.log4j.Logger log4jRoot;
47    java.util.logging.Logger julRootLogger = LogManager.getLogManager()
48    .getLogger("");
49  
50    java.util.logging.Logger julLogger = java.util.logging.Logger
51        .getLogger(LOGGER_NAME);
52    org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(LOGGER_NAME);
53    
54    Handler[] existingHandlers;
55  
56    public SLF4JBridgeHandlerPerfTest(String arg0) {
57      super(arg0);
58    }
59  
60    protected void setUp() throws Exception {
61      super.setUp();
62      fileAppender = new FileAppender(new PatternLayout("%r [%t] %p %c %x - %m%n"), "target/test-output/toto.log");
63  
64      existingHandlers = julRootLogger.getHandlers();
65      for (int i = 0; i < existingHandlers.length; i++) {
66        julRootLogger.removeHandler(existingHandlers[i]);
67      }
68      log4jRoot = org.apache.log4j.Logger.getRootLogger();
69      log4jRoot.addAppender(fileAppender);
70    }
71  
72    protected void tearDown() throws Exception {
73      super.tearDown();
74      SLF4JBridgeHandler.uninstall();
75      fileAppender.close();
76      log4jRoot.getLoggerRepository().resetConfiguration();
77      for (int i = 0; i < existingHandlers.length; i++) {
78        julRootLogger.addHandler(existingHandlers[i]);
79      }
80    }
81  
82    double julLoggerLoop() {
83      long start = System.nanoTime();
84      for (int i = 0; i < RUN_LENGTH; i++) {
85        julLogger.info("jul");
86      }
87      long end = System.nanoTime();
88      return (end - start) * 1.0 / RUN_LENGTH;
89    }
90  
91    double slf4jLoggerLoop() {
92      long start = System.nanoTime();
93      for (int i = 0; i < RUN_LENGTH; i++) {
94        slf4jLogger.info("slf4j");
95      }
96      long end = System.nanoTime();
97      return (end - start) * 1.0 / RUN_LENGTH;
98    }
99    
100   public void testPerf() {
101     SLF4JBridgeHandler.install();
102     
103     if(disabledLogger) {
104      log4jRoot.setLevel(org.apache.log4j.Level.ERROR);
105     }
106     julLoggerLoop();
107     double julAvg=julLoggerLoop();
108     System.out.println("Average cost per call (JUL->SLF4J->log4j): "+julAvg +" nanos");
109      
110     slf4jLoggerLoop();
111     double slf4jAvg=slf4jLoggerLoop();
112     System.out.println("Average cost per call (SLF4J->log4j): "+slf4jAvg +" nanos");
113     System.out.println("Ratio "+(julAvg/slf4jAvg));
114   }
115 }