0

我正在尝试测量“进程”的响应时间(我正在从服务器请求数据,然后呈现数据)。我想测量从我请求数据(当我按下“发送”按钮时)到数据显示在我的 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");
            }

        }

蚂蚁它的作品,但只是第一次。如果我发送另一个请求,则时间只会添加到旧时间。第一个请求需要 2 秒,第二个请求需要 7 秒(实际上它需要 2 秒)。

我尝试通过重置 a 和 b 来规避问题,方法是:

    a = 0; 
    b = 0;

在重置按钮中,但这似乎只会让事情变得有点疯狂。

关于如何解决问题的任何想法?

谢谢

4

1 回答 1

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");
        }

    }
于 2015-05-05T22:46:38.730 回答