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 org.apache.log4j.xml.DOMConfigurator;
17  
18  import ch.qos.logback.core.joran.spi.JoranException;
19  
20  // WARNING This code compiles but does not measure anything useful because log4j-over-slf4j is a dependency. Log4j
21  // should be used instead
22  
23  public class PerformanceComparatorLog4j {
24  
25      static org.apache.log4j.Logger log4jlogger = org.apache.log4j.Logger.getLogger(PerformanceComparatorLog4j.class);
26  
27      public static void main(String[] args) throws JoranException, InterruptedException {
28          initLog4jWithoutImmediateFlush();
29  
30          // Let's run once for Just In Time compiler
31          log4jDirectDebugCall();
32  
33          System.out.println("###############################################");
34          System.out.println("Log4j    without immediate flush: " + log4jDirectDebugCall() + " nanos per call");
35          System.out.println("###############################################");
36      }
37  
38      private static long log4jDirectDebugCall() {
39          Integer j = Integer.valueOf(2);
40          long start = System.nanoTime();
41          for (int i = 0; i < Common.loop; i++) {
42              log4jlogger.debug("SEE IF THIS IS LOGGED " + j + ".");
43          }
44          return (System.nanoTime() - start) / Common.loop;
45      }
46  
47      static String DIR_PREFIX = "src/test/java/ch/qos/logback/classic/issue/lbcore243/";
48  
49      static void initLog4jWithoutImmediateFlush() {
50          DOMConfigurator.configure(DIR_PREFIX + "log4j_without_immediateFlush.xml");
51      }
52  
53      static void initLog4jWithImmediateFlush() {
54          DOMConfigurator.configure(DIR_PREFIX + "log4j_with_immediateFlush.xml");
55      }
56  }