High Performance ASP NET Core Ilya Verbitskiy Web

High Performance ASP. NET Core Ilya Verbitskiy Web. Stoating s. r. o.

About me 15 years of experience in finance and e-commerce field Co-founder of Web. Stoating s. r. o. Using. NET since 2002

Modern. NET Framework • Base library • . NET Framework BCL • Application types • WPF • Windows Forms • Console • ASP. NET Core • Base library • . NET Core BCL • Application types • Console • ASP. NET Core • UWP • Xamarin. Forms Xamarin • Base library • Mono BCL • Application types • i. OS • Android • Mac. OS

. NET Core and. NET Standard. NET STANDARD . NET CORE Multiple. NET implementations . NET Core 2. 0 Code reuse Supports Windows, Linux и Mac. OS Specification Open Source (MIT) 70% of the existing code is version 2. 0 compatible Flexible deployment Compatibility mode Use any programming language Use any text editor

A little bit of philosophy You are not Google ROI Do not reinvent the wheel Solve you problem first There are many great tools around. Choose a right tool for YOUR project Performance problems are GREAT! But first, your product should became popular

Our site is so slow!

You site is in an Intranet - YSlow

Chrome Developer Tools

Implement all Google Page. Speed Insights recommendations first Sometimes it is enough, especially if you have many pictures on your web-site.

Java. Script and CSS bundling Build. Bundler. Minifier bundleconfig. json <environment> tag helper Gulp Grunt Webpack

Dependencies management Nu. Get - https: //www. nuget. org/ Packet - https: //fsprojects. github. io/Paket/index. html

Image processing Image. Magic - http: //imagemagick. org/ Magic. NET - https: //github. com/dlemstra/Magick. NET Don’t use System. Drawing и System. Windows. Media Lib. GD - http: //libgd. github. io/ Code. Art. Dotnet. GD Image. Processor Dynamic. Image. Resizer

HTTP compression Microsoft. Asp. Net. Core. Response. Compression Gzip. Compression. Provider IIS Dynamic Compression Module NGINX reverse proxy

Kestrel web-server Microsoft. Asp. Net. Core. Server. Kestrel Asynchronous web-server based on libuv. Can be used as a standalone web-server or via a reverse proxy Supports Unix domain sockets, Web. Sockets and HTTPS Parameters: Max. Concurrent. Connections (no limit) Max. Concurrent. Upgraded. Connections (no limit) Max. Request. Body. Size (28. 6 MB) and Request. Size. Limit. Attribute Min. Request. Body. Data. Rate (240 bytes/sec. with a 5 second grace period. )

Quick fixes didn’t help

Hardware is cheaper then developers You need a “bigger” server More RAM More CPU Docker support Cloud providers support ASP. NET Core Windows Azure AWS Google App Engine

Let’s look inside an HTTP request

Mini. Profiler Developed by Stack Exchange Mini. Profiler. EF 6 and Mini. Profiler. Entity. Framework. Core SQL queries generated by Entity Framework 6 and Entity Framework Core Stack. Exchange. Profiling. Data. Profiled. Db. Connection Profile Db. Connection Hepful with Dapper profiling Mini. Profiler. Mvc 5 and Mini. Profiler. Asp. Net. Core. Mvc Views Controllers

Digging into the system

SQL Server Profiler

Choosing the right ORM Маппинг POCO-объектов (SELECT, 500 итераций) Sql. Data. Reader 47 ms Dapper 49 ms Service. Stack. Orm. Lite 50 ms Peta. Poco 52 ms BLToolkit 80 ms Sub. Sonic Coding. Horror 107 ms NHibernate 181 ms Entity Framework 631 ms Источник: https: //github. com/Stack. Exchange/Dapper

Data layer optimization didn’t help Should we implement caching strategy?

Caching IMemory. Cache Microsoft. Extensions. Caching. Memory IDistributed. Cache Microsoft. Extensions. Caching. Redis Microsoft. Extensions. Caching. Sql. Server Cache Tag Helper <cache>@Date. Time. Now</cache> Distributed Cache Tag Helper <distributed-cache name=“my-id-1”>@Date. Time. Now</distributed-cache> Response. Cache. Attribute Microsoft. Asp. Net. Core. Response. Caching

Our app is leaking memory Is there a problem with GC?

GC modes Workstation GC Concurrent Non-concurrent Server GC Background Non-concurrent

Web. config <configuration> <runtime> <gc. Server enabled="true|false"/> <gc. Concurrent enabled="true|false"/> <gc. Trim. Commit. On. Low. Memory enabled="true|false"/>. . . </runtime> </configuration>
![[App. Name]. runtimeconfig. json { "runtime. Options": { "config. Properties": { "System. GC. Server": [App. Name]. runtimeconfig. json { "runtime. Options": { "config. Properties": { "System. GC. Server":](http://slidetodoc.com/presentation_image_h2/8f24366859a9ef4ebb070c95bb93f820/image-27.jpg)
[App. Name]. runtimeconfig. json { "runtime. Options": { "config. Properties": { "System. GC. Server": true|false, "System. GC. Concurrent": true|false, . . . }

Memory profiling in VS 2017

Perf. View: How much time does app spend in GC?

Perf. View: Where does the app allocate memory?

Perf. View: Current objects in the heap

No Visual Studio on the server? Win. Dbg C: bin>procdump. exe -ma Memory. Leak. loadby sos clr !dumpheap -stat !dumpheap -type System. String !do 0336 dc 54 !dumparray /d 0336 dc 54 !do 0336 dfb 8

Few words about pagination var persons = db. Person select First. Name, Last. Name. Order. By(p => p. First. Name) from Person . Then. By(p => p. Last. Name) order by First. Name, Last. Name. To. List(); offset 0 rows fetch next 10 rows only foreach(var person in persons. Take(100)) { … }

Span<T> and Memory<T>. NET Core 2. 1 System. Memory Synchronous method Span<T>, Read. Only. Span<T> Read/Write: T[], T*, stackalloc, Int. Prt Read only: string Asynchronous method Memory<T>, Read. Only. Memory<T>

“Manual” memory management Weak. Reference GCSettings. Latency. Mode Low. Latency (Workstation GC only) Sustained. Low. Latency (Workstation and Server GC) You should finish ASAP Do not allocate large objects GC. Collect GC. Try. Strt. No. GCRegion и GC. End. No. GCRegion Do you want to build your own GC? Local GC Project (Core. CLR) https: //github. com/dotnet/coreclr/tree/master/src/gc/sample

The app is still slow after GC tuning I/O, network and CPU problems.

Mini. Profiler critical section

Code profiling in VS 2017

Asynchronous operations

Task Parallel Library Paraller. For and Parallel. For. Each Task. When. Any, Task. When. All and Task. Continue. With Cancellation. Token Palallel LINQ

Async/await is “faster”, but it is not guaranteed Code is I/O and network intensive, but not CPU intensive You want to allow users cancelling long running operation Context switching helps your application running faster Asynchronous operations ARE NOT FASTER then synchronous Do not make all methods asynchronous in the application!

Message queues and microservices MQ Stack. Exchange. Redis Turbocharged. Beanstalk Zero. MQ Rd. Kafka Service Bus NService. Bus Mass. Transit и Mass. Transit. Rabbit. MQ Rest. Bus. Asp. Net и Rest. Bus. Rabbit. MQ

Summary 1. ROI 2. Implement all Google Page. Speed Insights recommendations 3. Vertical scalability 4. Check database 5. Memory profiling and GC tuning 6. Caching 7. I/O, network and CPU profiling 8. MQ and microservices

Thank you! https: //verbitskiy. co/ Twitter: @ilich_x 86 Git. Hub: https: //github. com/ilich
- Slides: 44