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 java.util.concurrent.TimeUnit;
21  
22  import org.apache.logging.log4j.Level;
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.LoggerContext;
26  import org.apache.logging.log4j.core.config.Configuration;
27  import org.apache.logging.log4j.core.config.DefaultConfiguration;
28  import org.openjdk.jmh.annotations.BenchmarkMode;
29  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
30  import org.openjdk.jmh.annotations.Mode;
31  import org.openjdk.jmh.annotations.OutputTimeUnit;
32  import org.openjdk.jmh.annotations.Scope;
33  import org.openjdk.jmh.annotations.Setup;
34  import org.openjdk.jmh.annotations.State;
35  import org.openjdk.jmh.logic.BlackHole;
36  
37  /**
38   * Tests the overhead of disabled logging.
39   */
40  // ============================== HOW TO RUN THIS TEST: ====================================
41  //
42  // java -jar log4j-perf/target/microbenchmarks.jar ".*SimpleBenchmark.*" -f 1 -wi 5 -i 5
43  //
44  // Usage help:
45  // java -jar log4j-perf/target/microbenchmarks.jar -help
46  //
47  @State(Scope.Thread)
48  public class SimpleBenchmark {
49      private static final String msg = "This is a test";
50      private Logger logger;
51  
52      @Setup
53      public void setup() {
54          final Configuration config = ((LoggerContext) LogManager.getContext()).getConfiguration();
55          if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
56              System.out.println("Configuration was " + config.getName());
57              ((LoggerContext) LogManager.getContext()).start(new DefaultConfiguration());
58          }
59          logger = LogManager.getLogger(SimpleBenchmark.class.getName());
60      }
61  
62      @BenchmarkMode(Mode.Throughput)
63      @OutputTimeUnit(TimeUnit.SECONDS)
64      @GenerateMicroBenchmark
65      public void testBaselineThroughput(final BlackHole bh) {
66      }
67  
68      @BenchmarkMode(Mode.Throughput)
69      @OutputTimeUnit(TimeUnit.SECONDS)
70      @GenerateMicroBenchmark
71      public void testIsDebugEnabledThroughput(final BlackHole bh) {
72          bh.consume(logger.isDebugEnabled());
73      }
74  
75      @BenchmarkMode(Mode.Throughput)
76      @OutputTimeUnit(TimeUnit.SECONDS)
77      @GenerateMicroBenchmark
78      public void testIsEnabledLevelThroughput(final BlackHole bh) {
79          bh.consume(logger.isEnabled(Level.DEBUG));
80      }
81  
82      @BenchmarkMode(Mode.Throughput)
83      @OutputTimeUnit(TimeUnit.SECONDS)
84      @GenerateMicroBenchmark
85      public void testDebugDisabledThroughput(final BlackHole bh) {
86          logger.debug(msg);
87      }
88  
89  
90      @BenchmarkMode(Mode.SampleTime)
91      @OutputTimeUnit(TimeUnit.NANOSECONDS)
92      @GenerateMicroBenchmark
93      public void testBaselineResponseTime(final BlackHole bh) {
94      }
95  
96      @BenchmarkMode(Mode.SampleTime)
97      @OutputTimeUnit(TimeUnit.NANOSECONDS)
98      @GenerateMicroBenchmark
99      public void testIsDebugEnabledResponseTime(final BlackHole bh) {
100         bh.consume(logger.isDebugEnabled());
101     }
102 
103     @BenchmarkMode(Mode.SampleTime)
104     @OutputTimeUnit(TimeUnit.NANOSECONDS)
105     @GenerateMicroBenchmark
106     public void testIsEnabledLevelResponseTime(final BlackHole bh) {
107         bh.consume(logger.isEnabled(Level.DEBUG));
108     }
109 
110     @BenchmarkMode(Mode.SampleTime)
111     @OutputTimeUnit(TimeUnit.NANOSECONDS)
112     @GenerateMicroBenchmark
113     public void testDebugDisabledResponseTime(final BlackHole bh) {
114         logger.debug(msg);
115     }
116 }