8

I have a simple Web API that returns the 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've used Stopwatch to test data-retrieval performance, it's apparent that it takes less than a second. However, when I issue a request to the GetContacts action via Chrome browser, it takes 4 to 5 seconds to return data.

enter image description here

Apparently that delay has nothing to do with my data-retrieval code. It seems to me that Web API is running slow. But I have no idea how to debug and trace that.

Is there any utility to log timing for ASP.NET HTTP request process pipeline? I mean, something like Navigation Timing to show that each event has occurred in what time?

3
  • 1
    Have you tried fiddler? Commented Nov 12, 2013 at 5:50
  • 2
    I think you need to narrow down where it's slowing down. It might be ASP.NET, it might be IIS, it might be down at the network layer. Have you tried attaching the debugger and putting a breakpoint at the first line of the function? If not, do that and see if it's called 4.5 seconds later or straight away. 4.5 seconds is a long time, sounds networky related to me. Commented Nov 12, 2013 at 5:53
  • 1
    @amrswalha, fiddler won't help here. The problem is on the server. OP is talking about ASP.NET HTTP Process Pipeline. Commented Nov 12, 2013 at 5:59

4 Answers 4

5

How big is your response? Maybe it is a cost of serialization and transfer? However, there is a lot of possibilities to profile it, I would start from profiling with one of the tools in the market like ANTS Performance Profiler or dotTrace

Sign up to request clarification or add additional context in comments.

Comments

2

Are you running it with the debugger? Do some tests without the debugger. I had similar problems with a web API project I am currently developing and for us turning off the debugger made the test take milliseconds instead of seconds.

There also seems to be some startup cost when calling a API the first time, subsequent request are always faster.

1 Comment

we ran tests without debugger as well! even after caching the loaded file it takes about 4 5 seconds to retrieve data from the server, it might be cause of the network, not sure yet!
0

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

1 Comment

this is not something that can be done with fiddler! as Saeed Neamati mentioned on earlier answer
0

4.5 seconds is pretty huge. If you use EF, you could use MiniProfiler.EF

I experienced some slowdown ( in the past) by incorrectly using Entity Framework Queryable ( converting it to lists, expanding, ...).

If you are using EF, keep it IQueryable as long as possible ( .ToList() executes a Query).

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

The cost of serialization could be important ( if ou are using DTO's), AutoMapper ( and probably other tools) seems slow on large lists. I'd suggest manually mapping them in an extension method, if you really want performance on big lists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.