【问题标题】:Equivalent java code to measure response time of server用于测量服务器响应时间的等效 java 代码
【发布时间】:2011-05-11 14:48:06
【问题描述】:

请问我应该为以下 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;

提前致谢。

【问题讨论】:

    标签: c# java httprequest response-time


    【解决方案1】:

    试试:

    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;
    

    【讨论】:

    • 嗨,谢谢,但我需要的是等效于 java 中的 httpwebrequest 和响应
    【解决方案2】:

    查找 HttpURLConnection 示例,例如 this one

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-20
      • 2013-01-28
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2012-08-10
      • 2023-03-19
      相关资源
      最近更新 更多