asp.net how to calculate the execution time per page _ Practical tips

Source: Internet
Author: User
Tags httpcontext

The example in this article describes how ASP.net calculates the time each page executes. Share to everyone for your reference. The specific analysis is as follows:

Here the ASP.net code can calculate the execution time of each page, no need to modify the relevant code of the page, this code will give all pages unified plus execution time display

public class Performancemonitormodule:ihttpmodule {public void Init (HttpApplication context) {context.  PreRequestHandlerExecute + = Delegate (Object Sender,eventargs e) {//set Page Timer Star HttpContext RequestContext = ((HttpApplication) sender).
   context;
   Stopwatch timer = new stopwatch ();
   requestcontext.items["Timer"] = timer; Timer.
   Start ();
  }; Context. PostRequestHandlerExecute + = Delegate (object sender, EventArgs e) {HttpContext HttpContext = ((HttpApplication) Sende R).
   context;
   HttpResponse response = Httpcontext.response;
   Stopwatch timer = (stopwatch) httpcontext.items["Timer"]; Timer.
   Stop (); Don ' t interfere with non-html responses if (response. ContentType = = "text/html") {Double seconds = (double) timer.
    elapsedticks/stopwatch.frequency; String result_time = String.
    Format ("{0:f4} sec", seconds);
   Renderqueriestoresponse (Response,result_time);
 }
  }; } void Renderqueriestoresponse (HttpResponse response, String REsult_time) {Response.
  Write ("<div style=\" margin:5px; Background-color: #FFFF00 \ ""); Response. Write (String.
  Format ("<b>page generated in" + Result_time)); Response.
 Write ("</div>"); public void Dispose () {/* not needed/}}

I hope this article will help you with your asp.net programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.