Compression decompression of WEBAPI performance optimization
Sometimes in order to improve WEBAPI performance and reduce response time, we use compression and decompression, while most client browsers now provide built-in decompression support. The larger the resources requested by the WEBAPI, the more noticeable the performance gains are with compression, and when the requested resource is very small, compression and decompression are not required because compression and decompression also take a certain amount of time.
See the Foreigner wrote an ASP. NET Web API GZip compression actionfilter with 8 lines of code
To tell the truth was attracted by this title, 8 lines of code to implement gzip compression filter, I followed his practice, found that incredibly Chinese garbled.
According to his way of realization:
1. Download Dotnetziplib Library
2. Add Ionic.Zlib.dll DLL Reference after decompression
3, the new deflatecompression features and gzipcompression features, respectively, representing deflate compression and gzip compression, the two compression methods are similar to the implementation code
A different place is
ACTCONTEXT.RESPONSE.CONTENT.HEADERS.ADD ("content-encoding", "gzip");
ACTCONTEXT.RESPONSE.CONTENT.HEADERS.ADD ("content-encoding", "deflate");
And
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:a Ctionfilterattribute {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 DeflateCompressionAttr Ibute: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); } }
4. Add a compression helper class 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 (); } } }}
5, controller call, here I write the test code:
public class Testcontroller:apicontroller { StringBuilder sb = new StringBuilder (); [Gzipcompression] public string Get (int id) {for (int i = 0; i < 1000;i++) { sb. Append ("Here is China's territory" + i); } Return SB. ToString () + DateTime.Now.ToLocalTime () + "," + ID; } }
The file size is 26.4kb and the request time is 1.27s, which is not using compression, comment//[gzipcompression] tag First
Using the [gzipcompression] tag, the file size is 2.4kb after adding compression, the response time is 1.21,respouse body is significantly smaller, but the response time is not obvious, because in the local environment download is too fast, and compression decompression to consume a certain amount of time, The interface loading time is mainly consumed on the onload. There is a problem: Chinese display garbled.
Using. Net-band compression, the corresponding class library--gzipstream and Deflatestream are provided in the System.IO.Compression. The controller call code does not change, create a new CompressContentAttribute.cs class, the code is as follows:
View Code
Run view results, compression ability is slightly worse than Dotnetziplib, but no longer garbled.
Change the tag in the controller code to [Deflatecompression], and then look at the effect using deflate compression:
Deflate compression, content-length value is 2538, and gzip compression content-length value is 2556, visible deflate compression effect is better.
Here, WEBAPI compression I was implemented through the action filter, of course, you can also write in the WEBAPI in the global configuration, considering that some API interface does not need to use compression, so the action filter is implemented by the way.
Dudu This article httpclient with aps.net Web API: Compression and decompression of request content on client compression, decompression on the server.
Webapi Performance Optimization