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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
21  import org.apache.logging.log4j.message.Message;
22  import org.apache.logging.log4j.message.SimpleMessage;
23  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
24  import org.openjdk.jmh.annotations.Scope;
25  import org.openjdk.jmh.annotations.Setup;
26  import org.openjdk.jmh.annotations.State;
27  import org.openjdk.jmh.logic.BlackHole;
28  
29  @State(Scope.Thread)
30  public class Log4jLogEventBenchmark {
31      private static Message MESSAGE;
32      private static Throwable ERROR;
33  
34      @Setup
35      public void setup() {
36          MESSAGE = new SimpleMessage("Test message");
37          ERROR = new Exception("test");
38      }
39  
40      @GenerateMicroBenchmark
41      public void testBaseline(BlackHole bh) {
42      }
43  
44      @GenerateMicroBenchmark
45      public void testNoException(BlackHole bh) {
46          Throwable t = null;
47          Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, t);
48          bh.consume(event);
49      }
50  
51      @GenerateMicroBenchmark
52      public void testException(BlackHole bh) {
53          Throwable t = ERROR;
54          Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, t);
55          bh.consume(event);
56      }
57  
58      // ============================== HOW TO RUN THIS TEST: ====================================
59      //
60      // In sampling mode (latency test):
61      // java -jar log4j-perf/target/microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm sample -tu ns
62      //
63      // Throughput test:
64      // java -jar microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm Throughput -tu ms
65      //
66      // Usage help:
67      // java -jar log4j-perf/target/microbenchmarks.jar -help
68      //
69  }