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 018 package org.apache.logging.log4j.perf.jmh; 019 020 import org.apache.logging.log4j.LogManager; 021 import org.apache.logging.log4j.Logger; 022 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; 023 import org.openjdk.jmh.annotations.Scope; 024 import org.openjdk.jmh.annotations.Setup; 025 import org.openjdk.jmh.annotations.State; 026 import org.openjdk.jmh.annotations.TearDown; 027 import org.slf4j.LoggerFactory; 028 029 /** 030 * Benchmarks Log4j 2, Log4j 1, and Logback using the DEBUG level which is disabled for this test. One of the primary 031 * performance concerns of logging frameworks is adding minimal overhead when logging is disabled. Some users disable 032 * all logging in production, while others disable finer logging levels in production. This benchmark demonstrates 033 * the overhead in calling {@code logger.isDebugEnabled()} and {@code logger.debug()}. 034 */ 035 // HOW TO RUN THIS TEST 036 // java -jar target/microbenchmarks.jar ".*DebugDisabledBenchmark.*" -f 1 -i 5 -wi 5 -bm sample -tu ns 037 @State(Scope.Thread) 038 public class DebugDisabledBenchmark { 039 Logger log4jLogger; 040 org.slf4j.Logger slf4jLogger; 041 org.apache.log4j.Logger log4jClassicLogger; 042 Integer j; 043 044 @Setup 045 public void setUp() { 046 System.setProperty("log4j.configurationFile", "log4j2-perf2.xml"); 047 System.setProperty("log4j.configuration", "log4j12-perf2.xml"); 048 System.setProperty("logback.configurationFile", "logback-perf2.xml"); 049 050 log4jLogger = LogManager.getLogger(DebugDisabledBenchmark.class); 051 slf4jLogger = LoggerFactory.getLogger(DebugDisabledBenchmark.class); 052 log4jClassicLogger = org.apache.log4j.Logger.getLogger(DebugDisabledBenchmark.class); 053 j = new Integer(2); 054 } 055 056 @TearDown 057 public void tearDown() { 058 System.clearProperty("log4j.configurationFile"); 059 System.clearProperty("log4j.configuration"); 060 System.clearProperty("logback.configurationFile"); 061 } 062 063 @GenerateMicroBenchmark 064 public boolean baseline() { 065 return true; 066 } 067 068 @GenerateMicroBenchmark 069 public boolean log4jIsDebugEnabled() { 070 return log4jLogger.isDebugEnabled(); 071 } 072 073 @GenerateMicroBenchmark 074 public boolean slf4jIsDebugEnabled() { 075 return slf4jLogger.isDebugEnabled(); 076 } 077 078 @GenerateMicroBenchmark 079 public boolean log4jClassicIsDebugEnabled() { 080 return log4jClassicLogger.isDebugEnabled(); 081 } 082 083 @GenerateMicroBenchmark 084 public void log4jDebugStringConcatenation() { 085 log4jLogger.debug("This is a debug [" + j + "] message"); 086 } 087 088 @GenerateMicroBenchmark 089 public void slf4jDebugStringConcatenation() { 090 slf4jLogger.debug("This is a debug [" + j + "] message"); 091 } 092 093 @GenerateMicroBenchmark 094 public void log4jClassicDebugStringConcatenation() { 095 log4jClassicLogger.debug("This is a debug [" + j + "] message"); 096 } 097 098 @GenerateMicroBenchmark 099 public void log4jDebugParameterizedString() { 100 log4jLogger.debug("This is a debug [{}] message", j); 101 } 102 103 @GenerateMicroBenchmark 104 public void slf4jDebugParameterizedString() { 105 slf4jLogger.debug("This is a debug [{}] message", j); 106 } 107 }