You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/04/14 16:02:12 UTC

logging-log4j2 git commit: Improved GelfLayoutBenchmark

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 8f5e2759e -> aaaaaad31


Improved GelfLayoutBenchmark


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

Branch: refs/heads/master
Commit: aaaaaad31cb24b3d6cdb078d491011baa5f0212b
Parents: 8f5e275
Author: Mikael Ståldal <mi...@magine.com>
Authored: Thu Apr 14 16:02:04 2016 +0200
Committer: Mikael Ståldal <mi...@magine.com>
Committed: Thu Apr 14 16:02:04 2016 +0200

----------------------------------------------------------------------
 .../log4j/perf/jmh/GelfLayoutBenchmark.java     | 67 +++++++++++++-----
 .../logging/log4j/perf/util/DemoAppender.java   | 71 ++++++++++++++++++++
 .../src/main/resources/log4j2-gelf-perf.xml     | 30 ---------
 3 files changed, 122 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aaaaaad3/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
index 870bc3d..ee2f143 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/GelfLayoutBenchmark.java
@@ -17,44 +17,79 @@
 
 package org.apache.logging.log4j.perf.jmh;
 
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.GelfLayout;
+import org.apache.logging.log4j.core.util.KeyValuePair;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.perf.util.DemoAppender;
 import org.openjdk.jmh.annotations.*;
 
-import java.io.File;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
- * Benchmarks Log4j 2 GelfLayout with a FileAppender.
+ * Benchmarks Log4j 2 GelfLayout.
  */
 // HOW TO RUN THIS TEST
 // java -jar target/benchmarks.jar GelfLayoutBenchmark -f 1 -i 5 -wi 5 -bm sample -tu ns
 @State(Scope.Thread)
 public class GelfLayoutBenchmark {
-    public static final String MESSAGE =
+    private static final CharSequence MESSAGE =
             "This is rather long and chatty log message with quite some interesting information and a bit of fun in it which is suitable here";
+    private static final LogEvent EVENT = createLogEvent();
+    private static final KeyValuePair[] ADDITIONAL_FIELDS = new KeyValuePair[0];
 
-    Logger log4j2Logger;
-    org.apache.log4j.Logger log4j1Logger;
+    private static LogEvent createLogEvent() {
+        final Marker marker = null;
+        final String fqcn = "com.mycom.myproject.mypackage.MyClass";
+        final org.apache.logging.log4j.Level level = org.apache.logging.log4j.Level.DEBUG;
+        final Message message = new SimpleMessage(MESSAGE);
+        final Throwable t = null;
+        final Map<String, String> mdc = null;
+        final ThreadContext.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();
+    }
+
+    Appender appender;
     int j;
 
     @Setup
     public void setUp() {
-        System.setProperty("log4j.configurationFile", "log4j2-gelf-perf.xml");
+        System.setProperty("log4j2.enable.direct.encoders", "true");
 
-        File log4j2File = new File("target/testlog4j2.json");
-        log4j2File.delete();
+        appender = new DemoAppender(new GelfLayout(
+                "host",
+                ADDITIONAL_FIELDS,
+                GelfLayout.CompressionType.OFF,
+                0));
 
-        log4j2Logger = LogManager.getLogger(GelfLayoutBenchmark.class);
         j = 0;
     }
 
     @TearDown
     public void tearDown() {
-        System.clearProperty("log4j.configurationFile");
-
-        File log4j2File = new File("target/testlog4j2.json");
-        log4j2File.delete();
+        System.clearProperty("log4j2.enable.direct.encoders");
     }
 
     @BenchmarkMode(Mode.Throughput)
@@ -69,7 +104,7 @@ public class GelfLayoutBenchmark {
     @OutputTimeUnit(TimeUnit.MILLISECONDS)
     @Benchmark
     public void log4j2Gelf() {
-        log4j2Logger.debug(MESSAGE);
+        appender.append(EVENT);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aaaaaad3/log4j-perf/src/main/java/org/apache/logging/log4j/perf/util/DemoAppender.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/util/DemoAppender.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/util/DemoAppender.java
new file mode 100644
index 0000000..35171c2
--- /dev/null
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/util/DemoAppender.java
@@ -0,0 +1,71 @@
+/*
+ * 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.util;
+
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.layout.ByteBufferDestination;
+import org.apache.logging.log4j.core.util.Constants;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Demo Appender that does not do any I/O.
+ */
+public class DemoAppender extends AbstractAppender implements ByteBufferDestination {
+    private final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[4096]);
+
+    public long checksum;
+
+    public DemoAppender(Layout<?> layout) {
+        super("demo", null, layout);
+    }
+
+    @Override
+    public void append(LogEvent event) {
+        if (Constants.ENABLE_DIRECT_ENCODERS) {
+            getLayout().encode(event, this);
+            drain(byteBuffer);
+        } else {
+            byte[] binary = getLayout().toByteArray(event);
+            consume(binary, 0, binary.length);
+        }
+    }
+
+    @Override
+    public ByteBuffer getByteBuffer() {
+        return byteBuffer;
+    }
+
+    @Override
+    public ByteBuffer drain(ByteBuffer buf) {
+        buf.flip();
+        consume(buf.array(), buf.position(), buf.limit());
+        buf.clear();
+        return buf;
+    }
+
+    private void consume(byte[] data, int offset, int length) {
+        // need to do something with the result or the JVM may optimize everything away
+        long sum = 0;
+        for (int i = offset; i < length; i++) {
+            sum += data[i];
+        }
+        checksum += sum;
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aaaaaad3/log4j-perf/src/main/resources/log4j2-gelf-perf.xml
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/resources/log4j2-gelf-perf.xml b/log4j-perf/src/main/resources/log4j2-gelf-perf.xml
deleted file mode 100644
index 94c74e5..0000000
--- a/log4j-perf/src/main/resources/log4j2-gelf-perf.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?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="GelfPerfTest" status="error">
-    <Appenders>
-        <File name="TestLogfile" fileName="target/testlog4j2.json" immediateFlush="false">
-            <GelfLayout compressionType="OFF"/>
-        </File>
-    </Appenders>
-    <Loggers>
-        <Root level="debug">
-            <AppenderRef ref="TestLogfile"/>
-        </Root>
-    </Loggers>
-</Configuration>
\ No newline at end of file