Reputation: 31
How can I test which method takes how much time to execute in a .Net web application?
In the log in page I have lots of methods and I want to know which method takes a long time to execute.
What is the best tool to find out the execution time of each method?
Upvotes: 2
Views: 2518
Reputation: 667
You could use tracing to check the diagnostic information about a single request for an ASP.NET page
Although there are 2 types of tracing
Application Level
To enable Tracing in the application level just add the below Element System.web
element.
<trace pageOutput="true"
enabled="true"
localOnly="false" //Making this attribute true ,will only show the tracing information in the local(server) !Recommended
traceMode="SortByTime"
/>
Lets say, If the URL of your application is http://localhost/SampleApplication, to Check the Trace info for your application change the URL to http://localhost/SampleApplication/trace.axd
Page Level
If you are just looking for page level tracing then add the trace attribute to the page directive
<%@ Page Language="C#"
Trace="true"
traceMode="SortByTime"%>
Hope this helps!
Upvotes: 1
Reputation: 13971
Developers must run profiling to find the performance of their applications
The Visual Studio Profiling Tools let developers measure, evaluate, and target performance-related issues in their code. These tools are fully integrated into the IDE to provide a seamless and approachable user experience.
Profiling an application is straightforward. You begin by creating a new performance session. In Visual Studio Team System Development Edition, you can use the Performance Session Wizard to create a new performance session. After a performance session ends, data gathered during profiling is saved in a .vsp file. You can view the .vsp file inside the IDE. There are several report views available to help visualize and detect performance issues from the data gathered.
To create and run a performance session find reference here
Upvotes: 0
Reputation: 1241
You can use a profiler tool like dotTrace. Such tools will usually give you a pretty good idea of the execution times.
Since the login probably executes database queries, you may want to check how long the queries take on their own. With an OR mapper that's usually the bottleneck anyway. When using NHibernate NHibernate Profiler is basically a must have.
Without tools, I'd recommend to implement a LoggingStopwatch
which utilizes your logging class and a StopWatch
internally.
Simplified example:
public class LoggingStopwatch : IDisposable
{
public LoggingStopwatch(ILogger logger, LogLevel level, string message)
{
this.logger = logger;
this.logLevel = logLevel;
this.message = message;
this.stopwatch = new Stopwatch();
this.Start();
}
public void Start()
{
// compare this.logLevel with the one set in this.logger
// and return if no output will be generated
// start stopwatch, log entry
}
public void Stop()
{
// stop stopwatch, log entry
}
public void Dispose()
{
this.Stop();
}
}
(Simplified to show the actual idea. I mean it! That's not how you actually implement the Disposable
pattern for example.)
Since it implements IDisposable
, you can use it like this:
using (new LoggingStopwatch(this.logger, LogLevel.Trace, "initializing backend session or whatever")
{
// business logic
}
Upvotes: 1