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.File;
21  import java.util.Arrays;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.core.LifeCycle;
27  import org.openjdk.jmh.annotations.BenchmarkMode;
28  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
29  import org.openjdk.jmh.annotations.Level;
30  import org.openjdk.jmh.annotations.Mode;
31  import org.openjdk.jmh.annotations.OutputTimeUnit;
32  import org.openjdk.jmh.annotations.Scope;
33  import org.openjdk.jmh.annotations.Setup;
34  import org.openjdk.jmh.annotations.State;
35  import org.openjdk.jmh.annotations.TearDown;
36  
37  /**
38   * Tests Log4j2 Async Loggers performance.
39   */
40  // ============================== HOW TO RUN THIS TEST: ====================================
41  //
42  // single thread:
43  // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5
44  //
45  // multiple threads (for example, 4 threads):
46  // java -jar log4j-perf/target/microbenchmarks.jar ".*Async.*Benchmark.*" -f 1 -wi 5 -i 5 -t 4 -si true
47  //
48  // Usage help:
49  // java -jar log4j-perf/target/microbenchmarks.jar -help
50  //
51  @State(Scope.Thread)
52  public class AsyncLoggersBenchmark {
53      final static char[] CHARS = new char[500];
54      static {
55          Arrays.fill(CHARS, 'a');
56      }
57      final static String TEST = new String(CHARS);
58  
59      Logger logger;
60  
61      @Setup(Level.Trial)
62      public void up() {
63          new File("perftest.log").delete();
64          System.setProperty("log4j.configurationFile", "perf3PlainNoLoc.xml");
65           System.setProperty("Log4jContextSelector",
66           "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
67          logger = LogManager.getLogger(getClass());
68      }
69  
70      @TearDown(Level.Trial)
71      public void down() {
72          ((LifeCycle) LogManager.getContext(false)).stop();
73          new File("perftest.log").delete();
74      }
75  
76      @GenerateMicroBenchmark
77      @BenchmarkMode(Mode.Throughput)
78      @OutputTimeUnit(TimeUnit.SECONDS)
79      public boolean throughputBaseline() {
80          return logger.isInfoEnabled();
81      }
82  
83      @GenerateMicroBenchmark
84      @BenchmarkMode(Mode.Throughput)
85      @OutputTimeUnit(TimeUnit.SECONDS)
86      public void throughput() {
87          logger.info(TEST);
88      }
89  
90      @GenerateMicroBenchmark
91      @BenchmarkMode(Mode.SampleTime)
92      @OutputTimeUnit(TimeUnit.NANOSECONDS)
93      public boolean latencyBaseline() {
94          return logger.isInfoEnabled();
95      }
96  
97      @GenerateMicroBenchmark
98      @BenchmarkMode(Mode.SampleTime)
99      @OutputTimeUnit(TimeUnit.NANOSECONDS)
100     public void latency() {
101         logger.info(TEST);
102     }
103 }