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    package org.apache.logging.log4j.perf.jmh;
018    
019    import java.io.File;
020    import java.util.Arrays;
021    import java.util.concurrent.TimeUnit;
022    
023    import org.apache.log4j.LogManager;
024    import org.apache.log4j.Logger;
025    import org.openjdk.jmh.annotations.BenchmarkMode;
026    import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
027    import org.openjdk.jmh.annotations.Level;
028    import org.openjdk.jmh.annotations.Mode;
029    import org.openjdk.jmh.annotations.OutputTimeUnit;
030    import org.openjdk.jmh.annotations.Scope;
031    import org.openjdk.jmh.annotations.Setup;
032    import org.openjdk.jmh.annotations.State;
033    import org.openjdk.jmh.annotations.TearDown;
034    
035    /**
036     * Tests Log4j-1.2 Async Appender performance.
037     */
038    // ============================== HOW TO RUN THIS TEST: ====================================
039    //
040    // single thread:
041    // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5
042    //
043    // multiple threads (for example, 4 threads):
044    // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5 -t 4 -si true
045    //
046    // Usage help:
047    // java -jar log4j-perf/target/microbenchmarks.jar -help
048    //
049    public class Log4j1AsyncAppenderBenchmark {
050    
051        final static char[] CHARS = new char[500];
052        static {
053            Arrays.fill(CHARS, 'a');
054        }
055        final static String TEST = new String(CHARS);
056    
057        @State(Scope.Benchmark)
058        public static class NormalState {
059            Logger logger;
060    
061            @Setup(Level.Trial)
062            public void up() {
063                System.setProperty("log4j.configuration", "perf-log4j12-async.xml");
064                logger = LogManager.getLogger(getClass());
065            }
066    
067            @TearDown(Level.Trial)
068            public void down() {
069                LogManager.shutdown();
070                new File("perftest.log").delete();
071            }
072        }
073    
074        @GenerateMicroBenchmark
075        @BenchmarkMode(Mode.Throughput)
076        @OutputTimeUnit(TimeUnit.SECONDS)
077        public boolean throughputBaseline(final NormalState e) {
078            return e.logger.isInfoEnabled();
079        }
080    
081        @GenerateMicroBenchmark
082        @BenchmarkMode(Mode.Throughput)
083        @OutputTimeUnit(TimeUnit.SECONDS)
084        public void throughput(final NormalState e) {
085            e.logger.info(TEST);
086        }
087    
088        @GenerateMicroBenchmark
089        @BenchmarkMode(Mode.SampleTime)
090        @OutputTimeUnit(TimeUnit.NANOSECONDS)
091        public boolean latencyBaseline(final NormalState e) {
092            return e.logger.isInfoEnabled();
093        }
094    
095        @GenerateMicroBenchmark
096        @BenchmarkMode(Mode.SampleTime)
097        @OutputTimeUnit(TimeUnit.NANOSECONDS)
098        public void latency(final NormalState e) {
099            e.logger.info(TEST);
100        }
101    }