Search code examples
javaweb-servicessoapresponse-time

Measure response time in java (web service)?


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


Solution

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