用java(web服务)测量响应时间?

用java(web服务)测量响应时间?,java,web-services,soap,response-time,Java,Web Services,Soap,Response Time,我试图测量“进程”的响应时间(我从服务器请求数据,然后显示数据)。我想测量从我请求数据(按下“发送”按钮)到数据显示在我的txtbox中所花费的时间 看起来是这样的: (these two are at the very top:) private long a private long b ...other code... a = System.currentTimeMillis(); btnSend.addActionListener(

我试图测量“进程”的响应时间(我从服务器请求数据,然后显示数据)。我想测量从我请求数据(按下“发送”按钮)到数据显示在我的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;
在重置按钮,但这似乎只是让事情变得有点疯狂

对如何解决这个问题有什么想法吗


谢谢

当您创建按钮时,它看起来非常像您正在设置a的值,而当您单击按钮时,它看起来非常像您正在设置b的值。如果你这样做,那么你将看到你正在看到的结果。A将保持不变,B将离它越来越远。然后当你重置时,事情会“变得有点疯狂”,因为现在a等于零。因此,它会说您的往返行程花费了大约45年(自1970年以来的时间,即currentTimeMillis()的0值)

相反,您希望在单击按钮时设置A的值,在得到结果后设置B的值

像这样:

btnSend.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String fileContents;
        a = System.currentTimeMillis();
        try {
            fileContents = control.getFileContents(txtSearch.getText());
            b = System.currentTimeMillis();
            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");
        }

    }

System.currentTimeMillis()用于获取系统时间。要测量经过的时间,请使用System.nanoTime()@AntoineWils,如果使用nanoTime,我将以什么单位获得结果?@cssprobs-Erm。您可以使用
TimeUnit
,以数十亿秒的时间将其转换为所需的单位。一旦您有了获取毫秒数所用的时间,就除以100000。阅读这篇文章了解其他方法@AntoineWils好的,我想我已经成功了,但我必须将纳秒除以1 000 000而不是10 000,这似乎是正确的方法。有关如何正确测量经过的时间的描述,请参阅(使用
System.nanoTime()
相反-不是为了精确,而是因为它是为测量经过的时间而设计的,不像
System.currentTimeMillis()
)@Kevin Walker那样工作得很好,谢谢!但有一个问题;为什么我第一次请求数据时的“过程”比我再次尝试时(在同一窗口中)花费的时间更长?大约是180ms对55ms。@cssprobs:很可能是缓存在第二个投票点的某个地方,但如果没有更多关于您正在拉什么、确切的“控件”是什么以及“getFileContents”是如何实现的细节,我无法回答这个问题。@KevinWalker好的,我想可能是连接已经打开或类似的事情。不要紧的思想,工作是好的。