简体   繁体   中英

Measure HTTP Response Time from a REST Client

I am trying to create a REST Client to measure the time taken for a HTTP Request to execute, that is to measure the time between the Client's request and the response from the server after it reaches the client.(There are other easier approaches to find this like fiddler etc, but I need this anyway). I am following Microsoft's example provided here:

https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx

should I just note the time when the response is returned?

// Send the request:

        DateTime T = System.DateTime.UtcNow; //--> Note the initial Time

        HttpWebResponse response = (HttpWebResponse) req.GetResponse();

        TimeSpan TT = System.DateTime.UtcNow - T; //--> Note the Time Difference

or should I be calculating the time after the response stream is read:

     DateTime T = System.DateTime.UtcNow;//--> Note the initial Time

                HttpWebResponse response = (HttpWebResponse) req.GetResponse();
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();                
TimeSpan TT = System.DateTime.UtcNow - T;//--> Note the Time Difference

I am just not sure about the exact lines where the Request is made to the server and the Response from the Server are available to the Client.

According to the msdn documentation , GetResponse() will send the request to the server and retreive the response.

If you're just interested in timing the call, then the first option would be your best bet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM