Skip to content

Instantly share code, notes, and snippets.

@AshwinJay
Last active August 27, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshwinJay/09287018c6e8f43d40f373595cb406d2 to your computer and use it in GitHub Desktop.
Save AshwinJay/09287018c6e8f43d40f373595cb406d2 to your computer and use it in GitHub Desktop.
Java logger invocation style benchmark

Java logger invocation style benchmark

A simple test to satisfy my curiosity about lambda peformance and the code it generates.

Borrows from http://www.baeldung.com/log4j-2-lazy-logging but tests against java.util.logging.* package.

Additionaly, you can use javap to see the code it generates: javap -l -v -p -c $YOUR_BUILD_DIR/com/foo/LogInvocationStyleBenchmark.class

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();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>lambda-log-perf</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JMH benchmark sample: Java</name>
<!--
This is the demo/sample template build script for building Java benchmarks with JMH.
Edit as needed.
-->
<prerequisites>
<maven>3.0</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jmh.version>1.19</jmh.version>
<javac.target>1.8</javac.target>
<uberjar.name>benchmarks</uberjar.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment