views:

212

answers:

2

I’m trying to measure a request with WebRequest,

But I’m getting significant smaller results then measuring with FireBug.

I guessing it’s because some content like Images and CSS isn’t included.

Is there a way to measure a full web request?

My code:

        public string GetPageHtmlTime(string strUrl)
    {
        WebRequest request = null;
        WebResponse response = null;
        HttpWebResponse httpCurrentWeResponse = null;

        try
        {
            //making a request to the file.
            request = WebRequest.Create(strUrl);
            //set 5 seconds timeout for the request
            request.Timeout = 5 * 1000;

            //Stopwatch
            Stopwatch sw = new Stopwatch();
            sw.Start();

            //get the server response
            response = request.GetResponse();
            httpCurrentWeResponse = (HttpWebResponse)response;
            sw.Stop();

            //if the http response return any type of failure
            if (httpCurrentWeResponse.StatusCode != HttpStatusCode.OK || response == null)
                return "Error: " + httpCurrentWeResponse.StatusCode;

            response.Close();

            //Return time:
            return "OK time=" + sw.ElapsedMilliseconds.ToString("0,0");

        }
        catch (System.Exception ex)
        {
            return "Error: ex=" + ex.Message;
        }

    }
A: 

Your code will only measure how long it takes for the code complete, the code will not wait for all the bytes to arrive at the client which will take significantly longer than the code.

What and where measure depends on where you expect to make optimisations. If you want to improve the experience at the client when the server is under light load then Firebug (or Fiddler) would be a good place to be measuring. If you wan't to improve performance on the server when its under heavy load then code profilers would the sort of tool you would be needing.

AnthonyWJones
+1  A: 

I don't know if it's an option for you, but you can use the WebBrowser control, as it will request all the elements of the page before firing the DocumentCompleted event.

klausbyskov