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 org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.core.impl.Log4jLogEvent;
021    import org.apache.logging.log4j.message.Message;
022    import org.apache.logging.log4j.message.SimpleMessage;
023    import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
024    import org.openjdk.jmh.annotations.Scope;
025    import org.openjdk.jmh.annotations.Setup;
026    import org.openjdk.jmh.annotations.State;
027    import org.openjdk.jmh.logic.BlackHole;
028    
029    @State(Scope.Thread)
030    public class Log4jLogEventBenchmark {
031        private static Message MESSAGE;
032        private static Throwable ERROR;
033    
034        @Setup
035        public void setup() {
036            MESSAGE = new SimpleMessage("Test message");
037            ERROR = new Exception("test");
038        }
039    
040        @GenerateMicroBenchmark
041        public void testBaseline(BlackHole bh) {
042        }
043    
044        @GenerateMicroBenchmark
045        public void testNoException(BlackHole bh) {
046            Throwable t = null;
047            Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, t);
048            bh.consume(event);
049        }
050    
051        @GenerateMicroBenchmark
052        public void testException(BlackHole bh) {
053            Throwable t = ERROR;
054            Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, t);
055            bh.consume(event);
056        }
057    
058        // ============================== HOW TO RUN THIS TEST: ====================================
059        //
060        // In sampling mode (latency test):
061        // java -jar log4j-perf/target/microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm sample -tu ns
062        //
063        // Throughput test:
064        // java -jar microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm Throughput -tu ms
065        //
066        // Usage help:
067        // java -jar log4j-perf/target/microbenchmarks.jar -help
068        //
069    }