Merge branch 'LOG4J2-Kotlin-JMHBenchmark' into master
diff --git a/log4j-api-kotlin-benchmark/README.md b/log4j-api-kotlin-benchmark/README.md
new file mode 100644
index 0000000..f13be59
--- /dev/null
+++ b/log4j-api-kotlin-benchmark/README.md
@@ -0,0 +1,8 @@
+# [Apache Log4j 2 Kotlin API Bencharmking](http://logging.apache.org/log4j/2.x/)
+
+## Usage
+
+```sh
+mvn install
+java -jar log4j-api-kotlin-benchmark/target/benchmarks.jar
+```
diff --git a/log4j-api-kotlin-benchmark/pom.xml b/log4j-api-kotlin-benchmark/pom.xml
new file mode 100644
index 0000000..07af174
--- /dev/null
+++ b/log4j-api-kotlin-benchmark/pom.xml
@@ -0,0 +1,220 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<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>
+ <parent>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api-kotlin-parent</artifactId>
+ <version>1.0.1-SNAPSHOT</version>
+ <relativePath>../</relativePath>
+ </parent>
+
+ <artifactId>log4j-api-kotlin-benchmark</artifactId>
+ <version>1.0.1-SNAPSHOT</version>
+ <packaging>jar</packaging>
+ <name>Apache Log4j Kotlin API Benchmark</name>
+ <description>Benchmarking the Log4j Kotlin API</description>
+ <url>http://http://logging.apache.org/log4j/2.x/</url>
+
+ <properties>
+ <log4jParentDir>${basedir}/..</log4jParentDir>
+ <jmh.version>1.23</jmh.version>
+ <jmh.generator>default</jmh.generator>
+ <javac.target>1.8</javac.target>
+ <uberjar.name>benchmarks</uberjar.name>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api-kotlin</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.openjdk.jmh</groupId>
+ <artifactId>jmh-core</artifactId>
+ <version>${jmh.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <sourceDirectory>src/main/kotlin</sourceDirectory>
+ <plugins>
+ <!--
+ 1. Compile Kotlin sources first.
+ -->
+
+ <plugin>
+ <artifactId>kotlin-maven-plugin</artifactId>
+ <groupId>org.jetbrains.kotlin</groupId>
+ <version>${kotlin.version}</version>
+ <executions>
+ <execution>
+ <id>process-sources</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <configuration>
+ <sourceDirs>
+ <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
+ </sourceDirs>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!--
+ 2. Invoke JMH generators to produce benchmark code
+ -->
+
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.2.1</version>
+ <executions>
+ <execution>
+ <phase>generate-resources</phase>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ <configuration>
+ <includePluginDependencies>true</includePluginDependencies>
+ <mainClass>org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator</mainClass>
+ <arguments>
+ <argument>${project.basedir}/target/classes/</argument>
+ <argument>${project.basedir}/target/generated-sources/jmh/</argument>
+ <argument>${project.basedir}/target/classes/</argument>
+ <argument>${jmh.generator}</argument>
+ </arguments>
+ </configuration>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.openjdk.jmh</groupId>
+ <artifactId>jmh-generator-bytecode</artifactId>
+ <version>${jmh.version}</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+
+ <!--
+ 3. Add JMH generated code to the compile session.
+ -->
+
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>1.8</version>
+ <executions>
+ <execution>
+ <id>add-source</id>
+ <phase>process-resources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>${project.basedir}/target/generated-sources/jmh</source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!--
+ 4. Compile JMH generated code.
+ -->
+
+ <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>
+ <compilerArgument>-proc:none</compilerArgument>
+ </configuration>
+ <executions>
+ <execution>
+ <id>compile-sources</id>
+ <phase>process-sources</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>compile-all</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!--
+ 5. Package all the dependencies into the JAR
+ -->
+
+ <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>
+ </build>
+</project>
diff --git a/log4j-api-kotlin-benchmark/src/main/kotlin/org/apache/logging/log4j/kotlin/benchmark/LoggingBenchmark.kt b/log4j-api-kotlin-benchmark/src/main/kotlin/org/apache/logging/log4j/kotlin/benchmark/LoggingBenchmark.kt
new file mode 100644
index 0000000..776c4db
--- /dev/null
+++ b/log4j-api-kotlin-benchmark/src/main/kotlin/org/apache/logging/log4j/kotlin/benchmark/LoggingBenchmark.kt
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.kotlin.benchmark
+
+import org.apache.logging.log4j.LogManager
+import org.apache.logging.log4j.Logger
+import org.apache.logging.log4j.kotlin.contextName
+import org.apache.logging.log4j.kotlin.logger
+//import org.apache.logging.log4j.kotlin.logger1
+import org.apache.logging.log4j.util.Supplier
+import org.openjdk.jmh.annotations.*
+import java.util.concurrent.TimeUnit
+
+val LOGGER1 = logger("Bar")
+val LOGGER2 = logger(contextName {})
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Fork(value = 2, jvmArgs = ["-Xms2G", "-Xmx2G"])
+@Warmup(iterations = 3, time = 5)
+@Measurement(iterations = 5, time = 1)
+open class LoggingBenchmark {
+ companion object {
+ @JvmStatic
+ val LOGGER3 = logger()
+ val LOGGER4: Logger = LogManager.getLogger()
+ }
+
+ @Benchmark
+ fun topLevelNamedLoggerFunctional() {
+ LOGGER1.info { "Test" }
+ }
+
+ @Benchmark
+ fun topLevelNamedLoggerDirect() {
+ LOGGER1.info("Test")
+ }
+
+ @Benchmark
+ fun topLevelLoggerWithContextLookupFunctional() {
+ LOGGER2.info {"Test" }
+ }
+
+ @Benchmark
+ fun topLevelLoggerWithContextLookupDirect() {
+ LOGGER2.info("Test")
+ }
+
+ @Benchmark
+ fun companionObjectKotlinLoggerFunctional() {
+ LOGGER3.info { "Test" }
+ }
+
+ @Benchmark
+ fun companionObjectKotlinLoggerDirect() {
+ LOGGER3.info("Test")
+ }
+
+ @Benchmark
+ fun companionObjectLog4jLoggerFunctional() {
+ LOGGER4.info(Supplier { "Test" })
+ }
+
+ @Benchmark
+ fun companionObjectLog4jLoggerDirect() {
+ LOGGER4.info("Test")
+ }
+}
diff --git a/log4j-api-kotlin-benchmark/src/main/resources/log4j2.xml b/log4j-api-kotlin-benchmark/src/main/resources/log4j2.xml
new file mode 100644
index 0000000..a7fd821
--- /dev/null
+++ b/log4j-api-kotlin-benchmark/src/main/resources/log4j2.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration name="KotlinApiSample" status="error">
+ <Appenders>
+ <Null name="Null"/>
+ </Appenders>
+ <Loggers>
+ <Root level="ERROR">
+ <AppenderRef ref="Null"/>
+ </Root>
+ </Loggers>
+</Configuration>
diff --git a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt
index 5548476..7488bf6 100644
--- a/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt
+++ b/log4j-api-kotlin/src/main/kotlin/org/apache/logging/log4j/kotlin/LoggingFactory.kt
@@ -45,7 +45,7 @@
* @param context should always be `{}`
* @return normalized context name
*/
-fun contextName(context: () -> Unit) = with(context::class.java.name) {
+fun contextName(context: () -> Unit): String = with(context::class.java.name) {
when {
contains("Kt$") -> substringBefore("Kt$")
contains("$") -> substringBefore("$")
diff --git a/pom.xml b/pom.xml
index d6e56fb..96b6d4b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -407,5 +407,6 @@
<modules>
<module>log4j-api-kotlin</module>
<module>log4j-api-kotlin-sample</module>
+ <module>log4j-api-kotlin-benchmark</module>
</modules>
</project>