001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    
018    package org.apache.logging.log4j.perf.jmh;
019    
020    import java.util.concurrent.TimeUnit;
021    
022    import org.apache.logging.log4j.Level;
023    import org.apache.logging.log4j.LogManager;
024    import org.apache.logging.log4j.Logger;
025    import org.apache.logging.log4j.core.LoggerContext;
026    import org.apache.logging.log4j.core.config.Configuration;
027    import org.apache.logging.log4j.core.config.DefaultConfiguration;
028    import org.openjdk.jmh.annotations.BenchmarkMode;
029    import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
030    import org.openjdk.jmh.annotations.Mode;
031    import org.openjdk.jmh.annotations.OutputTimeUnit;
032    import org.openjdk.jmh.annotations.Scope;
033    import org.openjdk.jmh.annotations.Setup;
034    import org.openjdk.jmh.annotations.State;
035    import org.openjdk.jmh.logic.BlackHole;
036    
037    /**
038     * Tests the overhead of disabled logging.
039     */
040    // ============================== HOW TO RUN THIS TEST: ====================================
041    //
042    // java -jar log4j-perf/target/microbenchmarks.jar ".*SimpleBenchmark.*" -f 1 -wi 5 -i 5
043    //
044    // Usage help:
045    // java -jar log4j-perf/target/microbenchmarks.jar -help
046    //
047    @State(Scope.Thread)
048    public class SimpleBenchmark {
049        private static final String msg = "This is a test";
050        private Logger logger;
051    
052        @Setup
053        public void setup() {
054            final Configuration config = ((LoggerContext) LogManager.getContext()).getConfiguration();
055            if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
056                System.out.println("Configuration was " + config.getName());
057                ((LoggerContext) LogManager.getContext()).start(new DefaultConfiguration());
058            }
059            logger = LogManager.getLogger(SimpleBenchmark.class.getName());
060        }
061    
062        @BenchmarkMode(Mode.Throughput)
063        @OutputTimeUnit(TimeUnit.SECONDS)
064        @GenerateMicroBenchmark
065        public void testBaselineThroughput(final BlackHole bh) {
066        }
067    
068        @BenchmarkMode(Mode.Throughput)
069        @OutputTimeUnit(TimeUnit.SECONDS)
070        @GenerateMicroBenchmark
071        public void testIsDebugEnabledThroughput(final BlackHole bh) {
072            bh.consume(logger.isDebugEnabled());
073        }
074    
075        @BenchmarkMode(Mode.Throughput)
076        @OutputTimeUnit(TimeUnit.SECONDS)
077        @GenerateMicroBenchmark
078        public void testIsEnabledLevelThroughput(final BlackHole bh) {
079            bh.consume(logger.isEnabled(Level.DEBUG));
080        }
081    
082        @BenchmarkMode(Mode.Throughput)
083        @OutputTimeUnit(TimeUnit.SECONDS)
084        @GenerateMicroBenchmark
085        public void testDebugDisabledThroughput(final BlackHole bh) {
086            logger.debug(msg);
087        }
088    
089    
090        @BenchmarkMode(Mode.SampleTime)
091        @OutputTimeUnit(TimeUnit.NANOSECONDS)
092        @GenerateMicroBenchmark
093        public void testBaselineResponseTime(final BlackHole bh) {
094        }
095    
096        @BenchmarkMode(Mode.SampleTime)
097        @OutputTimeUnit(TimeUnit.NANOSECONDS)
098        @GenerateMicroBenchmark
099        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    }