Source code

    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.bridge;
    026
    027import java.util.logging.Handler;
    028import java.util.logging.LogManager;
    029
    030import org.apache.log4j.FileAppender;
    031import org.apache.log4j.PatternLayout;
    032import org.junit.After;
    033import org.junit.Before;
    034import org.junit.Test;
    035import org.slf4j.LoggerFactory;
    036
    037public class SLF4JBridgeHandlerPerfTest {
    038
    039    static String LOGGER_NAME = "yay";
    040    static int RUN_LENGTH = 100 * 1000;
    041
    042    // set to false to test enabled logging performance
    043    boolean disabledLogger = true;
    044
    045    FileAppender fileAppender;
    046    org.apache.log4j.Logger log4jRoot;
    047    java.util.logging.Logger julRootLogger = LogManager.getLogManager().getLogger("");
    048
    049    java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(LOGGER_NAME);
    050    org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(LOGGER_NAME);
    051
    052    Handler[] existingHandlers;
    053
    054    @Before
    055    public void setUp() throws Exception {
    056        fileAppender = new FileAppender(new PatternLayout("%r [%t] %p %c %x - %m%n"), "target/test-output/toto.log");
    057
    058        existingHandlers = julRootLogger.getHandlers();
    059        for (int i = 0; i < existingHandlers.length; i++) {
    060            julRootLogger.removeHandler(existingHandlers[i]);
    061        }
    062        log4jRoot = org.apache.log4j.Logger.getRootLogger();
    063        log4jRoot.addAppender(fileAppender);
    064    }
    065
    066    @After
    067    public void tearDown() throws Exception {
    068        SLF4JBridgeHandler.uninstall();
    069        fileAppender.close();
    070        log4jRoot.getLoggerRepository().resetConfiguration();
    071        for (int i = 0; i < existingHandlers.length; i++) {
    072            julRootLogger.addHandler(existingHandlers[i]);
    073        }
    074    }
    075
    076    double julLoggerLoop() {
    077        long start = System.nanoTime();
    078        for (int i = 0; i < RUN_LENGTH; i++) {
    079            julLogger.info("jul");
    080        }
    081        long end = System.nanoTime();
    082        return (end - start) * 1.0 / RUN_LENGTH;
    083    }
    084
    085    double slf4jLoggerLoop() {
    086        long start = System.nanoTime();
    087        for (int i = 0; i < RUN_LENGTH; i++) {
    088            slf4jLogger.info("slf4j");
    089        }
    090        long end = System.nanoTime();
    091        return (end - start) * 1.0 / RUN_LENGTH;
    092    }
    093
    094    @Test
    095    public void testPerf() {
    096        SLF4JBridgeHandler.install();
    097
    098        if (disabledLogger) {
    099            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}