001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.issue.lbcore243;
015
016import ch.qos.logback.classic.LoggerContext;
017import ch.qos.logback.classic.joran.JoranConfigurator;
018import ch.qos.logback.core.joran.spi.JoranException;
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021
022// Results AMD Phenom II X6 1110T processor and SSD disk
023//Logback  with    immediate flush: 8356 nanos per call
024//Logback  without immediate flush: 1758 nanos per call
025
026public class PerformanceComparatorLogback {
027    static Logger logbacklogger = LoggerFactory.getLogger(PerformanceComparatorLogback.class);
028
029    public static void main(String[] args) throws JoranException, InterruptedException {
030        initLogbackWithoutImmediateFlush();
031        logbackParametrizedDebugCall();
032
033        initLogbackWithImmediateFlush();
034        logbackParametrizedDebugCall();
035        System.out.println("###############################################");
036        System.out.println("Logback  with    immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
037
038        initLogbackWithoutImmediateFlush();
039        System.out.println("Logback  without immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
040
041        System.out.println("###############################################");
042    }
043
044    private static long logbackParametrizedDebugCall() {
045
046        Integer j = Integer.valueOf(2);
047        long start = System.nanoTime();
048        for (int i = 0; i < Common.loop; i++) {
049            logbacklogger.debug("SEE IF THIS IS LOGGED {}.", j);
050        }
051        return (System.nanoTime() - start) / Common.loop;
052    }
053
054    static String DIR_PREFIX = "src/test/java/ch/qos/logback/classic/issue/lbcore243/";
055
056    static void configure(String file) throws JoranException {
057        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
058        JoranConfigurator jc = new JoranConfigurator();
059        jc.setContext(loggerContext);
060        loggerContext.reset();
061        jc.doConfigure(file);
062    }
063
064    private static void initLogbackWithoutImmediateFlush() throws JoranException {
065        configure(DIR_PREFIX + "logback_without_immediateFlush.xml");
066    }
067
068    private static void initLogbackWithImmediateFlush() throws JoranException {
069        configure(DIR_PREFIX + "logback_with_immediateFlush.xml");
070    }
071}