Web api scalability and performance


Published on

ASP.NET Web API : Performance and Scalability Key concepts.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Etag demo
  • Cache Control
  • Paging and Throttling
  • Protocol Technique
  • Push Content Stream
  • This test is for (r145; 1,000,000 iterations)
  • Http Batching
  • Push Content Sample
  • Start with blocking sampleStep 1 shows use of task.createnew – explain the concept of blocking threadpool threadStep 2 shows use of async keyword
  • //convert this slide to animation to explain the concept of threadsstatic void SyncMain(string[] args) { ...while (true) { varclient = socket.AcceptTcpClient(); ...vart1 = new Thread(new ThreadStart(() => ...ServiceClient(client);In the main loop we accept new clients (AcceptTcpClient), and for each one we start a new thread (ThreadStart) that calls ServiceClient(), which is this little function that is a lot like the pseudocode I mentioned earlier:static void ServiceClient(TcpClientclient) { using (varstream = client.GetStream()) { stream.Write(quote, 0, 1); // write header while (true) { WriteStockQuote(stream); } } }
  • http://www.iis.net/learn/get-started/introduction-to-iis/introduction-to-iis-architecture
  • DefaultConnectionLimitHKLM\SOFTWARE\Microsoft\ASP.NET\2.0.50727.0\MaxConcurrentRequestsPerCPU)ServicePointManager.DefaultConnectionLimit.Few ways you can do this.Best way is to do in global.asax.// See http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx /* 5.If your ASP.NET application is using web services (WFC or ASMX) or System.Net to communicate * with a backend over HTTP you may need to increase connectionManagement/maxconnection. * For ASP.NET applications, this is limited to 12 * #CPUs by the autoConfig feature. * This means that on a quad-proc, you can have at most 12 * 4 = 48 concurrent * connections to an IP end point. Because this is tied to autoConfig, the easiest way * to increase maxconnection in an ASP.NET application is to * set System.Net.ServicePointManager.DefaultConnectionLimitprogramatically, * from Application_Start, for example. Set the value to the number of concurrent * System.Net connections you expect your application to use. I've set this to Int32.MaxValue * and not had any side effects, so you might try that--this is actually the default used * in the native HTTP stack, WinHTTP. If you're not able to set * System.Net.ServicePointManager.DefaultConnectionLimit programmatically, you'll * need to disable autoConfig , but that means you also need to set maxWorkerThreads and maxIoThreads. * You won't need to set minFreeThreads or minLocalRequestFreeThreads if you're not using classic/ISAPI mode. * */System.Net.ServicePointManager.DefaultConnectionLimit = int.MaxValue;MaxConcurrentRequestsPerCPUYou can insert maxConcurrentRequestsPerCPU in aspnet.config like following<system.web> <applicationPoolmaxConcurrentRequestsPerCPU="5000" maxConcurrentThreadsPerCPU="0" requestQueueLimit="5000"/> </system.web>Of you can do following in registryHKLM\SOFTWARE\Microsoft\ASP.NET\2.0.50727.0\MaxConcurrentRequestsPerCPU = 5000It's not clear if you have v3.5 SP1, which is required in order to set the value in aspnet.config, so please try the registry change and restart IIS.autoConfigSpecifies whether to automatically configure the following settings to achieve optimal performance based on the machine configuration:The maxWorkerThreads attribute.The maxIoThreads attribute.The minFreeThreads attribute of the httpRuntime element.The minLocalRequestFreeThreads attribute of the httpRuntime element.The maxConnection attribute of the <connectionManagement> Element (Network Settings) element.The values are set according to the KB article at http://support.microsoft.com/?id=821268.This attribute does not affect the .NET Framework client applications; only ASP.NET applications.The autoConfig attribute can be one of the following values.TermDefinitionTrueIndicates that ASP.NET automatically configures the attributes in the preceding list to achieve optimal performance based on the machine configuration.FalseIndicates that ASP.NET should use the explicitly defined values for the attributes in the preceding list.The default in the Machine.config file is True, unless there is a previously existing configuration.
  • Web api scalability and performance

    1. 1. Discover. Master. Influence.
    2. 2. Lead Consultant at Readify @H_Desai http://himanshudesai.wordpress.com 2Discover. Master. Influence.
    3. 3. With thanks to our sponsors
    4. 4. • Request life cycle • Server Load Reduction • Measuring Performance • Server Configuration • Tools Discover. Master. Influence.
    5. 5. • Response time or latency – Measured @Server/Measured @Client • Throughput – Requests/sec • Resource utilization – CPU/Memory/Disk IO/Network IO Discover. Master. Influence.
    6. 6. HTTP request/response handling Client Server
    7. 7. 7Discover. Master. Influence. • Http Dispatcher Formatting Exception filters Your Code Request Action Filters Model Bind Authorization Filters Select action Route to controller Response
    8. 8. • Transferring Less Data – Http Caching (Etag,Max Age,Expires) –Efficient Content Types –Batching –Push Content Stream(Chunked) Discover. Master. Influence.
    9. 9. 9Discover. Master. Influence. Technique # 1 (Etag)
    10. 10. 10Discover. Master. Influence. Technique # 2 (Cache Control)
    11. 11. 11Discover. Master. Influence. Technique 3 (Throttling)
    12. 12. 12Discover. Master. Influence. Technique 4 (protocol)
    13. 13. Discover. Master. Influence. 0 500 1000 1500 2000 2500 3000 3500 4000 4500 xml json bson protobuf Formatters Payload (lower value - better)
    14. 14. 15Discover. Master. Influence. Serializer size serialize deserialize protobuf-net 3 268 1,881 proto# 3 76 1,792 BinaryFormatter 153 6,694 8,420 SoapFormatter 687 28,609 55,125 XmlSerializer 153 14,594 19,819 DataContractSerial izer 205 3,263 10,516 DataContractJsonS erializer 26 2,854 15,621 Source: https://code.google.com/p/protobuf-net/wiki/Performance
    15. 15. Discover. Master. Influence. How to implement Resource?
    16. 16. • I/O bound operation • Dashboard type app with low users • Long polling Discover. Master. Influence.
    17. 17. 20Discover. Master. Influence. Asynchronous (Demo)
    18. 18. Discover. Master. Influence. Measure?
    19. 19. • ASP.NET • CLR Performance Counters Discover. Master. Influence.
    20. 20. 24Discover. Master. Influence. • ASP.NET Performance Counters (IIS 6.0 or II7.0 classic mode) Client IIS App Domain App Domain QueueNamed Pipe Requests waiting to be written to client WebServiceISAPI Extension ReqSec ASP.NET Rejected Requests ASP.NET Requests Wait Time ASP.NET Requests Queued ASP.NET Requests in Application Queue ASP.NET Application Requests Executing ASP.NET Worker Process Restarts ASP.NET ApplicationsRequest Sec Pipeline ASP.NET Worker Process
    21. 21. 25Discover. Master. Influence. • Additional Performance Counters (IIS 7.0/7.5 integrated mode) Client IIS Named Pipe Pipeline W3SVC_W3WP Process W3SVC_W3WP(_Total)Requests / Sec W3SVC_W3WP(_Total)Active Requests
    22. 22. Discover. Master. Influence.
    23. 23. • IIS App pool – Queue Length (1000 by default) – Idle timeout (default is 20) • Aspnet_config or (registry) – maxConcurrentRequestsPerCPU – in v4.0 is 5000 (default) • ServicePointManager.DefaultConnectionLimit • Max ADO.NET connection Pool Size (default is 100) • Machine.config – Processmodel.Autoconfig (turned on by default) 28Discover. Master. Influence.
    24. 24. • Performance counters • Visual Studio Load test • Visual studio profiler (CPU/Memory/Concurrency measurements) • 3rd Party tools like Red gate’s ANTS profiler • Apache Benchmark 29Discover. Master. Influence.
    25. 25. • Transfer less data and make use of caching • Use efficient mime types for internal application • Don’t block your thread pool threads • Measure your throughput by tests • Tweak IIS settings when needed • Don’t run your test on Client OS 30Discover. Master. Influence.
    26. 26. Discover. Master. Influence.