简体   繁体   English

测量来自REST客户端的HTTP响应时间

[英]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). 我正在尝试创建一个REST客户端,以衡量执行HTTP请求所花费的时间,即衡量客户端的请求与服务器到达客户端后服务器的响应之间的时间。(还有其他更简单的方法可以找到像提琴手之类的东西,但是我还是需要这个)。 I am following Microsoft's example provided here: 我遵循此处提供的Microsoft示例:

https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx 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. 根据msdn文档GetResponse()将请求发送到服务器并获取响应。

If you're just interested in timing the call, then the first option would be your best bet. 如果您只想安排通话时间,那么第一个选择就是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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