View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.issue.lbcore243;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.joran.JoranConfigurator;
18  import ch.qos.logback.core.joran.spi.JoranException;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  // Results AMD Phenom II X6 1110T processor and SSD disk
23  //Logback  with    immediate flush: 8356 nanos per call
24  //Logback  without immediate flush: 1758 nanos per call
25  
26  public class PerformanceComparatorLogback {
27      static Logger logbacklogger = LoggerFactory.getLogger(PerformanceComparatorLogback.class);
28  
29      public static void main(String[] args) throws JoranException, InterruptedException {
30          initLogbackWithoutImmediateFlush();
31          logbackParametrizedDebugCall();
32  
33          initLogbackWithImmediateFlush();
34          logbackParametrizedDebugCall();
35          System.out.println("###############################################");
36          System.out.println("Logback  with    immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
37  
38          initLogbackWithoutImmediateFlush();
39          System.out.println("Logback  without immediate flush: " + logbackParametrizedDebugCall() + " nanos per call");
40  
41          System.out.println("###############################################");
42      }
43  
44      private static long logbackParametrizedDebugCall() {
45  
46          Integer j = Integer.valueOf(2);
47          long start = System.nanoTime();
48          for (int i = 0; i < Common.loop; i++) {
49              logbacklogger.debug("SEE IF THIS IS LOGGED {}.", j);
50          }
51          return (System.nanoTime() - start) / Common.loop;
52      }
53  
54      static String DIR_PREFIX = "src/test/java/ch/qos/logback/classic/issue/lbcore243/";
55  
56      static void configure(String file) throws JoranException {
57          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
58          JoranConfigurator jc = new JoranConfigurator();
59          jc.setContext(loggerContext);
60          loggerContext.reset();
61          jc.doConfigure(file);
62      }
63  
64      private static void initLogbackWithoutImmediateFlush() throws JoranException {
65          configure(DIR_PREFIX + "logback_without_immediateFlush.xml");
66      }
67  
68      private static void initLogbackWithImmediateFlush() throws JoranException {
69          configure(DIR_PREFIX + "logback_with_immediateFlush.xml");
70      }
71  }