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