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.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.apache.logging.log4j.core.LoggerContext;
25  import org.apache.logging.log4j.core.config.ConfigurationSource;
26  import org.apache.logging.log4j.core.config.Configurator;
27  import org.openjdk.jmh.annotations.BenchmarkMode;
28  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
29  import org.openjdk.jmh.annotations.Mode;
30  import org.openjdk.jmh.annotations.OutputTimeUnit;
31  import org.openjdk.jmh.annotations.Scope;
32  import org.openjdk.jmh.annotations.Setup;
33  import org.openjdk.jmh.annotations.State;
34  
35  /**
36   * This benchmark demonstrates how long it takes for a simple XML configuration file to be parsed and initialize a
37   * new {@link org.apache.logging.log4j.core.LoggerContext} using that configuration.
38   */
39  // TO RUN THIS TEST:
40  // java -jar target/microbenchmarks.jar '.*ConfiguratorInitializeBenchmark.*'
41  @State(Scope.Thread)
42  public class ConfiguratorInitializeBenchmark {
43  
44      private static final String inlineConfigurationXML =
45          "<Configuration name='ConfiguratorInitializeTest' status='off'>" +
46              "<Appenders>" +
47              "<Console name='STDOUT'>" +
48              "<PatternLayout pattern='%m%n'/>" +
49              "</Console>" +
50              "</Appenders>" +
51              "<Loggers>" +
52              "<Root level='error'>" +
53              "<AppenderRef ref='STDOUT'/>" +
54              "</Root>" +
55              "</Loggers>" +
56              "</Configuration>";
57  
58      private ConfigurationSource configurationSource;
59  
60      @Setup
61      public void setUp() throws IOException {
62          configurationSource = new ConfigurationSource(new ByteArrayInputStream(inlineConfigurationXML.getBytes()));
63      }
64  
65      @GenerateMicroBenchmark
66      @BenchmarkMode(Mode.SingleShotTime)
67      @OutputTimeUnit(TimeUnit.MILLISECONDS)
68      public LoggerContext initializeLoggerContext() {
69          return Configurator.initialize(null, configurationSource);
70      }
71  
72  }