WebAPI性能優化之壓縮解壓

有時候爲了提高WebAPI的性能,減小響應時間,咱們會使用壓縮和解壓,而如今大多數客戶端瀏覽器都提供了內置的解壓支持。在WebAPI請求的資源越大時,使用壓縮對性能提高的效果越明顯,而當請求的資源很小時則不須要使用壓縮和解壓,由於壓縮和解壓一樣也是須要耗費必定的時間的。html

看見老外寫了一篇ASP.NET Web API GZip compression ActionFilter with 8 lines of code web

說實話被這標題吸引了,8行代碼實現GZip壓縮過濾器,我就照着他的去實踐了一番,發現竟然中文出現亂碼。算法

按照他的實現方式:json

一、下載DotNetZipLibapi

二、解壓後添加Ionic.Zlib.dll的dll引用瀏覽器

三、新建DeflateCompression特性和GZipCompression特性,分別表明Deflate壓縮和GZip壓縮,這兩種壓縮方式的實現代碼很類似app

不一樣的地方就是ide

actContext.Response.Content.Headers.Add("Content-encoding", "gzip");post

actContext.Response.Content.Headers.Add("Content-encoding", "deflate");性能

  var compressor = new DeflateStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed)
 var compressor = new GZipStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed)
using System.Net.Http;
using System.Web.Http.Filters;

namespace WebAPI.Filter
{
    public class GZipCompressionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actContext)
        {
            var content = actContext.Response.Content;
            var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result;
            var zlibbedContent = bytes == null ? new byte[0] :
            CompressionHelper.GZipByte(bytes);
            actContext.Response.Content = new ByteArrayContent(zlibbedContent);
            actContext.Response.Content.Headers.Remove("Content-Type");
            actContext.Response.Content.Headers.Add("Content-encoding", "gzip");
            actContext.Response.Content.Headers.Add("Content-Type", "application/json");
            base.OnActionExecuted(actContext);
        }
    }
}
using System.Net.Http;
using System.Web.Http.Filters;

namespace WebAPI.Filter
{
    public class DeflateCompressionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actContext)
        {
            var content = actContext.Response.Content;
            var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result;
            var zlibbedContent = bytes == null ? new byte[0] :
            CompressionHelper.DeflateByte(bytes);
            actContext.Response.Content = new ByteArrayContent(zlibbedContent);
            actContext.Response.Content.Headers.Remove("Content-Type");
            actContext.Response.Content.Headers.Add("Content-encoding", "deflate");
            actContext.Response.Content.Headers.Add("Content-Type", "application/json");
            base.OnActionExecuted(actContext);
        }
    }

四、添加一個壓縮幫助類CompressionHelper

using System.IO;
using Ionic.Zlib;

namespace WebAPI.Filter
{
    public class CompressionHelper
    {
        public static byte[] DeflateByte(byte[] str)
        {
            if (str == null)
            {
                return null;
            }

            using (var output = new MemoryStream())
            {
                using (
                    var compressor = new DeflateStream(
                    output, CompressionMode.Compress,
                    CompressionLevel.BestSpeed))
                {
                    compressor.Write(str, 0, str.Length);
                }

                return output.ToArray();
            }
        }
        public static byte[] GZipByte(byte[] str)
        {
            if (str == null)
            {
                return null;
            }
            using (var output = new MemoryStream())
            {
                using (
                    var compressor = new GZipStream(
                    output, CompressionMode.Compress,
                    CompressionLevel.BestSpeed))
                {
                    compressor.Write(str, 0, str.Length);
                }

                return output.ToArray();
            }
        }
    }
}

五、控制器調用,這裏我寫的測試代碼:

    public class TestController : ApiController
    {
        StringBuilder sb = new StringBuilder();
        
        [GZipCompression]
        public string Get(int id)
        {
            for (int i = 0; i < 1000;i++ )
            {
                sb.Append("這裏是中國的領土" + i);
            }
            return sb.ToString() + DateTime.Now.ToLocalTime() + "," + id;
        }
    }

先看下不使用壓縮,註釋//[GZipCompression] 標記,文件大小是26.4kb,請求時間是1.27s

使用[GZipCompression]標記,添加壓縮後,文件大小是2.4kb,響應時間是1.21,Respouse Body明顯小了不少,可是響應時間少得並不明顯,由於在本地環境下載太快了,而壓縮解壓卻要消耗必定的時間,界面加載的時間主要消耗在onload上了。有個問題:中文顯示亂碼了。

 使用.net自帶的壓縮,在System.IO.Compression中提供了對應的類庫——GZipStream與DeflateStream。控制器調用代碼不變,新建一個CompressContentAttribute.cs類,代碼以下:

using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace WebAPI.Filter
{
    // <summary>
    /// 自動識別客戶端是否支持壓縮,若是支持則返回壓縮後的數據
    /// Attribute that can be added to controller methods to force content
    /// to be GZip encoded if the client supports it
    /// </summary>
    public class CompressContentAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Override to compress the content that is generated by
        /// an action method.
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            GZipEncodePage();
        }

        /// <summary>
        /// Determines if GZip is supported
        /// </summary>
        /// <returns></returns>
        public static bool IsGZipSupported()
        {
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(AcceptEncoding) &&
                    (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
                return true;
            return false;
        }

        /// <summary>
        /// Sets up the current page or handler to use GZip through a Response.Filter
        /// IMPORTANT:
        /// You have to call this method before any output is generated!
        /// </summary>
        public static void GZipEncodePage()
        {
            HttpResponse Response = HttpContext.Current.Response;

            if (IsGZipSupported())
            {
                string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

                if (AcceptEncoding.Contains("deflate"))
                {
                    Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
                                               System.IO.Compression.CompressionMode.Compress);
                    #region II6不支持此方法,(實際上此值默認爲null 也不須要移除)
                    //Response.Headers.Remove("Content-Encoding");
                    #endregion
                    Response.AppendHeader("Content-Encoding", "deflate");
                }
                else
                {
                    Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
                                                 System.IO.Compression.CompressionMode.Compress);
                    #region II6不支持此方法,(實際上此值默認爲null 也不須要移除)
                    //Response.Headers.Remove("Content-Encoding");
                    #endregion
                    Response.AppendHeader("Content-Encoding", "gzip");
                }
            }

            // Allow proxy servers to cache encoded and unencoded versions separately
            Response.AppendHeader("Vary", "Content-Encoding");
        }
    }

    /// <summary>
    /// 強制Defalte壓縮
    /// Content-encoding:gzip,Content-Type:application/json
    /// DEFLATE是一個無專利的壓縮算法,它能夠實現無損數據壓縮,有衆多開源的實現算法。
    /// </summary>
    public class DeflateCompressionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            HttpResponse Response = HttpContext.Current.Response;
            Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
                                              System.IO.Compression.CompressionMode.Compress);
            #region II6不支持此方法,(實際上此值默認爲null 也不須要移除)
            //Response.Headers.Remove("Content-Encoding");
            #endregion
            Response.AppendHeader("Content-Encoding", "deflate");
        }
    }

    /// <summary>
    /// 強制GZip壓縮,application/json
    /// Content-encoding:gzip,Content-Type:application/json
    /// GZIP是使用DEFLATE進行壓縮數據的另外一個壓縮庫
    /// </summary>
    public class GZipCompressionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            HttpResponse Response = HttpContext.Current.Response;
            Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
                                              System.IO.Compression.CompressionMode.Compress);
            #region II6不支持此方法,(實際上此值默認爲null 也不須要移除)
            //Response.Headers.Remove("Content-Encoding");
            #endregion
            Response.AppendHeader("Content-Encoding", "gzip");
        }
    }
}
View Code

運行查看結果,壓縮能力比DotNetZipLib略差,可是再也不出現亂碼了。

把控制器代碼中的標記改成   [DeflateCompression],使用Deflate壓縮再來看下效果:

 

Deflate壓縮後,Content-Length值爲2538,而GZip壓縮Content-Length值爲2556,可見Deflate壓縮效果更好。

這裏,WebAPI的壓縮我都是經過Action過濾器的方式來實現,固然你也能夠寫在WebAPI中的全局配置中,考慮到有些API接口並不須要使用到壓縮,因此就經過Action過濾器的方式來實現了。

dudu的這篇文章HttpClient與APS.NET Web API:請求內容的壓縮與解壓在客戶端壓縮、在服務端解壓。

相關文章
相關標籤/搜索