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      // set to false to test enabled logging performance
42      boolean disabledLogger = true;
43  
44      FileAppender fileAppender;
45      org.apache.log4j.Logger log4jRoot;
46      java.util.logging.Logger julRootLogger = LogManager.getLogManager().getLogger("");
47  
48      java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(LOGGER_NAME);
49      org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(LOGGER_NAME);
50  
51      Handler[] existingHandlers;
52  
53      public SLF4JBridgeHandlerPerfTest(String arg0) {
54          super(arg0);
55      }
56  
57      protected void setUp() throws Exception {
58          super.setUp();
59          fileAppender = new FileAppender(new PatternLayout("%r [%t] %p %c %x - %m%n"), "target/test-output/toto.log");
60  
61          existingHandlers = julRootLogger.getHandlers();
62          for (int i = 0; i < existingHandlers.length; i++) {
63              julRootLogger.removeHandler(existingHandlers[i]);
64          }
65          log4jRoot = org.apache.log4j.Logger.getRootLogger();
66          log4jRoot.addAppender(fileAppender);
67      }
68  
69      protected void tearDown() throws Exception {
70          super.tearDown();
71          SLF4JBridgeHandler.uninstall();
72          fileAppender.close();
73          log4jRoot.getLoggerRepository().resetConfiguration();
74          for (int i = 0; i < existingHandlers.length; i++) {
75              julRootLogger.addHandler(existingHandlers[i]);
76          }
77      }
78  
79      double julLoggerLoop() {
80          long start = System.nanoTime();
81          for (int i = 0; i < RUN_LENGTH; i++) {
82              julLogger.info("jul");
83          }
84          long end = System.nanoTime();
85          return (end - start) * 1.0 / RUN_LENGTH;
86      }
87  
88      double slf4jLoggerLoop() {
89          long start = System.nanoTime();
90          for (int i = 0; i < RUN_LENGTH; i++) {
91              slf4jLogger.info("slf4j");
92          }
93          long end = System.nanoTime();
94          return (end - start) * 1.0 / RUN_LENGTH;
95      }
96  
97      public void testPerf() {
98          SLF4JBridgeHandler.install();
99  
100         if (disabledLogger) {
101             log4jRoot.setLevel(org.apache.log4j.Level.ERROR);
102         }
103         julLoggerLoop();
104         double julAvg = julLoggerLoop();
105         System.out.println("Average cost per call (JUL->SLF4J->log4j): " + julAvg + " nanos");
106 
107         slf4jLoggerLoop();
108         double slf4jAvg = slf4jLoggerLoop();
109         System.out.println("Average cost per call (SLF4J->log4j): " + slf4jAvg + " nanos");
110         System.out.println("Ratio " + (julAvg / slf4jAvg));
111     }
112 }