2

What is the best way to measure the code run time in ASP.NET page?

Here is my code, trying to time the page load and writing to log.

   private Stopwatch PageTimer = null;

    protected void Page_Init(Object Src, EventArgs E)
    {
        if (!IsPostBack)
        {
            PageTimer = new Stopwatch();
            PageTimer.Start();


        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (!IsPostBack)
        {
            PageTimer.Stop();
            Logger.SectionEnd("PageTimer", PageTimer, "", true);
        }
        base.OnPreRender(e);
    }
SirMoreno
  • 1,109
  • 1
  • 15
  • 34

5 Answers5

4

You may try mvc-mini-profiler.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

I'd recommend you use a HttpModule for that. There's one you can use at

HttpModule For Timing Requests (pretty old, I know, but still valid)

A good thing about HttpModules is that you have them self-register themselves if the "main application" has code for it. Using a module in another application is then just a copy paste in to the bin folder and it will start to operate.

If the "main application" doesn't support modules to self-register it can be added to the bin and web.config to start operating.

Asken
  • 7,679
  • 10
  • 45
  • 77
2

If you'd like something simple and fast i suggest using the global.asax file.
You can create this file by selecting new file in a website.

The global.asax file has a few methods, the ones you should be looking for are
Application_BeginRequest
Application_EndRequest

You could start a timer, write some logging , do whatever to get the time between these 2 requests.
By doing it like this you have the exact time the request spent on your server, if you do it in the page_load and page_prerender you miss out on stuff like redirection, authentication, membership etc

Kristof
  • 3,267
  • 1
  • 20
  • 30
  • The timer can be stored in Context.Items. I did that with a Stopwatch and it seems to work fine. – jahu Apr 28 '21 at 13:00
1

Here is another choice: Web Performance Test Using Visual Studio

tsinghua
  • 23
  • 4
0

If you exclude logging, you can use firebug to measure client-side performance.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
frabiacca
  • 1,402
  • 2
  • 16
  • 32