|
package com.foo; |
|
|
|
import org.openjdk.jmh.annotations.Benchmark; |
|
import org.openjdk.jmh.runner.Runner; |
|
import org.openjdk.jmh.runner.RunnerException; |
|
import org.openjdk.jmh.runner.options.Options; |
|
import org.openjdk.jmh.runner.options.OptionsBuilder; |
|
|
|
import java.util.Random; |
|
import java.util.function.Supplier; |
|
import java.util.logging.*; |
|
|
|
public class LogInvocationStyleBenchmark { |
|
static { |
|
//From http://www.nailedtothex.org/roller/kyle/entry/java-util-logging-programmatic-configuration |
|
System.setProperty("java.util.logging.SimpleFormatter.format", |
|
"%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n"); |
|
ConsoleHandler consoleHandler = new ConsoleHandler(); |
|
consoleHandler.setLevel(Level.WARNING); |
|
consoleHandler.setFormatter(new SimpleFormatter()); |
|
|
|
//From http://carminedimascio.com/2013/08/how-to-programmatically-set-java-logging-defaults-and-override-these-defaults-using-logging-properties/ |
|
Logger logger = Logger.getLogger(""); |
|
logger.setLevel(Level.WARNING); |
|
for (Handler handler : logger.getHandlers()) { |
|
logger.removeHandler(handler); |
|
} |
|
logger.addHandler(consoleHandler); |
|
} |
|
|
|
@Benchmark |
|
public void testNoGuardParam() { |
|
final Logger logger = Logger.getLogger(LogInvocationStyleBenchmark.class.getName()); |
|
|
|
//Adapted to j.u.l package from http://www.baeldung.com/log4j-2-lazy-logging |
|
logger.log(Level.INFO, "This is a sample message [{0}]", getRandomNumber()); |
|
} |
|
|
|
@Benchmark |
|
public void testNoGuardConcat() { |
|
final Logger logger = Logger.getLogger(LogInvocationStyleBenchmark.class.getName()); |
|
|
|
//Adapted to j.u.l package from http://www.baeldung.com/log4j-2-lazy-logging |
|
logger.log(Level.INFO, "This is a sample message [" + getRandomNumber() + "]"); |
|
} |
|
|
|
@Benchmark |
|
public void testIfGuardParam() { |
|
final Logger logger = Logger.getLogger(LogInvocationStyleBenchmark.class.getName()); |
|
|
|
//Adapted to j.u.l package from http://www.baeldung.com/log4j-2-lazy-logging |
|
if (logger.isLoggable(Level.INFO)) { |
|
logger.log(Level.INFO, "This is a sample message [{0}]", getRandomNumber()); |
|
} |
|
} |
|
|
|
@Benchmark |
|
public void testIfGuardConcat() { |
|
final Logger logger = Logger.getLogger(LogInvocationStyleBenchmark.class.getName()); |
|
|
|
//Adapted to j.u.l package from http://www.baeldung.com/log4j-2-lazy-logging |
|
if (logger.isLoggable(Level.INFO)) { |
|
logger.log(Level.INFO, "This is a sample message [" + getRandomNumber() + "]"); |
|
} |
|
} |
|
|
|
@Benchmark |
|
public void testLambdaConcat() { |
|
final Logger logger = Logger.getLogger(LogInvocationStyleBenchmark.class.getName()); |
|
|
|
//Adapted to j.u.l package from http://www.baeldung.com/log4j-2-lazy-logging |
|
Supplier<String> msg = () -> "This is a sample message [" + getRandomNumber() + "]"; |
|
logger.log(Level.INFO, msg); |
|
} |
|
|
|
private int getRandomNumber() { |
|
return (new Random()).nextInt(10); |
|
} |
|
|
|
public static void main(String[] args) throws RunnerException { |
|
Options opt = new OptionsBuilder() |
|
.include(LogInvocationStyleBenchmark.class.getSimpleName()) |
|
.forks(1) |
|
.build(); |
|
new Runner(opt).run(); |
|
} |
|
} |