001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.perf.jmh; 018 019 import org.apache.logging.log4j.Level; 020 import org.apache.logging.log4j.core.LogEvent; 021 import org.apache.logging.log4j.core.impl.Log4jLogEvent; 022 import org.apache.logging.log4j.message.Message; 023 import org.apache.logging.log4j.message.SimpleMessage; 024 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; 025 import org.openjdk.jmh.annotations.Scope; 026 import org.openjdk.jmh.annotations.Setup; 027 import org.openjdk.jmh.annotations.State; 028 import org.openjdk.jmh.logic.BlackHole; 029 030 import java.io.Serializable; 031 032 @State(Scope.Thread) 033 public class Log4jLogEventBenchmark { 034 private static Message MESSAGE; 035 private static Throwable ERROR; 036 037 @Setup 038 public void setup() { 039 MESSAGE = new SimpleMessage("Test message"); 040 ERROR = new Exception("test"); 041 } 042 043 @GenerateMicroBenchmark 044 public void testBaseline(final BlackHole bh) { 045 } 046 047 @GenerateMicroBenchmark 048 public LogEvent createLogEventWithoutException() { 049 return new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null); 050 } 051 052 @GenerateMicroBenchmark 053 public LogEvent createLogEventWithoutExceptionUsingBuilder() { 054 return Log4jLogEvent.newBuilder() 055 .setLoggerName("a.b.c") 056 .setLoggerFqcn("a.b.c") 057 .setLevel(Level.INFO) 058 .setMessage(MESSAGE) 059 .build(); 060 } 061 062 @GenerateMicroBenchmark 063 public LogEvent createLogEventWithExceptionUsingBuilder() { 064 return Log4jLogEvent.newBuilder() 065 .setLoggerName("a.b.c") 066 .setLoggerFqcn("a.b.c") 067 .setLevel(Level.INFO) 068 .setMessage(MESSAGE) 069 .setThrown(ERROR) 070 .build(); 071 } 072 073 @GenerateMicroBenchmark 074 public StackTraceElement getSourceLocationOfLogEvent() { 075 final LogEvent event = Log4jLogEvent.newBuilder() 076 .setLoggerName(this.getClass().getName()) 077 .setLoggerFqcn(this.getClass().getName()) 078 .setLevel(Level.INFO) 079 .setMessage(MESSAGE) 080 .build(); 081 event.setIncludeLocation(true); 082 return event.getSource(); 083 } 084 085 @GenerateMicroBenchmark 086 public Serializable createSerializableLogEventProxyWithoutException() { 087 final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null); 088 return Log4jLogEvent.serialize(event, false); 089 } 090 091 @GenerateMicroBenchmark 092 public Serializable createSerializableLogEventProxyWithException(final BlackHole bh) { 093 final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, ERROR); 094 return Log4jLogEvent.serialize(event, false); 095 } 096 097 // ============================== HOW TO RUN THIS TEST: ==================================== 098 // 099 // 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 }