You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2017/02/28 13:44:07 UTC
[2/2] logging-log4j2 git commit: LOG4J2-1827 - Move integration tests
to their own module to speed up build
LOG4J2-1827 - Move integration tests to their own module to speed up build
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/87edda83
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/87edda83
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/87edda83
Branch: refs/heads/master
Commit: 87edda8314e290e37ce46fa68ec8964ffd7be200
Parents: a2172ad
Author: Ralph Goers <rg...@nextiva.com>
Authored: Tue Feb 28 06:43:57 2017 -0700
Committer: Ralph Goers <rg...@nextiva.com>
Committed: Tue Feb 28 06:43:57 2017 -0700
----------------------------------------------------------------------
log4j-core-its/pom.xml | 230 +++++++++++++++++++
.../log4j/FilterPerformanceComparison.java | 214 +++++++++++++++++
.../logging/log4j/PerformanceComparison.java | 203 ++++++++++++++++
.../apache/logging/log4j/PerformanceRun.java | 136 +++++++++++
.../logging/log4j/core/SimplePerfTest.java | 170 ++++++++++++++
.../logging/log4j/core/ThreadedPerfTest.java | 85 +++++++
.../apache/logging/log4j/core/ThreadedTest.java | 138 +++++++++++
.../appender/mom/activemq/JmsAppenderIT.java | 144 ++++++++++++
log4j-core/pom.xml | 10 +-
.../log4j/FilterPerformanceComparison.java | 214 -----------------
.../logging/log4j/PerformanceComparison.java | 203 ----------------
.../apache/logging/log4j/PerformanceRun.java | 136 -----------
.../logging/log4j/core/SimplePerfTest.java | 170 --------------
.../logging/log4j/core/ThreadedPerfTest.java | 85 -------
.../apache/logging/log4j/core/ThreadedTest.java | 138 -----------
.../appender/mom/activemq/JmsAppenderIT.java | 144 ------------
pom.xml | 1 +
17 files changed, 1322 insertions(+), 1099 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-core-its/pom.xml b/log4j-core-its/pom.xml
new file mode 100644
index 0000000..203c4ef
--- /dev/null
+++ b/log4j-core-its/pom.xml
@@ -0,0 +1,230 @@
+<?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.
+ -->
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>2.8.2-SNAPSHOT</version>
+ <relativePath>../</relativePath>
+ </parent>
+ <artifactId>log4j-core-its</artifactId>
+ <packaging>pom</packaging>
+ <name>Apache Log4j Core Integration Tests</name>
+ <description>Integration Tests for the Apache Log4j Implementation</description>
+ <properties>
+ <log4jParentDir>${basedir}/..</log4jParentDir>
+ <docLabel>Core Documentation</docLabel>
+ <projectDir>/core</projectDir>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <!-- Required for AsyncLoggers -->
+ <dependency>
+ <groupId>com.lmax</groupId>
+ <artifactId>disruptor</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- Alternative implementation of BlockingQueue using Conversant Disruptor for AsyncAppender -->
+ <dependency>
+ <groupId>com.conversantmedia</groupId>
+ <artifactId>disruptor</artifactId>
+ <classifier>jdk7</classifier>
+ <optional>true</optional>
+ </dependency>
+ <!-- Alternative implementation of BlockingQueue using JCTools for AsyncAppender -->
+ <dependency>
+ <groupId>org.jctools</groupId>
+ <artifactId>jctools-core</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- Required for JSON support -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- Required for JSON support -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- Required for YAML support (including JSON requirements) -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-yaml</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- Required for XML layout and receiver support -->
+ <dependency>
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-xml</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <!-- POM for jackson-dataformat-xml 2.8.3 depends on woodstox-core 5.0.2 -->
+ <dependency>
+ <groupId>com.fasterxml.woodstox</groupId>
+ <artifactId>woodstox-core</artifactId>
+ <version>5.0.2</version>
+ <optional>true</optional>
+ </dependency>
+
+ <!-- TEST DEPENDENCIES -->
+
+ <!-- Pull in useful test classes from API -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <!-- Log4j 1.2 tests -->
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.17</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- SLF4J tests -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-ext</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- JUnit, naturally -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- Useful mock classes and utilities -->
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- Logback performance tests -->
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- Used for JMS appenders (needs an implementation of course) -->
+ <dependency>
+ <groupId>org.jboss.spec.javax.jms</groupId>
+ <artifactId>jboss-jms-api_1.1_spec</artifactId>
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
+ <!-- JPA, JNDI and JMS tests -->
+ <dependency>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-broker</artifactId>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jms_1.1_spec</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${compiler.plugin.version}</version>
+ <executions>
+ <execution>
+ <id>test-compile</id>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ <phase>test-compile</phase>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skip>true</skip>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*.java</include>
+ </includes>
+ <excludes>
+ <exclude>**/ForceNoDefClassFoundError.*</exclude>
+ </excludes>
+ <groups>
+ org.apache.logging.log4j.categories.PerformanceTests
+ </groups>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
new file mode 100644
index 0000000..1de0e97
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
@@ -0,0 +1,214 @@
+/*
+ * 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;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.MDC;
+
+/**
+ *
+ */
+@Category(PerformanceTests.class)
+public class FilterPerformanceComparison {
+
+ private final Logger logger = LogManager.getLogger(FilterPerformanceComparison.class.getName());
+ private final org.slf4j.Logger logbacklogger = org.slf4j.LoggerFactory.getLogger(FilterPerformanceComparison.class);
+
+
+ // How many times should we try to log:
+ private static final int COUNT = 10000000;
+ private static final int THREADED_COUNT = 100000;
+ private static final int WARMUP = 1000;
+
+ private static final String CONFIG = "log4j2-perf-filter.xml";
+ private static final String LOGBACK_CONFIG = "logback-perf-filter.xml";
+
+ private static final String LOGBACK_CONF = "logback.configurationFile";
+
+ @BeforeClass
+ public static void setupClass() {
+ System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+ System.setProperty(LOGBACK_CONF, LOGBACK_CONFIG);
+ }
+
+ @AfterClass
+ public static void cleanupClass() {
+ System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+ System.clearProperty(LOGBACK_CONF);
+ }
+
+ @After
+ public void after() {
+ ThreadContext.clearAll();
+ }
+
+ @Test
+ public void testPerformanceEmptyContext() throws Exception {
+ testPerformance(Collections.<String, String>emptyMap());
+ }
+
+ @Test
+ public void testPerformanceNonEmptyContext() throws Exception {
+ testPerformance(createNonEmptyContextData());
+ }
+
+ private Map<String, String> createNonEmptyContextData() {
+ final Map<String, String> context = new HashMap<>();
+ context.put("user0", "Apache");
+ context.put("user1", "Apache");
+ return context;
+ }
+
+ private static void putContextData(final Map<String, String> contextData) {
+ ThreadContext.putAll(contextData);
+ for (final Map.Entry<String, String> entry : contextData.entrySet()) {
+ MDC.put(entry.getKey(), entry.getValue());
+ }
+ }
+
+ private void testPerformance(final Map<String, String> contextData) throws Exception {
+ putContextData(contextData);
+ Target.LOGBACK.timedLoop(logger, logbacklogger, WARMUP);
+ Target.LOG4J2.timedLoop(logger, logbacklogger, WARMUP);
+
+ System.out.println("Single-threaded Log4j 2.0, "
+ + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
+
+ final long result3 = Target.LOG4J2.timedLoop(logger, logbacklogger, COUNT);
+ System.out.println("Single-threaded Logback, "
+ + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
+
+ final long result2 = Target.LOGBACK.timedLoop(logger, logbacklogger, COUNT);
+
+ System.out.println("###############################################");
+ System.out.println("Logback: " + result2);
+ System.out.println("Log4j 2.0: " + result3);
+ System.out.println("###############################################");
+ }
+
+ @Test
+ public void testThreadsEmptyContext() throws Exception {
+ testThreads(Collections.<String, String>emptyMap());
+ }
+
+ @Test
+ public void testThreadsNonEmptyContext() throws Exception {
+ testThreads(createNonEmptyContextData());
+ }
+
+ private void testThreads(final Map<String, String> contextData) throws Exception {
+ System.out.println("Testing multithreading");
+ final int threadedCount = COUNT; // THREADED_COUNT * threadCount < COUNT ? COUNT / threadCount : THREADED_COUNT;
+ final int[] threadCounts = new int[] {1, 2, 5, 10, 20, 50};
+ for (final int threadCount : threadCounts) {
+ System.out.println("Testing " + threadCount + " threads, "
+ + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
+ final Worker[] workers = new Worker[threadCount];
+ final long[] results = new long[threadCount];
+ for (int i=0; i < threadCount; ++i) {
+ workers[i] = new Worker(Target.LOG4J2, threadedCount, results, i, contextData);
+ }
+ for (int i=0; i < threadCount; ++i) {
+ workers[i].start();
+ }
+ long total = 0;
+ for (int i=0; i < threadCount; ++i) {
+ workers[i].join();
+ total += results[i];
+ }
+ final long result3 = total / threadCount;
+ total = 0;
+ for (int i=0; i < threadCount; ++i) {
+ workers[i] = new Worker(Target.LOGBACK, threadedCount, results, i, contextData);
+ }
+ for (int i=0; i < threadCount; ++i) {
+ workers[i].start();
+ }
+ for (int i=0; i < threadCount; ++i) {
+ workers[i].join();
+ total += results[i];
+ }
+ final long result2 = total / threadCount;
+ System.out.println("###############################################");
+ System.out.println("Logback: " + result2);
+ System.out.println("Log4j 2.0: " + result3 );
+ System.out.println("###############################################");
+ }
+ }
+
+ private enum Target {
+ LOGBACK {
+ @Override
+ long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop) {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ logbacklogger.debug("SEE IF THIS IS LOGGED {}.", j);
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+ },
+
+ LOG4J2 {
+ @Override
+ long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop) {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ logger.debug("SEE IF THIS IS LOGGED {}.", j);
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+ };
+ abstract long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop);
+ }
+
+ private class Worker extends Thread {
+
+ private final Target target;
+ private final int count;
+ private final long[] results;
+ private final int index;
+ private final Map<String, String> contextData;
+
+ public Worker(final Target target, final int count, final long[] results, final int index,
+ final Map<String, String> contextData) {
+ this.target = target;
+ this.count = count;
+ this.results = results;
+ this.index = index;
+ this.contextData = contextData;
+ }
+
+ @Override
+ public void run() {
+ putContextData(contextData);
+ results[index] = target.timedLoop(logger, logbacklogger, count);
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceComparison.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
new file mode 100644
index 0000000..5c3aad6
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
@@ -0,0 +1,203 @@
+/*
+ * 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;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.util.Profiler;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * Use this class to analyze performance between Log4j and other logging frameworks.
+ */
+@Category(PerformanceTests.class)
+public class PerformanceComparison {
+
+ private final Logger logger = LogManager.getLogger(PerformanceComparison.class.getName());
+ private final org.slf4j.Logger logbacklogger = org.slf4j.LoggerFactory.getLogger(PerformanceComparison.class);
+ private final org.apache.log4j.Logger log4jlogger = org.apache.log4j.Logger.getLogger(PerformanceComparison.class);
+
+
+ // How many times should we try to log:
+ private static final int COUNT = 500000;
+ private static final int PROFILE_COUNT = 500000;
+ private static final int WARMUP = 50000;
+
+ private static final String CONFIG = "log4j2-perf.xml";
+ private static final String LOGBACK_CONFIG = "logback-perf.xml";
+ private static final String LOG4J_CONFIG = "log4j12-perf.xml";
+
+ private static final String LOGBACK_CONF = "logback.configurationFile";
+ private static final String LOG4J_CONF = "log4j.configuration";
+
+ @BeforeClass
+ public static void setupClass() {
+ System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+ System.setProperty(LOGBACK_CONF, LOGBACK_CONFIG);
+ System.setProperty(LOG4J_CONF, LOG4J_CONFIG);
+ }
+
+ @AfterClass
+ public static void cleanupClass() {
+ System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+ System.clearProperty(LOGBACK_CONF);
+ System.clearProperty(LOG4J_CONF);
+ new File("target/testlog4j.log").deleteOnExit();
+ new File("target/testlog4j2.log").deleteOnExit();
+ new File("target/testlogback.log").deleteOnExit();
+ }
+
+ @Test
+ public void testPerformance() throws Exception {
+
+ log4j(WARMUP);
+ logback(WARMUP);
+ log4j2(WARMUP);
+
+ if (Profiler.isActive()) {
+ System.out.println("Profiling Log4j 2.0");
+ Profiler.start();
+ final long result = log4j2(PROFILE_COUNT);
+ Profiler.stop();
+ System.out.println("###############################################");
+ System.out.println("Log4j 2.0: " + result);
+ System.out.println("###############################################");
+ } else {
+ doRun();
+ doRun();
+ doRun();
+ doRun();
+ }
+ }
+
+ private void doRun() {
+ System.out.print("Log4j : ");
+ System.out.println(log4j(COUNT));
+
+ System.out.print("Logback : ");
+ System.out.println(logback(COUNT));
+
+ System.out.print("Log4j 2.0: ");
+ System.out.println(log4j2(COUNT));
+
+ System.out.println("###############################################");
+ }
+
+ //@Test
+ public void testRawPerformance() throws Exception {
+ final OutputStream os = new FileOutputStream("target/testos.log", true);
+ final long result1 = writeToStream(COUNT, os);
+ os.close();
+ final OutputStream bos = new BufferedOutputStream(new FileOutputStream("target/testbuffer.log", true));
+ final long result2 = writeToStream(COUNT, bos);
+ bos.close();
+ final Writer w = new FileWriter("target/testwriter.log", true);
+ final long result3 = writeToWriter(COUNT, w);
+ w.close();
+ final FileOutputStream cos = new FileOutputStream("target/testchannel.log", true);
+ final FileChannel channel = cos.getChannel();
+ final long result4 = writeToChannel(COUNT, channel);
+ cos.close();
+ System.out.println("###############################################");
+ System.out.println("FileOutputStream: " + result1);
+ System.out.println("BufferedOutputStream: " + result2);
+ System.out.println("FileWriter: " + result3);
+ System.out.println("FileChannel: " + result4);
+ System.out.println("###############################################");
+ }
+
+ private long log4j(final int loop) {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ log4jlogger.debug("SEE IF THIS IS LOGGED " + j + '.');
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long logback(final int loop) {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ logbacklogger.debug("SEE IF THIS IS LOGGED " + j + '.');
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+
+ private long log4j2(final int loop) {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ logger.debug("SEE IF THIS IS LOGGED " + j + '.');
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+
+ private long writeToWriter(final int loop, final Writer w) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ w.write("SEE IF THIS IS LOGGED " + j + '.');
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long writeToStream(final int loop, final OutputStream os) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ os.write(getBytes("SEE IF THIS IS LOGGED " + j + '.'));
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long writeToChannel(final int loop, final FileChannel channel) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final ByteBuffer buf = ByteBuffer.allocateDirect(8*1024);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ channel.write(getByteBuffer(buf, "SEE IF THIS IS LOGGED " + j + '.'));
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private ByteBuffer getByteBuffer(final ByteBuffer buf, final String s) {
+ buf.clear();
+ buf.put(s.getBytes());
+ buf.flip();
+ return buf;
+ }
+
+ private byte[] getBytes(final String s) {
+ return s.getBytes();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceRun.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceRun.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceRun.java
new file mode 100644
index 0000000..64f0e35
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/PerformanceRun.java
@@ -0,0 +1,136 @@
+/*
+ * 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;
+
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.ClassRule;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * Use this class to analyze Log4j-only performance.
+ * <p>
+ * See {@linkplain PerformanceComparison} to compare performance with other logging frameworks.
+ * </p>
+ */
+@Category(PerformanceTests.class)
+public class PerformanceRun {
+
+ private static final String CONFIG = "log4j2-perf.xml";
+
+ @ClassRule
+ public static LoggerContextRule context = new LoggerContextRule(CONFIG);
+
+ private final Logger logger = context.getLogger(PerformanceRun.class.getName());
+
+ // How many times should we try to log:
+ private static final int COUNT = 1000000;
+
+ @Test
+ public void testPerformance() throws Exception {
+ System.out.println("Starting Log4j 2.0");
+ final long result3 = log4j2(COUNT);
+
+ System.out.println("###############################################");
+ System.out.println("Log4j 2.0: " + result3);
+ System.out.println("###############################################");
+ }
+
+ @Test
+ @Ignore("Why was this test disabled?")
+ public void testRawPerformance() throws Exception {
+ final OutputStream os = new FileOutputStream("target/testos.log", true);
+ final long result1 = writeToStream(COUNT, os);
+ os.close();
+ final OutputStream bos = new BufferedOutputStream(new FileOutputStream("target/testbuffer.log", true));
+ final long result2 = writeToStream(COUNT, bos);
+ bos.close();
+ final Writer w = new FileWriter("target/testwriter.log", true);
+ final long result3 = writeToWriter(COUNT, w);
+ w.close();
+ final FileOutputStream cos = new FileOutputStream("target/testchannel.log", true);
+ final FileChannel channel = cos.getChannel();
+ final long result4 = writeToChannel(COUNT, channel);
+ cos.close();
+ System.out.println("###############################################");
+ System.out.println("FileOutputStream: " + result1);
+ System.out.println("BufferedOutputStream: " + result2);
+ System.out.println("FileWriter: " + result3);
+ System.out.println("FileChannel: " + result4);
+ System.out.println("###############################################");
+ }
+
+ private long log4j2(final int loop) {
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("SEE IF THIS IS LOGGED");
+ }
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long writeToWriter(final int loop, final Writer w) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ w.write("SEE IF THIS IS LOGGED " + j + '.');
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long writeToStream(final int loop, final OutputStream os) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ os.write(getBytes("SEE IF THIS IS LOGGED " + j + '.'));
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private long writeToChannel(final int loop, final FileChannel channel) throws Exception {
+ final Integer j = Integer.valueOf(2);
+ final ByteBuffer buf = ByteBuffer.allocateDirect(8 * 1024);
+ final long start = System.nanoTime();
+ for (int i = 0; i < loop; i++) {
+ channel.write(getByteBuffer(buf, "SEE IF THIS IS LOGGED " + j + '.'));
+ }
+ return (System.nanoTime() - start) / loop;
+ }
+
+ private ByteBuffer getByteBuffer(final ByteBuffer buf, final String s) {
+ buf.clear();
+ buf.put(s.getBytes());
+ buf.flip();
+ return buf;
+ }
+
+ private byte[] getBytes(final String s) {
+ return s.getBytes();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
new file mode 100644
index 0000000..e4ae209
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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.core;
+
+import java.util.Random;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.util.Timer;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ */
+@Category(PerformanceTests.class)
+public class SimplePerfTest {
+
+ private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(SimplePerfTest.class.getName());
+ private volatile Level lvl = Level.DEBUG;
+ private static final int LOOP_CNT = 10000000;
+ private static final int WARMUP = 1000;
+ private static long maxTime;
+ private static Random rand = new SimpleRandom();
+ private static int RAND_SIZE = 250;
+ private static int[] values = new int[RAND_SIZE];
+
+ @BeforeClass
+ public static void setupClass() {
+
+ final Configuration config = LoggerContext.getContext().getConfiguration();
+
+ if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
+ System.out.println("Configuration was " + config.getName());
+ LoggerContext.getContext().start(new DefaultConfiguration());
+ }
+
+ for (int i=0; i < WARMUP; ++i) {
+ overhead();
+ }
+ System.gc();
+ final Timer timer = new Timer("Setup", LOOP_CNT);
+ timer.start();
+ for (int i=0; i < (LOOP_CNT / 150); ++i) {
+ overhead();
+ }
+ timer.stop();
+ maxTime = timer.getElapsedNanoTime();
+ System.gc();
+ System.out.println(timer.toString());
+ }
+
+ @Test
+ public void debugDisabled() {
+ System.gc();
+ final Timer timer = new Timer("DebugDisabled", LOOP_CNT);
+ timer.start();
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.isDebugEnabled();
+ }
+ timer.stop();
+ System.out.println(timer.toString());
+ assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
+ }
+
+ @Test
+ public void debugDisabledByLevel() {
+ System.gc();
+ final Timer timer = new Timer("DebugDisabled", LOOP_CNT);
+ timer.start();
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.isEnabled(Level.DEBUG);
+ }
+ timer.stop();
+ System.out.println(timer.toString());
+ assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
+ }
+
+ @Test
+ public void debugLogger() {
+ System.gc();
+ final Timer timer = new Timer("DebugLogger", LOOP_CNT);
+ final String msg = "This is a test";
+ timer.start();
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.debug(msg);
+ }
+ timer.stop();
+ System.out.println(timer.toString());
+ assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
+ }
+ /*
+ @Test
+ public void errorLogger() {
+ Timer timer = new Timer("ErrorLogger", 10);
+ timer.start();
+ for (int i=0; i < 10; ++i) {
+ logger.error("This is a test");
+ }
+ timer.stop();
+ System.out.println(timer.toString());
+ } */
+
+ /*
+ * Try to generate some overhead that can't be optimized well. Not sure how accurate this is,
+ * but the point is simply to insure that changes made don't suddenly cause performance issues.
+ */
+ private static void overhead() {
+ final int values[] = new int[RAND_SIZE];
+ final Random rand = new SimpleRandom();
+
+ for (int i = 0; i < RAND_SIZE; ++i) {
+ values[i] = rand.nextInt();
+ }
+ bubbleSort(values);
+ }
+
+ private static class SimpleRandom extends Random {
+ /**
+ * Generated serial version ID.
+ */
+ private static final long serialVersionUID = 3517002855516031846L;
+ private int low = 5;
+ private int high = 55;
+
+ @Override
+ public int nextInt() {
+ high = 36969 * (high & 65535) + (high >> 16);
+ low = 18000 * (low & 65535) + (low >> 16);
+ return (high << 16) + low;
+ }
+ }
+
+ /**
+ * Standard BubbleSort algorithm.
+ * @param array The array to sort.
+ */
+ private static void bubbleSort(final int array[]) {
+ final int length = array.length;
+ for (int i = 0; i < length; i++) {
+ for (int j = 1; j > length - i; j++) {
+ if (array[j-1] > array[j]) {
+ final int temp = array[j-1];
+ array[j-1] = array[j];
+ array[j] = temp;
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
new file mode 100644
index 0000000..781ae8a
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.core;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.core.util.Timer;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ *
+ */
+@Category(PerformanceTests.class)
+public class ThreadedPerfTest {
+
+ private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(ThreadedPerfTest.class.getName());
+ private volatile Level lvl = Level.DEBUG;
+ private static final int LOOP_CNT = 10000000;
+ private static final int THREADS = 10;
+
+ @Test
+ public void debugDisabled() {
+ final Timer timer = new Timer("DebugDisabled", LOOP_CNT * THREADS);
+ final Runnable runnable = new DebugDisabledRunnable();
+ final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
+ timer.start();
+ for (int i=0; i < THREADS; ++i) {
+ pool.execute(runnable);
+ }
+ pool.shutdown();
+ timer.stop();
+ System.out.println(timer.toString());
+ }
+
+ @Test
+ public void debugLogger() {
+ final Timer timer = new Timer("DebugLogger", LOOP_CNT * THREADS);
+ final Runnable runnable = new DebugLoggerRunnable();
+ final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
+ timer.start();
+ for (int i=0; i < THREADS; ++i) {
+ pool.execute(runnable);
+ }
+ pool.shutdown();
+ timer.stop();
+ System.out.println(timer.toString());
+ }
+
+ public static class DebugDisabledRunnable implements Runnable {
+ @Override
+ public void run() {
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.isDebugEnabled();
+ }
+ }
+ }
+
+ public static class DebugLoggerRunnable implements Runnable {
+ @Override
+ public void run() {
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.debug("This is a test");
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedTest.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedTest.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedTest.java
new file mode 100644
index 0000000..3df298a
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.core;
+
+import java.io.File;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.categories.PerformanceTests;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ *
+ */
+@Category(PerformanceTests.class)
+public class ThreadedTest {
+ private static final String DIR = "target/threaded";
+ private static final String CONFIG = "log4j-threaded.xml";
+ private static final int LOOP_CNT = 25;
+ private static final int THREADS = 4;
+ private static final AtomicInteger counter = new AtomicInteger(0);
+ private static final LoggerContextRule context = new LoggerContextRule(CONFIG);
+
+ private final Logger logger = context.getLogger(ThreadedTest.class.getName());
+ private volatile Level lvl = Level.DEBUG;
+
+ // this would look pretty sweet with lambdas
+ @ClassRule
+ public static RuleChain chain = RuleChain.outerRule(new TestRule() {
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ deleteDir();
+ try {
+ base.evaluate();
+ } finally {
+ deleteDir();
+ }
+ }
+ };
+ }
+ }).around(context);
+
+ @Test
+ public void testDeadlock() throws Exception {
+ final ExecutorService pool = Executors.newFixedThreadPool(THREADS * 2);
+ final State state = new State();
+ for (int count=0; count < THREADS; ++count) {
+ pool.execute(new LoggingRunnable(state));
+ pool.execute(new StateSettingRunnable(state));
+ }
+ Thread.sleep(250);
+ pool.shutdown();
+ System.out.println("Counter = " + counter);
+ }
+
+ public class LoggingRunnable implements Runnable {
+ private final State state;
+
+ public LoggingRunnable(final State state) {
+ this.state = state;
+ }
+ @Override
+ public void run() {
+ for (int i=0; i < LOOP_CNT; ++i) {
+ logger.debug(state);
+ }
+ }
+ }
+ public class StateSettingRunnable implements Runnable {
+ private final State state;
+
+ public StateSettingRunnable(final State state) {
+ this.state = state;
+ }
+ @Override
+ public void run() {
+ for (int i=0; i < LOOP_CNT*4; ++i) {
+ Thread.yield();
+ state.setState();
+ }
+ }
+ }
+
+ private static void deleteDir() {
+ final File dir = new File(DIR);
+ if (dir.exists()) {
+ final File[] files = dir.listFiles();
+ for (final File file : files) {
+ file.delete();
+ }
+ dir.delete();
+ }
+ }
+
+ class State {
+
+ synchronized void setState() {
+ // Something takes a long time here
+ logger.debug("hello world");
+ }
+
+ synchronized Object getState() {
+ return counter.incrementAndGet();
+ }
+
+ @Override
+ public String toString() {
+ return "state=" + getState();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core-its/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java
----------------------------------------------------------------------
diff --git a/log4j-core-its/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java
new file mode 100644
index 0000000..effdff0
--- /dev/null
+++ b/log4j-core-its/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java
@@ -0,0 +1,144 @@
+/*
+ * 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.core.appender.mom.activemq;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+
+import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.categories.Appenders;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.mom.JmsAppender;
+import org.apache.logging.log4j.core.appender.mom.JmsManager;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.core.net.JndiManager;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import static org.junit.Assert.*;
+
+/**
+ * Integration test for JmsAppender using an embedded ActiveMQ broker.
+ */
+@Category(Appenders.Jms.class)
+public class JmsAppenderIT {
+
+ private static final String KEY_SERIALIZABLE_PACKAGES = "org.apache.activemq.SERIALIZABLE_PACKAGES";
+
+ private static JmsManager jmsManager;
+
+ private JmsAppender appender;
+
+ @BeforeClass
+ public static void setUpClass() {
+ System.setProperty(KEY_SERIALIZABLE_PACKAGES,
+ "org.apache.logging.log4j.core.impl,org.apache.logging.log4j.util,org.apache.logging.log4j");
+ final Properties additional = new Properties();
+ additional.setProperty("queue.TestQueue", "TestQueue");
+ final JndiManager jndiManager = JndiManager.getJndiManager(ActiveMQInitialContextFactory.class.getName(),
+ "vm://localhost?broker.persistent=false", null, null, null, additional);
+ jmsManager = JmsManager.getJmsManager("JmsManager", jndiManager, "ConnectionFactory", "TestQueue", null, null);
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ jmsManager.close();
+ System.getProperties().remove(KEY_SERIALIZABLE_PACKAGES);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ // @formatter:off
+ appender = JmsAppender.newBuilder().
+ setName("JmsAppender").
+ setLayout(SerializedLayout.createLayout()).
+ setIgnoreExceptions(true).
+ setJmsManager(jmsManager).
+ build();
+ // @formatter:off
+ appender.start();
+ }
+
+ @Test
+ public void testLogToQueue() throws Exception {
+ final int messageCount = 100;
+ final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
+ final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount);
+ messageConsumer.setMessageListener(consumer);
+ final String messageText = "Hello, World!";
+ final String loggerName = this.getClass().getName();
+ for (int i = 0; i < messageCount; i++) {
+ final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName(loggerName) //
+ .setLoggerFqcn(loggerName).setLevel(Level.INFO) //
+ .setMessage(new SimpleMessage(messageText)).setThreadName(Thread.currentThread().getName()) //
+ .setTimeMillis(System.currentTimeMillis()).build();
+ appender.append(event);
+ }
+ consumer.awaitAndAssertAllMessagesConsumed();
+ }
+
+ private static class JmsQueueConsumer implements MessageListener {
+
+ private final int messageCount;
+ private final CountDownLatch countDownLatch;
+ private final Collection<LogEvent> events;
+
+ private JmsQueueConsumer(final int messageCount) {
+ this.messageCount = messageCount;
+ this.countDownLatch = new CountDownLatch(messageCount);
+ this.events = new ArrayList<>(messageCount);
+ }
+
+ @Override
+ public void onMessage(final Message message) {
+ try {
+ consume((ObjectMessage) message);
+ } catch (final JMSException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void consume(final ObjectMessage message) throws JMSException {
+ try {
+ final LogEvent event = (LogEvent) message.getObject();
+ events.add(event);
+ } finally {
+ countDownLatch.countDown();
+ }
+ }
+
+ public void awaitAndAssertAllMessagesConsumed() throws InterruptedException {
+ countDownLatch.await(5, TimeUnit.SECONDS);
+ assertEquals(messageCount, events.size());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-core/pom.xml b/log4j-core/pom.xml
index c0744f7..a761223 100644
--- a/log4j-core/pom.xml
+++ b/log4j-core/pom.xml
@@ -383,15 +383,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
- <includes>
- <include>**/*.java</include>
- </includes>
- <excludes>
- <exclude>**/ForceNoDefClassFoundError.*</exclude>
- </excludes>
- <groups>
- org.apache.logging.log4j.categories.PerformanceTests
- </groups>
+ <skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java b/log4j-core/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
deleted file mode 100644
index 1de0e97..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/FilterPerformanceComparison.java
+++ /dev/null
@@ -1,214 +0,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.
- */
-package org.apache.logging.log4j;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.logging.log4j.categories.PerformanceTests;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.slf4j.MDC;
-
-/**
- *
- */
-@Category(PerformanceTests.class)
-public class FilterPerformanceComparison {
-
- private final Logger logger = LogManager.getLogger(FilterPerformanceComparison.class.getName());
- private final org.slf4j.Logger logbacklogger = org.slf4j.LoggerFactory.getLogger(FilterPerformanceComparison.class);
-
-
- // How many times should we try to log:
- private static final int COUNT = 10000000;
- private static final int THREADED_COUNT = 100000;
- private static final int WARMUP = 1000;
-
- private static final String CONFIG = "log4j2-perf-filter.xml";
- private static final String LOGBACK_CONFIG = "logback-perf-filter.xml";
-
- private static final String LOGBACK_CONF = "logback.configurationFile";
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- System.setProperty(LOGBACK_CONF, LOGBACK_CONFIG);
- }
-
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- System.clearProperty(LOGBACK_CONF);
- }
-
- @After
- public void after() {
- ThreadContext.clearAll();
- }
-
- @Test
- public void testPerformanceEmptyContext() throws Exception {
- testPerformance(Collections.<String, String>emptyMap());
- }
-
- @Test
- public void testPerformanceNonEmptyContext() throws Exception {
- testPerformance(createNonEmptyContextData());
- }
-
- private Map<String, String> createNonEmptyContextData() {
- final Map<String, String> context = new HashMap<>();
- context.put("user0", "Apache");
- context.put("user1", "Apache");
- return context;
- }
-
- private static void putContextData(final Map<String, String> contextData) {
- ThreadContext.putAll(contextData);
- for (final Map.Entry<String, String> entry : contextData.entrySet()) {
- MDC.put(entry.getKey(), entry.getValue());
- }
- }
-
- private void testPerformance(final Map<String, String> contextData) throws Exception {
- putContextData(contextData);
- Target.LOGBACK.timedLoop(logger, logbacklogger, WARMUP);
- Target.LOG4J2.timedLoop(logger, logbacklogger, WARMUP);
-
- System.out.println("Single-threaded Log4j 2.0, "
- + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
-
- final long result3 = Target.LOG4J2.timedLoop(logger, logbacklogger, COUNT);
- System.out.println("Single-threaded Logback, "
- + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
-
- final long result2 = Target.LOGBACK.timedLoop(logger, logbacklogger, COUNT);
-
- System.out.println("###############################################");
- System.out.println("Logback: " + result2);
- System.out.println("Log4j 2.0: " + result3);
- System.out.println("###############################################");
- }
-
- @Test
- public void testThreadsEmptyContext() throws Exception {
- testThreads(Collections.<String, String>emptyMap());
- }
-
- @Test
- public void testThreadsNonEmptyContext() throws Exception {
- testThreads(createNonEmptyContextData());
- }
-
- private void testThreads(final Map<String, String> contextData) throws Exception {
- System.out.println("Testing multithreading");
- final int threadedCount = COUNT; // THREADED_COUNT * threadCount < COUNT ? COUNT / threadCount : THREADED_COUNT;
- final int[] threadCounts = new int[] {1, 2, 5, 10, 20, 50};
- for (final int threadCount : threadCounts) {
- System.out.println("Testing " + threadCount + " threads, "
- + (contextData.isEmpty() ? "EMPTY context" : "NON-EMPTY context"));
- final Worker[] workers = new Worker[threadCount];
- final long[] results = new long[threadCount];
- for (int i=0; i < threadCount; ++i) {
- workers[i] = new Worker(Target.LOG4J2, threadedCount, results, i, contextData);
- }
- for (int i=0; i < threadCount; ++i) {
- workers[i].start();
- }
- long total = 0;
- for (int i=0; i < threadCount; ++i) {
- workers[i].join();
- total += results[i];
- }
- final long result3 = total / threadCount;
- total = 0;
- for (int i=0; i < threadCount; ++i) {
- workers[i] = new Worker(Target.LOGBACK, threadedCount, results, i, contextData);
- }
- for (int i=0; i < threadCount; ++i) {
- workers[i].start();
- }
- for (int i=0; i < threadCount; ++i) {
- workers[i].join();
- total += results[i];
- }
- final long result2 = total / threadCount;
- System.out.println("###############################################");
- System.out.println("Logback: " + result2);
- System.out.println("Log4j 2.0: " + result3 );
- System.out.println("###############################################");
- }
- }
-
- private enum Target {
- LOGBACK {
- @Override
- long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop) {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- logbacklogger.debug("SEE IF THIS IS LOGGED {}.", j);
- }
- return (System.nanoTime() - start) / loop;
- }
- },
-
- LOG4J2 {
- @Override
- long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop) {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- logger.debug("SEE IF THIS IS LOGGED {}.", j);
- }
- return (System.nanoTime() - start) / loop;
- }
- };
- abstract long timedLoop(final Logger logger, final org.slf4j.Logger logbacklogger, final int loop);
- }
-
- private class Worker extends Thread {
-
- private final Target target;
- private final int count;
- private final long[] results;
- private final int index;
- private final Map<String, String> contextData;
-
- public Worker(final Target target, final int count, final long[] results, final int index,
- final Map<String, String> contextData) {
- this.target = target;
- this.count = count;
- this.results = results;
- this.index = index;
- this.contextData = contextData;
- }
-
- @Override
- public void run() {
- putContextData(contextData);
- results[index] = target.timedLoop(logger, logbacklogger, count);
- }
- }
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceComparison.java b/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
deleted file mode 100644
index 5c3aad6..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceComparison.java
+++ /dev/null
@@ -1,203 +0,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.
- */
-package org.apache.logging.log4j;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.OutputStream;
-import java.io.Writer;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-
-import org.apache.logging.log4j.categories.PerformanceTests;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.util.Profiler;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-/**
- * Use this class to analyze performance between Log4j and other logging frameworks.
- */
-@Category(PerformanceTests.class)
-public class PerformanceComparison {
-
- private final Logger logger = LogManager.getLogger(PerformanceComparison.class.getName());
- private final org.slf4j.Logger logbacklogger = org.slf4j.LoggerFactory.getLogger(PerformanceComparison.class);
- private final org.apache.log4j.Logger log4jlogger = org.apache.log4j.Logger.getLogger(PerformanceComparison.class);
-
-
- // How many times should we try to log:
- private static final int COUNT = 500000;
- private static final int PROFILE_COUNT = 500000;
- private static final int WARMUP = 50000;
-
- private static final String CONFIG = "log4j2-perf.xml";
- private static final String LOGBACK_CONFIG = "logback-perf.xml";
- private static final String LOG4J_CONFIG = "log4j12-perf.xml";
-
- private static final String LOGBACK_CONF = "logback.configurationFile";
- private static final String LOG4J_CONF = "log4j.configuration";
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- System.setProperty(LOGBACK_CONF, LOGBACK_CONFIG);
- System.setProperty(LOG4J_CONF, LOG4J_CONFIG);
- }
-
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- System.clearProperty(LOGBACK_CONF);
- System.clearProperty(LOG4J_CONF);
- new File("target/testlog4j.log").deleteOnExit();
- new File("target/testlog4j2.log").deleteOnExit();
- new File("target/testlogback.log").deleteOnExit();
- }
-
- @Test
- public void testPerformance() throws Exception {
-
- log4j(WARMUP);
- logback(WARMUP);
- log4j2(WARMUP);
-
- if (Profiler.isActive()) {
- System.out.println("Profiling Log4j 2.0");
- Profiler.start();
- final long result = log4j2(PROFILE_COUNT);
- Profiler.stop();
- System.out.println("###############################################");
- System.out.println("Log4j 2.0: " + result);
- System.out.println("###############################################");
- } else {
- doRun();
- doRun();
- doRun();
- doRun();
- }
- }
-
- private void doRun() {
- System.out.print("Log4j : ");
- System.out.println(log4j(COUNT));
-
- System.out.print("Logback : ");
- System.out.println(logback(COUNT));
-
- System.out.print("Log4j 2.0: ");
- System.out.println(log4j2(COUNT));
-
- System.out.println("###############################################");
- }
-
- //@Test
- public void testRawPerformance() throws Exception {
- final OutputStream os = new FileOutputStream("target/testos.log", true);
- final long result1 = writeToStream(COUNT, os);
- os.close();
- final OutputStream bos = new BufferedOutputStream(new FileOutputStream("target/testbuffer.log", true));
- final long result2 = writeToStream(COUNT, bos);
- bos.close();
- final Writer w = new FileWriter("target/testwriter.log", true);
- final long result3 = writeToWriter(COUNT, w);
- w.close();
- final FileOutputStream cos = new FileOutputStream("target/testchannel.log", true);
- final FileChannel channel = cos.getChannel();
- final long result4 = writeToChannel(COUNT, channel);
- cos.close();
- System.out.println("###############################################");
- System.out.println("FileOutputStream: " + result1);
- System.out.println("BufferedOutputStream: " + result2);
- System.out.println("FileWriter: " + result3);
- System.out.println("FileChannel: " + result4);
- System.out.println("###############################################");
- }
-
- private long log4j(final int loop) {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- log4jlogger.debug("SEE IF THIS IS LOGGED " + j + '.');
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long logback(final int loop) {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- logbacklogger.debug("SEE IF THIS IS LOGGED " + j + '.');
- }
- return (System.nanoTime() - start) / loop;
- }
-
-
- private long log4j2(final int loop) {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- logger.debug("SEE IF THIS IS LOGGED " + j + '.');
- }
- return (System.nanoTime() - start) / loop;
- }
-
-
- private long writeToWriter(final int loop, final Writer w) throws Exception {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- w.write("SEE IF THIS IS LOGGED " + j + '.');
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long writeToStream(final int loop, final OutputStream os) throws Exception {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- os.write(getBytes("SEE IF THIS IS LOGGED " + j + '.'));
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long writeToChannel(final int loop, final FileChannel channel) throws Exception {
- final Integer j = Integer.valueOf(2);
- final ByteBuffer buf = ByteBuffer.allocateDirect(8*1024);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- channel.write(getByteBuffer(buf, "SEE IF THIS IS LOGGED " + j + '.'));
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private ByteBuffer getByteBuffer(final ByteBuffer buf, final String s) {
- buf.clear();
- buf.put(s.getBytes());
- buf.flip();
- return buf;
- }
-
- private byte[] getBytes(final String s) {
- return s.getBytes();
- }
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceRun.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceRun.java b/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceRun.java
deleted file mode 100644
index 64f0e35..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/PerformanceRun.java
+++ /dev/null
@@ -1,136 +0,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.
- */
-package org.apache.logging.log4j;
-
-import java.io.BufferedOutputStream;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.OutputStream;
-import java.io.Writer;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-
-import org.apache.logging.log4j.categories.PerformanceTests;
-import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.ClassRule;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-/**
- * Use this class to analyze Log4j-only performance.
- * <p>
- * See {@linkplain PerformanceComparison} to compare performance with other logging frameworks.
- * </p>
- */
-@Category(PerformanceTests.class)
-public class PerformanceRun {
-
- private static final String CONFIG = "log4j2-perf.xml";
-
- @ClassRule
- public static LoggerContextRule context = new LoggerContextRule(CONFIG);
-
- private final Logger logger = context.getLogger(PerformanceRun.class.getName());
-
- // How many times should we try to log:
- private static final int COUNT = 1000000;
-
- @Test
- public void testPerformance() throws Exception {
- System.out.println("Starting Log4j 2.0");
- final long result3 = log4j2(COUNT);
-
- System.out.println("###############################################");
- System.out.println("Log4j 2.0: " + result3);
- System.out.println("###############################################");
- }
-
- @Test
- @Ignore("Why was this test disabled?")
- public void testRawPerformance() throws Exception {
- final OutputStream os = new FileOutputStream("target/testos.log", true);
- final long result1 = writeToStream(COUNT, os);
- os.close();
- final OutputStream bos = new BufferedOutputStream(new FileOutputStream("target/testbuffer.log", true));
- final long result2 = writeToStream(COUNT, bos);
- bos.close();
- final Writer w = new FileWriter("target/testwriter.log", true);
- final long result3 = writeToWriter(COUNT, w);
- w.close();
- final FileOutputStream cos = new FileOutputStream("target/testchannel.log", true);
- final FileChannel channel = cos.getChannel();
- final long result4 = writeToChannel(COUNT, channel);
- cos.close();
- System.out.println("###############################################");
- System.out.println("FileOutputStream: " + result1);
- System.out.println("BufferedOutputStream: " + result2);
- System.out.println("FileWriter: " + result3);
- System.out.println("FileChannel: " + result4);
- System.out.println("###############################################");
- }
-
- private long log4j2(final int loop) {
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- if (logger.isDebugEnabled()) {
- logger.debug("SEE IF THIS IS LOGGED");
- }
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long writeToWriter(final int loop, final Writer w) throws Exception {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- w.write("SEE IF THIS IS LOGGED " + j + '.');
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long writeToStream(final int loop, final OutputStream os) throws Exception {
- final Integer j = Integer.valueOf(2);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- os.write(getBytes("SEE IF THIS IS LOGGED " + j + '.'));
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private long writeToChannel(final int loop, final FileChannel channel) throws Exception {
- final Integer j = Integer.valueOf(2);
- final ByteBuffer buf = ByteBuffer.allocateDirect(8 * 1024);
- final long start = System.nanoTime();
- for (int i = 0; i < loop; i++) {
- channel.write(getByteBuffer(buf, "SEE IF THIS IS LOGGED " + j + '.'));
- }
- return (System.nanoTime() - start) / loop;
- }
-
- private ByteBuffer getByteBuffer(final ByteBuffer buf, final String s) {
- buf.clear();
- buf.put(s.getBytes());
- buf.flip();
- return buf;
- }
-
- private byte[] getBytes(final String s) {
- return s.getBytes();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
deleted file mode 100644
index e4ae209..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java
+++ /dev/null
@@ -1,170 +0,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.
- */
-package org.apache.logging.log4j.core;
-
-import java.util.Random;
-
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.categories.PerformanceTests;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.DefaultConfiguration;
-import org.apache.logging.log4j.core.util.Timer;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import static org.junit.Assert.*;
-
-/**
- *
- */
-@Category(PerformanceTests.class)
-public class SimplePerfTest {
-
- private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(SimplePerfTest.class.getName());
- private volatile Level lvl = Level.DEBUG;
- private static final int LOOP_CNT = 10000000;
- private static final int WARMUP = 1000;
- private static long maxTime;
- private static Random rand = new SimpleRandom();
- private static int RAND_SIZE = 250;
- private static int[] values = new int[RAND_SIZE];
-
- @BeforeClass
- public static void setupClass() {
-
- final Configuration config = LoggerContext.getContext().getConfiguration();
-
- if (!DefaultConfiguration.DEFAULT_NAME.equals(config.getName())) {
- System.out.println("Configuration was " + config.getName());
- LoggerContext.getContext().start(new DefaultConfiguration());
- }
-
- for (int i=0; i < WARMUP; ++i) {
- overhead();
- }
- System.gc();
- final Timer timer = new Timer("Setup", LOOP_CNT);
- timer.start();
- for (int i=0; i < (LOOP_CNT / 150); ++i) {
- overhead();
- }
- timer.stop();
- maxTime = timer.getElapsedNanoTime();
- System.gc();
- System.out.println(timer.toString());
- }
-
- @Test
- public void debugDisabled() {
- System.gc();
- final Timer timer = new Timer("DebugDisabled", LOOP_CNT);
- timer.start();
- for (int i=0; i < LOOP_CNT; ++i) {
- logger.isDebugEnabled();
- }
- timer.stop();
- System.out.println(timer.toString());
- assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
- }
-
- @Test
- public void debugDisabledByLevel() {
- System.gc();
- final Timer timer = new Timer("DebugDisabled", LOOP_CNT);
- timer.start();
- for (int i=0; i < LOOP_CNT; ++i) {
- logger.isEnabled(Level.DEBUG);
- }
- timer.stop();
- System.out.println(timer.toString());
- assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
- }
-
- @Test
- public void debugLogger() {
- System.gc();
- final Timer timer = new Timer("DebugLogger", LOOP_CNT);
- final String msg = "This is a test";
- timer.start();
- for (int i=0; i < LOOP_CNT; ++i) {
- logger.debug(msg);
- }
- timer.stop();
- System.out.println(timer.toString());
- assertTrue("Timer exceeded max time of " + maxTime, maxTime > timer.getElapsedNanoTime());
- }
- /*
- @Test
- public void errorLogger() {
- Timer timer = new Timer("ErrorLogger", 10);
- timer.start();
- for (int i=0; i < 10; ++i) {
- logger.error("This is a test");
- }
- timer.stop();
- System.out.println(timer.toString());
- } */
-
- /*
- * Try to generate some overhead that can't be optimized well. Not sure how accurate this is,
- * but the point is simply to insure that changes made don't suddenly cause performance issues.
- */
- private static void overhead() {
- final int values[] = new int[RAND_SIZE];
- final Random rand = new SimpleRandom();
-
- for (int i = 0; i < RAND_SIZE; ++i) {
- values[i] = rand.nextInt();
- }
- bubbleSort(values);
- }
-
- private static class SimpleRandom extends Random {
- /**
- * Generated serial version ID.
- */
- private static final long serialVersionUID = 3517002855516031846L;
- private int low = 5;
- private int high = 55;
-
- @Override
- public int nextInt() {
- high = 36969 * (high & 65535) + (high >> 16);
- low = 18000 * (low & 65535) + (low >> 16);
- return (high << 16) + low;
- }
- }
-
- /**
- * Standard BubbleSort algorithm.
- * @param array The array to sort.
- */
- private static void bubbleSort(final int array[]) {
- final int length = array.length;
- for (int i = 0; i < length; i++) {
- for (int j = 1; j > length - i; j++) {
- if (array[j-1] > array[j]) {
- final int temp = array[j-1];
- array[j-1] = array[j];
- array[j] = temp;
- }
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/87edda83/log4j-core/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
deleted file mode 100644
index 781ae8a..0000000
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java
+++ /dev/null
@@ -1,85 +0,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.
- */
-package org.apache.logging.log4j.core;
-
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.categories.PerformanceTests;
-import org.apache.logging.log4j.core.util.Timer;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-/**
- *
- */
-@Category(PerformanceTests.class)
-public class ThreadedPerfTest {
-
- private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(ThreadedPerfTest.class.getName());
- private volatile Level lvl = Level.DEBUG;
- private static final int LOOP_CNT = 10000000;
- private static final int THREADS = 10;
-
- @Test
- public void debugDisabled() {
- final Timer timer = new Timer("DebugDisabled", LOOP_CNT * THREADS);
- final Runnable runnable = new DebugDisabledRunnable();
- final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
- timer.start();
- for (int i=0; i < THREADS; ++i) {
- pool.execute(runnable);
- }
- pool.shutdown();
- timer.stop();
- System.out.println(timer.toString());
- }
-
- @Test
- public void debugLogger() {
- final Timer timer = new Timer("DebugLogger", LOOP_CNT * THREADS);
- final Runnable runnable = new DebugLoggerRunnable();
- final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
- timer.start();
- for (int i=0; i < THREADS; ++i) {
- pool.execute(runnable);
- }
- pool.shutdown();
- timer.stop();
- System.out.println(timer.toString());
- }
-
- public static class DebugDisabledRunnable implements Runnable {
- @Override
- public void run() {
- for (int i=0; i < LOOP_CNT; ++i) {
- logger.isDebugEnabled();
- }
- }
- }
-
- public static class DebugLoggerRunnable implements Runnable {
- @Override
- public void run() {
- for (int i=0; i < LOOP_CNT; ++i) {
- logger.debug("This is a test");
- }
- }
- }
-}