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.io.ByteArrayInputStream;
021    import java.io.IOException;
022    import java.util.concurrent.TimeUnit;
023    
024    import org.apache.logging.log4j.core.LoggerContext;
025    import org.apache.logging.log4j.core.config.ConfigurationSource;
026    import org.apache.logging.log4j.core.config.Configurator;
027    import org.openjdk.jmh.annotations.BenchmarkMode;
028    import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
029    import org.openjdk.jmh.annotations.Mode;
030    import org.openjdk.jmh.annotations.OutputTimeUnit;
031    import org.openjdk.jmh.annotations.Scope;
032    import org.openjdk.jmh.annotations.Setup;
033    import org.openjdk.jmh.annotations.State;
034    
035    /**
036     * This benchmark demonstrates how long it takes for a simple XML configuration file to be parsed and initialize a
037     * new {@link org.apache.logging.log4j.core.LoggerContext} using that configuration.
038     */
039    // TO RUN THIS TEST:
040    // java -jar target/microbenchmarks.jar '.*ConfiguratorInitializeBenchmark.*'
041    @State(Scope.Thread)
042    public class ConfiguratorInitializeBenchmark {
043    
044        private static final String inlineConfigurationXML =
045            "<Configuration name='ConfiguratorInitializeTest' status='off'>" +
046                "<Appenders>" +
047                "<Console name='STDOUT'>" +
048                "<PatternLayout pattern='%m%n'/>" +
049                "</Console>" +
050                "</Appenders>" +
051                "<Loggers>" +
052                "<Root level='error'>" +
053                "<AppenderRef ref='STDOUT'/>" +
054                "</Root>" +
055                "</Loggers>" +
056                "</Configuration>";
057    
058        private ConfigurationSource configurationSource;
059    
060        @Setup
061        public void setUp() throws IOException {
062            configurationSource = new ConfigurationSource(new ByteArrayInputStream(inlineConfigurationXML.getBytes()));
063        }
064    
065        @GenerateMicroBenchmark
066        @BenchmarkMode(Mode.SingleShotTime)
067        @OutputTimeUnit(TimeUnit.MILLISECONDS)
068        public LoggerContext initializeLoggerContext() {
069            return Configurator.initialize(null, configurationSource);
070        }
071    
072    }