You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/03/14 21:04:44 UTC

[logging-log4j2] 04/17: LOG4J2-3393 Improve "blackhole" behavior in ByteBuffer consumption.

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch LOG4J2-3393
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit b45c682ea33bec2200da3ce39f9dbbf0ea95c21e
Author: Volkan Yazici <vo...@yazi.ci>
AuthorDate: Mon Feb 14 22:57:30 2022 +0100

    LOG4J2-3393 Improve "blackhole" behavior in ByteBuffer consumption.
---
 .../layout/template/json/BlackHoleByteBufferDestination.java   | 10 ++++++++--
 .../layout/template/json/JsonTemplateLayoutBenchmark.java      |  9 +++++----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/BlackHoleByteBufferDestination.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/BlackHoleByteBufferDestination.java
index 3c0f4fb..ca6041b 100644
--- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/BlackHoleByteBufferDestination.java
+++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/BlackHoleByteBufferDestination.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.layout.template.json;
 
 import org.apache.logging.log4j.core.layout.ByteBufferDestination;
+import org.apache.logging.log4j.core.layout.ByteBufferDestinationHelper;
 
 import java.nio.ByteBuffer;
 
@@ -35,16 +36,21 @@ class BlackHoleByteBufferDestination implements ByteBufferDestination {
 
     @Override
     public ByteBuffer drain(final ByteBuffer byteBuffer) {
+        byteBuffer.flip();
+        this.byteBuffer.clear();
+        this.byteBuffer.put(byteBuffer);
         byteBuffer.clear();
         return byteBuffer;
     }
 
     @Override
     public void writeBytes(final ByteBuffer byteBuffer) {
-        byteBuffer.clear();
+        ByteBufferDestinationHelper.writeToUnsynchronized(byteBuffer, this);
     }
 
     @Override
-    public void writeBytes(final byte[] buffer, final int offset, final int length) {}
+    public void writeBytes(final byte[] buffer, final int offset, final int length) {
+        ByteBufferDestinationHelper.writeToUnsynchronized(buffer, offset, length, this);
+    }
 
 }
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutBenchmark.java
index 2f865fa..0b3ca8b 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutBenchmark.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutBenchmark.java
@@ -172,15 +172,16 @@ public class JsonTemplateLayoutBenchmark {
             final Layout<String> layout,
             final List<LogEvent> logEvents,
             final ByteBufferDestination destination) {
+        final ByteBuffer byteBuffer = destination.getByteBuffer();
+        int checksum = 0;
         // noinspection ForLoopReplaceableByForEach (avoid iterator instantiation)
         for (int logEventIndex = 0; logEventIndex < logEvents.size(); logEventIndex++) {
             LogEvent logEvent = logEvents.get(logEventIndex);
             layout.encode(logEvent, destination);
+            checksum += byteBuffer.position();
+            byteBuffer.clear();
         }
-        final ByteBuffer byteBuffer = destination.getByteBuffer();
-        final int position = byteBuffer.position();
-        byteBuffer.clear();
-        return position;
+        return checksum;
     }
 
     public static void main(String[] args) throws IOException {