用于测量服务器响应时间的等效java代码

时间:2011-05-11 14:48:07

标签: c# java httprequest response-time

我应该使用以下C#代码的等效java api是什么?我需要的是检查响应时间。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
System.Diagnostics.Stopwatch timer = new Stopwatch();
timer.Start();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;

提前致谢。

2 个答案:

答案 0 :(得分:5)

尝试:

long start = System.currentTimeMillis();
//doSth.

long elapsed = System.currentTimeMillis() - start;

为了获得更高的精确度,您可以使用System.nanoTime()返回两次调用之间的纳秒。请注意:This method provides nanosecond precision, but not necessarily nanosecond accuracy.

从URL读取并测量速度的简单方法可能是:

 long start = System.currentTimeMillis();
 URL url = new URL(urlString);  
 url.getContent();
 long elapsed = System.currentTimeMillis() - start;

答案 1 :(得分:0)

查找像this one这样的HttpURLConnection示例。

然后使用System.nanotime()对其进行基准测试