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  package org.apache.logging.log4j.perf.jmh;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.openjdk.jmh.annotations.BenchmarkMode;
24  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
25  import org.openjdk.jmh.annotations.Level;
26  import org.openjdk.jmh.annotations.Mode;
27  import org.openjdk.jmh.annotations.OutputTimeUnit;
28  import org.openjdk.jmh.annotations.Scope;
29  import org.openjdk.jmh.annotations.Setup;
30  import org.openjdk.jmh.annotations.State;
31  import org.openjdk.jmh.annotations.TearDown;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import ch.qos.logback.core.spi.LifeCycle;
36  
37  /**
38   * Tests Logback Async Appender 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  public class LogbackAsyncAppenderBenchmark {
52  
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      @State(Scope.Benchmark)
60      public static class NormalState {
61          Logger logger;
62  
63          @Setup(Level.Trial)
64          public void up() {
65              System.setProperty("logback.configurationFile", "perf-logback-async.xml");
66              logger = (Logger) LoggerFactory.getLogger(getClass());
67          }
68  
69          @TearDown(Level.Trial)
70          public void down() {
71              ((LifeCycle) LoggerFactory.getILoggerFactory()).stop();
72              new File("perftest.log").delete();
73          }
74      }
75  
76      @GenerateMicroBenchmark
77      @BenchmarkMode(Mode.Throughput)
78      @OutputTimeUnit(TimeUnit.SECONDS)
79      public boolean throughputBaseline(final NormalState e) {
80          return e.logger.isInfoEnabled();
81      }
82  
83      @GenerateMicroBenchmark
84      @BenchmarkMode(Mode.Throughput)
85      @OutputTimeUnit(TimeUnit.SECONDS)
86      public void throughput(final NormalState e) {
87          e.logger.info(TEST);
88      }
89  
90      @GenerateMicroBenchmark
91      @BenchmarkMode(Mode.SampleTime)
92      @OutputTimeUnit(TimeUnit.NANOSECONDS)
93      public boolean latencyBaseline(final NormalState e) {
94          return e.logger.isInfoEnabled();
95      }
96  
97      @GenerateMicroBenchmark
98      @BenchmarkMode(Mode.SampleTime)
99      @OutputTimeUnit(TimeUnit.NANOSECONDS)
100     public void latency(final NormalState e) {
101         e.logger.info(TEST);
102     }
103 }