-
-
Save stonegao/1344225 to your computer and use it in GitHub Desktop.
perf test for explicitly checking isDebugEnabled vs passing a closure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.log4j.{Logger, Level} | |
object LogPerfTest { | |
val logger = Logger.getLogger(this.getClass) | |
def main(args: Array[String]) { | |
val stmt = "This is a test log statement" | |
var iterations = args(0).toInt | |
for(logLevel <- List(Level.DEBUG, Level.INFO)) { | |
var i = 0 | |
logger.setLevel(logLevel) | |
var start = System.nanoTime | |
while(i < iterations) { | |
if(logger.isDebugEnabled) | |
logger.debug(stmt) | |
i += 1 | |
} | |
var ellapsed: Double = System.nanoTime - start | |
System.err.println("ifDebugEnabled with logging = " + logLevel + ": " + (ellapsed / iterations) + " ns per invocation") | |
System.err.flush() | |
i = 0 | |
start = System.nanoTime | |
while(i < iterations) { | |
debug(stmt) | |
i += 1 | |
} | |
ellapsed = System.nanoTime - start | |
System.err.println("Debug helper with logging = " + logLevel + ": " + (ellapsed / iterations) + " ns per invocation") | |
System.err.flush() | |
} | |
} | |
def debug(stmt: => String) = | |
if(logger.isDebugEnabled) | |
logger.debug(stmt) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment