I have a simple Web API that returns the list of contacts:
我有一个简单的Web API,它返回联系人列表:
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.
由于我使用秒表来测试数据检索性能,显然它只需不到一秒钟。但是,当我通过Chrome浏览器向GetContacts操作发出请求时,返回数据需要4到5秒。
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.
显然,延迟与我的数据检索代码无关。在我看来,Web API运行缓慢。但我不知道如何调试和跟踪它。
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?
是否有任何实用程序来记录ASP.NET HTTP请求流程管道的时间?我的意思是,像Navigation Timing这样的事情表明每个事件都发生在什么时候?
4 个解决方案
#1
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
你的反应有多大?也许这是序列化和转移的成本?但是,有很多可能对它进行分析,我将从使用市场上的一种工具(如ANTS Performance Profiler或dotTrace)进行分析开始。
#2
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.
你用调试器运行它吗?没有调试器进行一些测试。我当前正在开发的Web API项目遇到了类似的问题,对于我们关闭调试器,测试需要几毫秒而不是秒。
There also seems to be some startup cost when calling a API the first time, subsequent request are always faster.
在第一次调用API时似乎也有一些启动成本,后续请求总是更快。
#3
0
Try using Fiddler (http://fiddler2.com/), a free web debugging tool. It has most of the features that you are looking for.
尝试使用免费的网络调试工具Fiddler(http://fiddler2.com/)。它具有您正在寻找的大多数功能。
#4
0
4.5 seconds is pretty huge. If you use EF, you could use MiniProfiler.EF
4.5秒是非常巨大的。如果您使用EF,则可以使用MiniProfiler.EF
I experienced some slowdown ( in the past) by incorrectly using Entity Framework Queryable ( converting it to lists, expanding, ...).
我通过错误地使用Entity Framework Queryable(将其转换为列表,扩展,......)经历了一些减速(过去)。
If you are using EF, keep it IQueryable as long as possible ( .ToList() executes a Query).
如果您正在使用EF,请尽可能长时间保持IQueryable(.ToList()执行查询)。
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).
根据您的需要,使用调试工具,如MiniProfiler,MiniProfiler.Ef和其他建议的工具也可能是好的(虽然我以前没有使用它们)。
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.
序列化的成本可能很重要(如果你使用的是DTO),AutoMapper(可能还有其他工具)在大型列表上似乎很慢。如果你真的希望在大型列表上表现,我建议用扩展方法手动映射它们。
#1
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
你的反应有多大?也许这是序列化和转移的成本?但是,有很多可能对它进行分析,我将从使用市场上的一种工具(如ANTS Performance Profiler或dotTrace)进行分析开始。
#2
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.
你用调试器运行它吗?没有调试器进行一些测试。我当前正在开发的Web API项目遇到了类似的问题,对于我们关闭调试器,测试需要几毫秒而不是秒。
There also seems to be some startup cost when calling a API the first time, subsequent request are always faster.
在第一次调用API时似乎也有一些启动成本,后续请求总是更快。
#3
0
Try using Fiddler (http://fiddler2.com/), a free web debugging tool. It has most of the features that you are looking for.
尝试使用免费的网络调试工具Fiddler(http://fiddler2.com/)。它具有您正在寻找的大多数功能。
#4
0
4.5 seconds is pretty huge. If you use EF, you could use MiniProfiler.EF
4.5秒是非常巨大的。如果您使用EF,则可以使用MiniProfiler.EF
I experienced some slowdown ( in the past) by incorrectly using Entity Framework Queryable ( converting it to lists, expanding, ...).
我通过错误地使用Entity Framework Queryable(将其转换为列表,扩展,......)经历了一些减速(过去)。
If you are using EF, keep it IQueryable as long as possible ( .ToList() executes a Query).
如果您正在使用EF,请尽可能长时间保持IQueryable(.ToList()执行查询)。
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).
根据您的需要,使用调试工具,如MiniProfiler,MiniProfiler.Ef和其他建议的工具也可能是好的(虽然我以前没有使用它们)。
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.
序列化的成本可能很重要(如果你使用的是DTO),AutoMapper(可能还有其他工具)在大型列表上似乎很慢。如果你真的希望在大型列表上表现,我建议用扩展方法手动映射它们。