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.LogEvent;
21  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
22  import org.apache.logging.log4j.message.Message;
23  import org.apache.logging.log4j.message.SimpleMessage;
24  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
25  import org.openjdk.jmh.annotations.Scope;
26  import org.openjdk.jmh.annotations.Setup;
27  import org.openjdk.jmh.annotations.State;
28  import org.openjdk.jmh.logic.BlackHole;
29  
30  import java.io.Serializable;
31  
32  @State(Scope.Thread)
33  public class Log4jLogEventBenchmark {
34      private static Message MESSAGE;
35      private static Throwable ERROR;
36  
37      @Setup
38      public void setup() {
39          MESSAGE = new SimpleMessage("Test message");
40          ERROR = new Exception("test");
41      }
42  
43      @GenerateMicroBenchmark
44      public void testBaseline(final BlackHole bh) {
45      }
46  
47      @GenerateMicroBenchmark
48      public LogEvent createLogEventWithoutException() {
49          return new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null);
50      }
51  
52      @GenerateMicroBenchmark
53      public LogEvent createLogEventWithoutExceptionUsingBuilder() {
54          return Log4jLogEvent.newBuilder()
55              .setLoggerName("a.b.c")
56              .setLoggerFqcn("a.b.c")
57              .setLevel(Level.INFO)
58              .setMessage(MESSAGE)
59              .build();
60      }
61  
62      @GenerateMicroBenchmark
63      public LogEvent createLogEventWithExceptionUsingBuilder() {
64          return Log4jLogEvent.newBuilder()
65              .setLoggerName("a.b.c")
66              .setLoggerFqcn("a.b.c")
67              .setLevel(Level.INFO)
68              .setMessage(MESSAGE)
69              .setThrown(ERROR)
70              .build();
71      }
72  
73      @GenerateMicroBenchmark
74      public StackTraceElement getSourceLocationOfLogEvent() {
75          final LogEvent event = Log4jLogEvent.newBuilder()
76              .setLoggerName(this.getClass().getName())
77              .setLoggerFqcn(this.getClass().getName())
78              .setLevel(Level.INFO)
79              .setMessage(MESSAGE)
80              .build();
81          event.setIncludeLocation(true);
82          return event.getSource();
83      }
84  
85      @GenerateMicroBenchmark
86      public Serializable createSerializableLogEventProxyWithoutException() {
87          final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null);
88          return Log4jLogEvent.serialize(event, false);
89      }
90  
91      @GenerateMicroBenchmark
92      public Serializable createSerializableLogEventProxyWithException(final BlackHole bh) {
93          final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, ERROR);
94          return Log4jLogEvent.serialize(event, false);
95      }
96  
97      // ============================== HOW TO RUN THIS TEST: ====================================
98      //
99      // In sampling mode (latency test):
100     // java -jar log4j-perf/target/microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm sample -tu ns
101     //
102     // Throughput test:
103     // java -jar microbenchmarks.jar ".*Log4jLogEventBenchmark.*" -i 5 -f 1 -wi 5 -bm Throughput -tu ms
104     //
105     // Usage help:
106     // java -jar log4j-perf/target/microbenchmarks.jar -help
107     //
108 }