如何测量websocket响应所花费的时间

时间:2016-05-13 05:23:15

标签: java websocket

我们在项目中使用Web套接字,并且需要评估websocket的速度。如何衡量websocket响应的时间

1 个答案:

答案 0 :(得分:0)

鉴于我是Stack Overflow的新手,我不能写评论,我会尝试给你一个回答你发布的信息。

如果你去Google,你会发现很多关于“如何计算已用时间”或“用Java执行时间”的例子。以下示例摘自mkyong

日期()的getTime():

long lStartTime = new Date().getTime();
//some tasks
long lEndTime = new Date().getTime();

long difference = lEndTime - lStartTime;

System.out.println("Elapsed milliseconds: " + difference);

System.currentTimeMillis的()

long lStartTime = System.currentTimeMillis();
//some tasks
long lEndTime = System.currentTimeMillis();

long difference = lEndTime - lStartTime;

System.out.println("Elapsed milliseconds: " + difference);

System.nanoTime()

long lStartTime = System.nanoTime();
//some tasks
long lEndTime = System.nanoTime();

long difference = lEndTime - lStartTime;

System.out.println("Elapsed milliseconds: " + difference/1000000); 

完整示例

import java.util.Date;

public class TimeApp {
  public static void main(String[] argv) {

    long lStartTime = new Date().getTime(); // start time

    createArray(); // some tasks to eat time

    long lEndTime = new Date().getTime(); // end time

    long difference = lEndTime - lStartTime; // check different

    System.out.println("Elapsed milliseconds: " + difference);

  }

  public static void createArray() {

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    String sArray[] = new String[1000000];

    for (int i = 0; i < 1000000; i++)
      sArray[i] = "Array " + i;

  }
}

希望这会对你有帮助!