ASP.NET Web API Performance Problem

I have a simple web API that returns a list of contacts:

public class ContactsApi : ApiController { public List<Contact> GetContacts() { Stopwatch watch = new Stopwatch(); watch.Start(); // Doing some business to get contacts; watch.Stop(); // The operation only takes less than 500 milliseconds // returning list of contacts } } 

As I used Stopwatch to test data search performance, it obviously takes less than a second. However, when I submit a GetContacts action GetContacts through the Chrome browser, it takes 4 to 5 seconds to return the data.

enter image description here

Apparently this delay has nothing to do with my data retrieval code. It seems to me that the web API is slow. But I do not know how to debug and track this.

Is there any utility for registering time for the ASP.NET HTTP request process pipeline? I mean something like "Navigation Time" to show that each event happened at what time?

+8
performance c # asp.net-web-api asp.net-mvc-4
source share
4 answers

How big is your reaction? Maybe this is the cost of serialization and transmission? However, there are many possibilities for profiling it, I would start by profiling with one of the tools on the market, for example ANTS Performance Profiler or dotTrace

+5
source share

Do you run it with a debugger? Perform some tests without a debugger. I had similar problems with the web API project that I am developing now, and for us I turned off the debugger so that the test would take milliseconds instead of seconds.

Also, as a rule, when launching the API, there is some cost to launch, the subsequent request is always faster.

+2
source share

Try using Fiddler ( http://fiddler2.com/ ), a free web debugging tool. It has most of the features you are looking for.

0
source share

4.5 seconds is pretty awesome. If you use EF, you can use MiniProfiler.EF

I had some slowdown (in the past) by misusing the Entity Framework Queryable (converting it to lists, extension, ...).

If you use EF, keep it IQueryable as long as possible (.ToList () executes the query).

According to your needs, use debugging tools like MiniProfiler, MiniProfiler.Ef and other suggested tools are probably good too (although I haven't used them in the past).

Serialization costs can be important (if the OU uses DTO), AutoMapper (and possibly other tools) seems slow on large lists. I would suggest manually matching them in the extension method if you really need performance on large lists.

0
source share

All Articles