public
Created — forked from /LogPerfTest.scala

perf test for explicitly checking isDebugEnabled vs passing a closure

  • Download Gist
LogPerfTest.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.