.net core slow upon first request


#1

.net core is slow upon first request, and then subsequent requests are super fast.

Please refer to following stats:
First hit to application:

[18:18:55 INF] Request starting HTTP/1.1 GET http://localhost:5008/api/Print/Test
[18:19:08 INF] Executing action method Controller.Test (Service) with arguments (null) - ModelState is Valid
[18:19:09 INF] Executing JsonResult, writing value { Message = Test success }.
[18:19:13 INF] Executed action Controller.Test (Service) in 9159.67ms
[18:19:13 INF] Request finished in 18372.9308ms 200 application/json; charset=utf-8

Second hit

[18:21:46 INF] Request starting HTTP/1.1 GET http://localhost:5008/api/Print/Test
[18:21:46 INF] Executing action method Controller.Test (Service) with arguments (null) - ModelState is Valid
[18:21:46 INF] Executing JsonResult, writing value { Message = Test success }.
[18:21:46 INF] Executed action Test (Service) in 64.5092ms
[18:21:46 INF] Request finished in 85.1069ms 200 application/json; charset=utf-8

Can someone please tell me, why it is taking so much time for first request?
How this delay can be avoided?


#2

same issue here, i think reason because of asp.net core lifecycle.


#3

Need a little more info: What are you running your code on? macOS/Linux/Windows and where? MacBook/RaspberryPi/AWS/Azure, PC/Surface/Laptop? etc. If PC, hat kind of processor, memory and storage are you using?

Also, note that most modern platforms have a startup cost. If, for example, you’re running CoreCLR on Windows on a PC, the JIT has to parse the IL of your classes in your assemblies, and compile to machine code before it can execute anything. If your site is using full ASP.NET & MVC, etc., that means it’s having to load, parse, compile and then execute the code in several hundred assemblies. This can take some time.

HOWEVER, ~20s to startup a website sounds high: On my Surface Pro 4 Core-i7 with 16GB RAM and a 512GB SSD, running a .NET Core 1.1 ASP.NET MVC site generated via Yeoman

  • First-hit takes 2756.8643ms to execute the HomeController.Index() method, and 3056.1864ms to complete the request.
  • Second hit takes 11.7073ms to execute the HomeController.Index() method, and 46.4979ms to complete the request.
  • Fifth hit takes 24.7564ms to execute the HomeController.Index() method, and 3.858msto complete the request.

There’s a reason any responsible tester/benchmarker always runs a test suite 2-3 times before running the recorded run: This makes sure everything is compiled, CPU/memory/server/DB/disk caches are full, etc.

My guess is that you’re running a machine with a spinning hard-drive which is MASSIVELY slower than an SSD. If so, I’d encourage you to replace your HDD with a half decent SSD which are increasingly affordable these days and will likely result in your PC demonstrating MASSIVE performance & responsiveness improvements.

HTH.


.NET Foundation Website | Blog | Projects | Code of Conduct