【发布时间】:2015-05-05 22:35:47
【问题描述】:
我正在尝试测量“进程”的响应时间(我正在从服务器请求数据,然后呈现数据)。我想测量从我请求数据(当我按下“发送”按钮时)到数据显示在我的 txtbox 中所花费的时间。
看起来像这样:
(these two are at the very top:)
private long a
private long b
...other code...
a = System.currentTimeMillis();
btnSend.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
String fileContents;
b = System.currentTimeMillis();
try {
fileContents = control.getFileContents(txtSearch.getText());
txtView.setText(fileContents + "\n" + "\n" + "The process took "+(b-a)+"milliseconds to execute." + "\n" + "("+((b-a)/1000)+" seconds)");
} catch (RemoteException e) {
txtView.setText("File not found");
}
}
Ant 它可以工作,但只是第一次。如果我发送另一个请求,则时间只会添加到旧时间。第一个请求需要 2 秒,第二个请求需要 7 秒(实际上需要 2 秒)。
我尝试通过重置 a 和 b 来规避问题:
a = 0;
b = 0;
在重置按钮中,但这似乎让事情变得有点疯狂。
关于如何解决问题的任何想法?
谢谢
【问题讨论】:
-
System.currentTimeMillis() 用于获取系统时间。要测量经过的时间,请使用 System.nanoTime()
-
@AntoineWils 如果使用纳米时间,我会以什么单位获得结果?
-
@cssprobs Erm,nanoseconds。您可以使用
TimeUnit将其转换为您想要的单位。 -
数十亿秒。一旦你有经过的时间得到毫秒数,除以 100000。阅读这篇文章以了解其他方式stackoverflow.com/questions/924208/…
-
@AntoineWils 好的,我想我让它工作了,但我必须将纳秒除以 1 000 000 而不是 100 000,这似乎是正确的方法。
标签: java web-services soap response-time