Web API performance optimization (I) compression and api Performance Optimization

Source: Internet
Author: User

Web API performance optimization (I) compression and api Performance Optimization

Simple Application Scenario: retrieve log JSON information by page.

Simple implementation, simple record

When not compressed

Use PostMan to request http: // localhost: 34390/api/gpm/syslog/page? Pageindex = 1 & pagesize = 10

Some technologies are used: AOP, IOC, Request/Response mode (I personally think it is very suitable for WebAPI), which is not described here. ApiResullt is a Class I encapsulate (automatic serialization). As for why I create one myself, I just feel comfortable! This is the most important thing.

Response result: Size: 2.04KB, Content-Length: 1607

Use Compression

Use DotNetZip for compression. Of course, use the AOP method.Default compression level

1 // <summary> 2 // compress the returned information 3 /// </summary> 4 [AttributeUsage (AttributeTargets. class | AttributeTargets. method, Inherited = true, AllowMultiple = true)] 5 public class CompressionAttribute: ActionFilterAttribute 6 {7 public override void OnActionExecuted (HttpActionExecutedContext actionExecutedContext) 8 {9 var content = actionExecutedContext. response. content; 10 var bytes = content = null? Null: content. ReadAsByteArrayAsync (). Result; 11 var compressContent = bytes = null? New byte [0]: CompressionHelper. deflateByte (bytes); 12 actionExecutedContext. response. content = new ByteArrayContent (compressContent); 13 actionExecutedContext. response. content. headers. remove ("Content-Type"); 14 actionExecutedContext. response. content. headers. add ("Content-encoding", 15 string. equals (actionExecutedContext. request. headers. acceptEncoding. first (). value, "deflate") 16? "Deflate" 17: "gzip"); 18 actionExecutedContext. response. content. headers. add ("Content-Type", "application/json; charset = UTF-8"); 19 base. onActionExecuted (actionExecutedContext ); 20} 21} 22 // <summary> 23 // compression help class 24 // </summary> 25 internal static class CompressionHelper26 {27 public static byte [] DeflateByte (byte [] str) 28 {29 if (str = null) 30 {31 return null; 32} 33 using (var output = new MemoryStream () 34 {35 using (var compressor = new Ionic. zlib. GZipStream (output, Ionic. zlib. compressionMode. compress, Ionic. zlib. compressionLevel. default) 36 {37 compressor. write (str, 0, str. length); 38} 39 return output. toArray (); 40} 41} 42}

Request Response Results: Size: 833B, Content-Length: 329. The improvement effect is very obvious. Careful friends will find that the response time is getting longer, of course, this is caused by compression time, but it has little impact.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.