SLF4JBridgeHandlerPerfTest xref

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