[grizzly~git:b69de02c] [2.3.x] Changes for https://java.net/jira/browse/GRIZZLY-1461 (Add gzip t

  • From: rlubke@...
  • To: commits@...
  • Subject: [grizzly~git:b69de02c] [2.3.x] Changes for https://java.net/jira/browse/GRIZZLY-1461 (Add gzip t
  • Date: Thu, 23 May 2013 05:24:25 +0000

Project:    grizzly
Repository: git
Revision:   b69de02c988557fe4c19285dc0f2390856cf2fa8
Author:     rlubke
Date:       2013-05-23 05:23:52 UTC
Link:       

Log Message:
------------
[2.3.x] Changes for https://java.net/jira/browse/GRIZZLY-1461 ;(Add gzip 
tracking)
 - Updated existing probes to include an after content encoding event.
 - update JMX probe to track the before/after results and include a percent 
difference (includes both gzip and lzma).



Revisions:
----------
b69de02c988557fe4c19285dc0f2390856cf2fa8


Modified Paths:
---------------
modules/http/src/main/java/org/glassfish/grizzly/http/GZipContentEncoding.java
modules/http/src/main/java/org/glassfish/grizzly/http/HttpCodecFilter.java
modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbe.java
modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbeNotifier.java
modules/http/src/main/java/org/glassfish/grizzly/http/LZMAContentEncoding.java
modules/monitoring/http/src/main/java/org/glassfish/grizzly/http/jmx/HttpCodecFilter.java


Diffs:
------
--- 
a/modules/http/src/main/java/org/glassfish/grizzly/http/GZipContentEncoding.java
+++ 
b/modules/http/src/main/java/org/glassfish/grizzly/http/GZipContentEncoding.java
@@ -59,7 +59,7 @@ public class GZipContentEncoding implements ContentEncoding 
{
 
     private static final String[] ALIASES = {"gzip", "deflate"};
 
-    private static final String name = "gzip";
+    public static final String NAME = "gzip";
     
     private final GZipDecoder decoder;
     private final GZipEncoder encoder;
@@ -114,7 +114,7 @@ public class GZipContentEncoding implements 
ContentEncoding {
 
     @Override
     public String getName() {
-        return name;
+        return NAME;
     }
 
     @Override
@@ -237,14 +237,14 @@ public class GZipContentEncoding implements 
ContentEncoding {
             return false;
         }
         final GZipContentEncoding other = (GZipContentEncoding) obj;
-        return this.name.equals(other.name);
+        return this.NAME.equals(other.NAME);
 
     }
 
     @Override
     public int hashCode() {
         int hash = 3;
-        hash = 53 * hash + (this.name.hashCode());
+        hash = 53 * hash + (this.NAME.hashCode());
         return hash;
     }
 }--- 
a/modules/http/src/main/java/org/glassfish/grizzly/http/HttpCodecFilter.java
+++ 
b/modules/http/src/main/java/org/glassfish/grizzly/http/HttpCodecFilter.java
@@ -1252,6 +1252,12 @@ public abstract class HttpCodecFilter extends 
HttpBaseFilter
             }
 
             final HttpContent decodedContent = result.getHttpContent();
+
+            HttpProbeNotifier.notifyContentEncodingParseResult(this,
+                                                               connection,
+                                                               httpHeader,
+                                                               
decodedContent.getContent(),
+                                                               encoding);
             
             result.recycle();
 
@@ -1573,11 +1579,18 @@ public abstract class HttpCodecFilter extends 
HttpBaseFilter
             
             final HttpContent encodedContent = encoding.encode(connection, 
httpContent);
 
+
             if (encodedContent == null) {
                 httpContent.recycle();
                 return null;
             }
 
+            HttpProbeNotifier.notifyContentEncodingSerializeResult(this,
+                                                                   
connection,
+                                                                   
httpHeader,
+                                                                   
encodedContent.getContent(),
+                                                                   encoding);
+
             httpContent = encodedContent;
         }
 --- a/modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbe.java
+++ b/modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbe.java
@@ -126,6 +126,24 @@ public interface HttpProbe {
             HttpHeader header, Buffer buffer, ContentEncoding 
contentEncoding);
 
     /**
+     * This method will be called after the {@link ContentEncoding} has been
+     * applied.
+     *
+     * @param connection {@link Connection}, the event belongs to.
+     * @param header HTTP {@link HttpHeader}, the event belongs to.
+     * @param result the result of the decode operation.
+     * @param contentEncoding the {@link ContentEncoding} that was applied.
+     *
+     * @since 2.3.3
+     */
+    public void onContentEncodingParseResultEvent(Connection connection,
+                                                  HttpHeader header,
+                                                  Buffer result,
+                                                  ContentEncoding 
contentEncoding);
+
+    /**
+
+    /**
      * Method will be called, when {@link ContentEncoding} will be applied
      * during the serialization/encoding of the certain HTTP message content 
chunk.
      *
@@ -138,6 +156,22 @@ public interface HttpProbe {
             HttpHeader header, Buffer buffer, ContentEncoding 
contentEncoding);
 
     /**
+     * Method will be called, when {@link ContentEncoding} will be applied
+     * during the serialization/encoding of the certain HTTP message content 
chunk.
+     *
+     * @param connection      {@link Connection}, the event belongs to.
+     * @param header          HTTP {@link HttpHeader}, the event belongs to.
+     * @param result          The result of the encoding processes.
+     * @param contentEncoding {@link ContentEncoding} to be applied.
+     *
+     * @since 2.3.3
+     */
+    public void onContentEncodingSerializeResultEvent(Connection connection,
+                                                      HttpHeader header,
+                                                      Buffer result,
+                                                      ContentEncoding 
contentEncoding);
+
+    /**
      * Method will be called, when {@link TransferEncoding} will be applied
      * during the parsing/decoding of the certain HTTP message content chunk.
      *
@@ -229,6 +263,12 @@ public interface HttpProbe {
         public void onContentEncodingParseEvent(Connection connection, 
HttpHeader header, Buffer buffer, ContentEncoding contentEncoding) {}
 
         /**
+          * {@inheritDoc}
+          */
+        @Override
+        public void onContentEncodingParseResultEvent(Connection connection, 
HttpHeader header, Buffer result, ContentEncoding contentEncoding) {}
+
+        /**
          * {@inheritDoc}
          */
         @Override
@@ -238,6 +278,12 @@ public interface HttpProbe {
          * {@inheritDoc}
          */
         @Override
+        public void onContentEncodingSerializeResultEvent(Connection 
connection, HttpHeader header, Buffer result, ContentEncoding 
contentEncoding) {}
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
         public void onTransferEncodingParseEvent(Connection connection, 
HttpHeader header, Buffer buffer, TransferEncoding transferEncoding) {}
 
         /**--- 
a/modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbeNotifier.java
+++ 
b/modules/http/src/main/java/org/glassfish/grizzly/http/HttpProbeNotifier.java
@@ -188,6 +188,33 @@ final class HttpProbeNotifier {
             }
         }
     }
+
+    /**
+     * Notify registered {@link HttpProbe}s about the result of the "content 
encoding decode" event.
+     *
+     * @param httpFilter      the <tt>HttpCodecFilter</tt> event occurred on.
+     * @param connection      the <tt>Connection</tt> event occurred on.
+     * @param header          HTTP {@link HttpHeader}, the event belongs to.
+     * @param result          the result of the decoding process.
+     * @param contentEncoding the {@link ContentEncoding} which was applied.
+     * @since 2.3.3
+     */
+    static void notifyContentEncodingParseResult(final HttpCodecFilter 
httpFilter,
+                                                 final Connection connection,
+                                                 final HttpHeader header,
+                                                 final Buffer result,
+                                                 final ContentEncoding 
contentEncoding) {
+        final HttpProbe[] probes =
+                httpFilter.monitoringConfig.getProbesUnsafe();
+        if (probes != null) {
+            for (HttpProbe probe : probes) {
+                probe.onContentEncodingSerializeResultEvent(connection,
+                                                            header,
+                                                            result,
+                                                            contentEncoding);
+            }
+        }
+    }
     
     /**
      * Notify registered {@link HttpProbe}s about the "content encoding 
serialize" event.
@@ -212,6 +239,34 @@ final class HttpProbeNotifier {
     }
 
     /**
+     * Notify registered {@link HttpProbe}s about the result of the "content 
encoding serialize" event.
+     *
+     * @param httpFilter the <tt>HttpCodecFilter</tt> event occurred on.
+     * @param connection the <tt>Connection</tt> event occurred on.
+     * @param header HTTP {@link HttpHeader}, the event belongs to.
+     * @param result the result of the encoding process.
+     * @param contentEncoding the {@link ContentEncoding} which was applied.
+     *
+     * @since 2.3.3
+     */
+    static void notifyContentEncodingSerializeResult(final HttpCodecFilter 
httpFilter,
+                                                     final Connection 
connection,
+                                                     final HttpHeader header,
+                                                     final Buffer result,
+                                                     final ContentEncoding 
contentEncoding) {
+        final HttpProbe[] probes =
+                httpFilter.monitoringConfig.getProbesUnsafe();
+        if (probes != null) {
+            for (HttpProbe probe : probes) {
+                probe.onContentEncodingSerializeResultEvent(connection,
+                                                            header,
+                                                            result,
+                                                            contentEncoding);
+            }
+        }
+    }
+
+    /**
      * Notify registered {@link HttpProbe}s about the "transfer encoding 
parse" event.
      *
      * @param httpFilter the <tt>HttpCodecFilter</tt> event occurred on.--- 
a/modules/http/src/main/java/org/glassfish/grizzly/http/LZMAContentEncoding.java
+++ 
b/modules/http/src/main/java/org/glassfish/grizzly/http/LZMAContentEncoding.java
@@ -49,7 +49,7 @@ public class LZMAContentEncoding implements ContentEncoding 
{
 
     private static final String[] ALIASES = { "lzma" };
 
-    private static final String NAME = "lzma";
+    public static final String NAME = "lzma";
 
     private final LZMADecoder decoder;
     private final LZMAEncoder encoder;--- 
a/modules/monitoring/http/src/main/java/org/glassfish/grizzly/http/jmx/HttpCodecFilter.java
+++ 
b/modules/monitoring/http/src/main/java/org/glassfish/grizzly/http/jmx/HttpCodecFilter.java
@@ -43,9 +43,11 @@ package org.glassfish.grizzly.http.jmx;
 import org.glassfish.grizzly.Buffer;
 import org.glassfish.grizzly.Connection;
 import org.glassfish.grizzly.http.ContentEncoding;
+import org.glassfish.grizzly.http.GZipContentEncoding;
 import org.glassfish.grizzly.http.HttpContent;
 import org.glassfish.grizzly.http.HttpHeader;
 import org.glassfish.grizzly.http.HttpProbe;
+import org.glassfish.grizzly.http.LZMAContentEncoding;
 import org.glassfish.grizzly.http.TransferEncoding;
 import org.glassfish.grizzly.monitoring.jmx.JmxObject;
 import org.glassfish.gmbal.Description;
@@ -54,6 +56,9 @@ import org.glassfish.gmbal.ManagedAttribute;
 import org.glassfish.gmbal.ManagedObject;
 
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
 import org.glassfish.grizzly.http.HttpPacket;
 import org.glassfish.grizzly.jmxbase.GrizzlyJmxManager;
 
@@ -71,6 +76,13 @@ public class HttpCodecFilter extends JmxObject {
     private final AtomicLong httpContentReceived = new AtomicLong();
     private final AtomicLong httpContentWritten = new AtomicLong();
     private final AtomicLong httpCodecErrorCount = new AtomicLong();
+    private long contentCompressionTotalGzip;
+    private long contentBeforeCompressionTotalGzip;
+    private long contentCompressionTotalLzma;
+    private long contentBeforeCompressionTotalLzma;
+    private final ReentrantReadWriteLock gzipLock = new 
ReentrantReadWriteLock();
+    private final ReentrantReadWriteLock lzmaLock = new 
ReentrantReadWriteLock();
+
 
     private final HttpProbe probe = new JmxHttpProbe();
 
@@ -146,6 +158,90 @@ public class HttpCodecFilter extends JmxObject {
         return httpCodecErrorCount.get();
     }
 
+    /**
+     * @return total number of bytes sent to gzip to be compressed.
+     */
+    @ManagedAttribute(id="http-codec-before-gzip-compression-total")
+    @Description("The total number of bytes before gzip compression has been 
applied.")
+    public long getTotalBytesBeforeGzipEncoding() {
+        return contentBeforeCompressionTotalGzip;
+    }
+
+    /**
+     * @return total number of bytes after gzip compression.
+     */
+    @ManagedAttribute(id="http-codec-after-gzip-compression-total")
+    @Description("The total number of bytes after gzip compression has been 
applied.")
+    public long getTotalBytesAfterGzipEncoding() {
+        return contentCompressionTotalGzip;
+    }
+
+    /**
+     * @return the gzip compression ratio.
+     */
+    @ManagedAttribute(id="http-codec-gzip-avg-compression-percent")
+    @Description("The average gzip compression result.")
+    public String getGzipCompressionRatio() {
+        final Lock lock = gzipLock.readLock();
+        lock.lock();
+        long l1;
+        long l2;
+        try {
+            l1 = contentBeforeCompressionTotalGzip;
+            l2 = contentCompressionTotalGzip;
+        } finally {
+            lock.unlock();
+        }
+        return calculateAvgCompressionPercent(l1, l2);
+    }
+
+    /**
+     * @return total number of bytes sent to lzma be compressed.
+     */
+    @ManagedAttribute(id = "http-codec-before-lzma-compression-total")
+    @Description( "The total number of bytes before lzma compression has 
been applied.")
+    public long getTotalBytesBeforeLzmaEncoding() {
+        return contentBeforeCompressionTotalLzma;
+    }
+
+    /**
+     * @return total number of bytes after lzma compression.
+     */
+    @ManagedAttribute(id = "http-codec-after-lzma-compression-total")
+    @Description( "The total number of bytes after lzma compression has been 
applied.")
+    public long getTotalBytesAfterLzmaEncoding() {
+        return contentCompressionTotalLzma;
+    }
+
+    /**
+     * @return the lzma compression ratio.
+     */
+    @ManagedAttribute(id = "http-codec-lzma-avg-compression-percent")
+    @Description( "The average lzma compression result.")
+    public String getLzmaAvgCompressionPercent() {
+        final Lock lock = lzmaLock.readLock();
+        lock.lock();
+        long l1;
+        long l2;
+        try {
+            l1 = contentBeforeCompressionTotalLzma;
+            l2 = contentCompressionTotalLzma;
+        } finally {
+            lock.unlock();
+        }
+        return calculateAvgCompressionPercent(l1, l2);
+    }
+
+
+    // --------------------------------------------------------- Private 
Methods
+
+
+    private String calculateAvgCompressionPercent(double original, double 
result) {
+        double r = 100 - ((result / original) * 100);
+
+        return String.format("%.2f%%", r);
+    }
+
 
     // ---------------------------------------------------------- Nested 
Classes
 
@@ -182,15 +278,60 @@ public class HttpCodecFilter extends JmxObject {
         }
 
         @Override
+        public void onContentEncodingParseResultEvent(Connection connection, 
HttpHeader header, Buffer result, ContentEncoding contentEncoding) {
+
+        }
+
+        @Override
         public void onContentChunkSerializeEvent(Connection connection, 
HttpContent content) {
         }
 
         @Override
+        public void onContentEncodingSerializeResultEvent(Connection 
connection, HttpHeader header, Buffer result, ContentEncoding 
contentEncoding) {
+            final String name = contentEncoding.getName();
+            if (GZipContentEncoding.NAME.equals(name)) {
+                final Lock lock = gzipLock.writeLock();
+                lock.lock();
+                try {
+                    contentCompressionTotalGzip += result.remaining();
+                } finally {
+                    lock.unlock();
+                }
+            } else if (LZMAContentEncoding.NAME.equals(name)) {
+                final Lock lock = lzmaLock.writeLock();
+                lock.lock();
+                try {
+                    contentCompressionTotalLzma += result.remaining();
+                } finally {
+                    lock.unlock();
+                }
+            }
+        }
+
+        @Override
         public void onContentEncodingParseEvent(Connection connection, 
HttpHeader header, Buffer buffer, ContentEncoding contentEncoding) {
         }
 
         @Override
-        public void onContentEncodingSerializeEvent(Connection connection, 
HttpHeader header, Buffer buffer, ContentEncoding contentEncoding) {
+        public void onContentEncodingSerializeEvent(Connection connection, 
HttpHeader header, Buffer result, ContentEncoding contentEncoding) {
+            final String name = contentEncoding.getName();
+            if (GZipContentEncoding.NAME.equals(name)) {
+                final Lock lock = gzipLock.writeLock();
+                lock.lock();
+                try {
+                    contentBeforeCompressionTotalGzip += result.remaining();
+                } finally {
+                    lock.unlock();
+                }
+            } else if (LZMAContentEncoding.NAME.equals(name)) {
+                final Lock lock = lzmaLock.writeLock();
+                lock.lock();
+                try {
+                    contentBeforeCompressionTotalLzma += result.remaining();
+                } finally {
+                    lock.unlock();
+                }
+            }
         }
 
         @Override





[grizzly~git:b69de02c] [2.3.x] Changes for https://java.net/jira/browse/GRIZZLY-1461 (Add gzip t

rlubke 05/23/2013
Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20131025.e7cbc9d)
 
 
Close
loading
Please Confirm
Close