View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.perf.jmh;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
23  import org.openjdk.jmh.annotations.Scope;
24  import org.openjdk.jmh.annotations.Setup;
25  import org.openjdk.jmh.annotations.State;
26  import org.openjdk.jmh.annotations.TearDown;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Benchmarks Log4j 2, Log4j 1, and Logback using the DEBUG level which is disabled for this test. One of the primary
31   * performance concerns of logging frameworks is adding minimal overhead when logging is disabled. Some users disable
32   * all logging in production, while others disable finer logging levels in production. This benchmark demonstrates
33   * the overhead in calling {@code logger.isDebugEnabled()} and {@code logger.debug()}.
34   */
35  // HOW TO RUN THIS TEST
36  // java -jar target/microbenchmarks.jar ".*DebugDisabledBenchmark.*" -f 1 -i 5 -wi 5 -bm sample -tu ns
37  @State(Scope.Thread)
38  public class DebugDisabledBenchmark {
39      Logger log4jLogger;
40      org.slf4j.Logger slf4jLogger;
41      org.apache.log4j.Logger log4jClassicLogger;
42      Integer j;
43  
44      @Setup
45      public void setUp() {
46          System.setProperty("log4j.configurationFile", "log4j2-perf2.xml");
47          System.setProperty("log4j.configuration", "log4j12-perf2.xml");
48          System.setProperty("logback.configurationFile", "logback-perf2.xml");
49  
50          log4jLogger = LogManager.getLogger(DebugDisabledBenchmark.class);
51          slf4jLogger = LoggerFactory.getLogger(DebugDisabledBenchmark.class);
52          log4jClassicLogger = org.apache.log4j.Logger.getLogger(DebugDisabledBenchmark.class);
53          j = new Integer(2);
54      }
55  
56      @TearDown
57      public void tearDown() {
58          System.clearProperty("log4j.configurationFile");
59          System.clearProperty("log4j.configuration");
60          System.clearProperty("logback.configurationFile");
61      }
62  
63      @GenerateMicroBenchmark
64      public boolean baseline() {
65          return true;
66      }
67  
68      @GenerateMicroBenchmark
69      public boolean log4jIsDebugEnabled() {
70          return log4jLogger.isDebugEnabled();
71      }
72  
73      @GenerateMicroBenchmark
74      public boolean slf4jIsDebugEnabled() {
75          return slf4jLogger.isDebugEnabled();
76      }
77  
78      @GenerateMicroBenchmark
79      public boolean log4jClassicIsDebugEnabled() {
80          return log4jClassicLogger.isDebugEnabled();
81      }
82  
83      @GenerateMicroBenchmark
84      public void log4jDebugStringConcatenation() {
85          log4jLogger.debug("This is a debug [" + j + "] message");
86      }
87  
88      @GenerateMicroBenchmark
89      public void slf4jDebugStringConcatenation() {
90          slf4jLogger.debug("This is a debug [" + j + "] message");
91      }
92  
93      @GenerateMicroBenchmark
94      public void log4jClassicDebugStringConcatenation() {
95          log4jClassicLogger.debug("This is a debug [" + j + "] message");
96      }
97  
98      @GenerateMicroBenchmark
99      public void log4jDebugParameterizedString() {
100         log4jLogger.debug("This is a debug [{}] message", j);
101     }
102 
103     @GenerateMicroBenchmark
104     public void slf4jDebugParameterizedString() {
105         slf4jLogger.debug("This is a debug [{}] message", j);
106     }
107 }