You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2015/08/23 17:03:38 UTC

logging-log4j2 git commit: LOG4J2-930 benchmark to compare log4j2 and logback PatternLayout performance

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 7ec8a13c5 -> f92b4c88b


LOG4J2-930 benchmark to compare log4j2 and logback PatternLayout
performance

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f92b4c88
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f92b4c88
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f92b4c88

Branch: refs/heads/master
Commit: f92b4c88b9544c4938ee492dab6852cdd1999f14
Parents: 7ec8a13
Author: rpopma <rp...@apache.org>
Authored: Mon Aug 24 00:03:33 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Mon Aug 24 00:03:33 2015 +0900

----------------------------------------------------------------------
 .../jmh/PatternLayoutComparisonBenchmark.java   | 127 +++++++++++++++++++
 1 file changed, 127 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f92b4c88/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/PatternLayoutComparisonBenchmark.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/PatternLayoutComparisonBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/PatternLayoutComparisonBenchmark.java
new file mode 100644
index 0000000..3acd892
--- /dev/null
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/PatternLayoutComparisonBenchmark.java
@@ -0,0 +1,127 @@
+/*
+ * 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.perf.jmh;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.Map;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.ThreadContext.ContextStack;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+/**
+ * Compares Log4j2 with Logback PatternLayout performance.
+ */
+// ============================== HOW TO RUN THIS TEST: ====================================
+//
+// single thread:
+// java -jar log4j-perf/target/benchmarks.jar ".*PatternLayoutComparison.*" -f 1 -wi 10 -i 10 -tu ns -bm sample
+//
+// Usage help:
+// java -jar log4j-perf/target/benchmarks.jar -help
+//
+@State(Scope.Thread)
+public class PatternLayoutComparisonBenchmark {
+
+    final static String STR = "AB!(%087936DZYXQWEIOP$#^~-=/><nb"; // length=32
+    final static LogEvent LOG4J2EVENT = createLog4j2Event();
+    private static final Charset CHARSET_DEFAULT = Charset.defaultCharset();
+    private static final String LOG4JPATTERN = "%d %5p [%t] %c{1} %X{transactionId} - %m%n";
+    private final PatternLayout LOG4J2_PATTERN_LAYOUT = PatternLayout.createLayout(LOG4JPATTERN,
+            null, null, CHARSET_DEFAULT, false, true, null, null);
+
+    private static LogEvent createLog4j2Event() {
+        final Marker marker = null;
+        final String fqcn = "com.mycom.myproject.mypackage.MyClass";
+        final Level level = Level.DEBUG;
+        final Message message = new SimpleMessage(STR);
+        final Throwable t = null;
+        final Map<String, String> mdc = null;
+        final ContextStack ndc = null;
+        final String threadName = null;
+        final StackTraceElement location = null;
+        final long timestamp = 12345678;
+
+        return Log4jLogEvent.newBuilder() //
+                .setLoggerName("name(ignored)") //
+                .setMarker(marker) //
+                .setLoggerFqcn(fqcn) //
+                .setLevel(level) //
+                .setMessage(message) //
+                .setThrown(t) //
+                .setContextMap(mdc) //
+                .setContextStack(ndc) //
+                .setThreadName(threadName) //
+                .setSource(location) //
+                .setTimeMillis(timestamp) //
+                .build();
+    }
+
+    // Logback
+    private static final String LOGBACKPATTERN = "%d %5p [%t] %c{0} %X{transactionId} - %m%n";
+    private final PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
+    private final LoggerContext context = new LoggerContext();
+    private final Logger logger = context.getLogger(PatternLayoutComparisonBenchmark.class);
+    private final ILoggingEvent LOGBACKEVENT = makeLoggingEvent(STR);
+    private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+    @Setup
+    public void setUp() throws IOException {
+        patternLayoutEncoder.setPattern(LOGBACKPATTERN);
+        patternLayoutEncoder.setContext(context);
+        patternLayoutEncoder.start();
+        ((ch.qos.logback.classic.PatternLayout) patternLayoutEncoder.getLayout()).setOutputPatternAsHeader(false);
+        patternLayoutEncoder.init(baos);
+    }
+
+    ILoggingEvent makeLoggingEvent(String message) {
+        return new LoggingEvent(PatternLayoutComparisonBenchmark.class.getName(), logger,
+                ch.qos.logback.classic.Level.DEBUG, message, null, null);
+    }
+
+    @Benchmark
+    public byte[] logback() throws IOException {
+        baos.reset();
+        patternLayoutEncoder.doEncode(LOGBACKEVENT);
+        // patternLayoutEncoder.close();
+        return baos.toByteArray();
+    }
+
+    @Benchmark
+    public byte[] log4j2() {
+        return LOG4J2_PATTERN_LAYOUT.toByteArray(LOG4J2EVENT);
+    }
+
+}