0

I'm trying to measure the response time for a "process" (I'm requesting data from a server and then presenting the data). I want to measure the time it takes from when I ask for the data (when I press the "send" button) to when the data is displayed in my txtbox.

That looks like this:

    (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 it works, but just the first time. If I send another request the time is just added to the old time. The first request takes 2 seconds, and the second it says it took 7 seconds (when in reality it took like 2).

I tried circumventing the problem by reseting a and b by putting:

    a = 0; 
    b = 0;

in the reset button, but that only seem to have made things go a bit crazy.

Any thoughts on how to solve the problem?

Thanks

cssprobs
  • 35
  • 1
  • 4
  • 1
    System.currentTimeMillis() is to be used to get the system time. To measure elapsed time use System.nanoTime() – Antoine Wils May 05 '15 at 23:04
  • @AntoineWils In what unit would I get the results if using nanotime? – cssprobs May 05 '15 at 23:12
  • @cssprobs Erm, [nanoseconds](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()). You can convert it to the unit you want using `TimeUnit`. – Andy Turner May 05 '15 at 23:16
  • in billions of seconds. Divide by 100000 once you have the elapsed time to get the amount of milliseconds. Read this post for other ways http://stackoverflow.com/questions/924208/how-to-convert-nanoseconds-to-seconds-using-the-timeunit-enum – Antoine Wils May 05 '15 at 23:17
  • @AntoineWils OK, I got it to work I think, but I had to divide the nanoseconds with 1 000 000 and not 100 000, which seems to be the correct way. – cssprobs May 05 '15 at 23:47
  • @cssprob apologies it was a typo. It is indeed to be devided by one million. I am happy you got it – Antoine Wils May 05 '15 at 23:51

1 Answers1

0

It looks an awful lot like you are setting the value of a when you create the button, and b when you click it. If you do that, then you will see the results you are seeing. A will stay the same, and B will get further and further away from it. Then when you reset, things will "go a bit crazy" because now A is equal to zero. So it will say that your round trip took about 45 years (the time since 1970, which is the 0 value of currentTimeMillis().)

Instead, you want to set the value of A when you click the button, and B after you have your result.

Like this:

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

    }
Kevin Walker
  • 116
  • 4
  • See http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java for a description of how to correctly measure elapsed time (using `System.nanoTime()` instead - not for precision, but because it is designed to measure elapsed time, unlike `System.currentTimeMillis()`) – Andy Turner May 05 '15 at 23:05
  • @Kevin Walker That worked perfectly, thanks!One question though; why does the "process" take longer the first time I ask for the data than when I try once again (in the same window)? It's ca 180ms vs 55ms. – cssprobs May 05 '15 at 23:07
  • @cssprobs : It is very likely cached somewhere poll number two, but I can't answer that without a lot more detail about what you are pulling and exactly what "control" is and how "getFileContents" is implemented. – Kevin Walker May 05 '15 at 23:14
  • @KevinWalker OK, I thought maybe it was that the connection had already been opened or something like that. Doesn't matter thought, works good as is. – cssprobs May 05 '15 at 23:29