Link to home
Start Free TrialLog in
Avatar of wjh7554
wjh7554

asked on

"Timing" Calculation:

Hi all,

I need to perform some tasks to calculate the total time that needed to execute a specific method.

But I am totaly no idea what kind of packages that I should use. I have tried to look into java..util.* and the Timer inside is to let me to schedule something and not what I am looking for.

http://java.sun.com/j2se/1.3/docs/api/

So, I am wondering anyone in here can tell me what package of what interface or what API's classes that I can use in order for me to do this.

I would be very happy if somebody can show me how to do.. ha....

But other than that i have some questions....

1. if I am putting all my code in one method, like this

*******************************
public Resulset execute() {
//start of my timer
..
..
1. connect to db
2. Execute my squery
3. return result set.

//stop the timer.
//calculate the time
}

**********************

Then should be no problem right.

But what happend if I have separate my code into modular... like this...

public boolean getValid() {
 boolean test = executequery();
return test;
}
...
...

public boolean executequery() {
...
...
boolean ok=false;
//connection;
//query statement;
//run the statement

if ok, return ok;
}

public boolean hasNex() {
if (rs.next()) {
...
...
}



Then how am i going to time the timer??
Is it I have to start to count from the getValid been called, and till hasNext() method been called?

Orrrrrrrrrrrrrrrrrrrrrrr, it's up to me to decide....

Friend, give me some idea. and also guidance....

Thanks in advance..
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wjh7554
wjh7554

ASKER

ken,

The start time I get is this,
1046134075031

and the endtime is this
1046134076047

Duration :
1016

Question is , what is this number?? H.a....

104134075031, meaning what???
http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#currentTimeMillis()
according to api doc
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

so your task tooks 1.016 secound.
Avatar of wjh7554

ASKER

kennethxu,

Thank you.
Avatar of wjh7554

ASKER

kennethxu, it's should be 10.16 and not 1.016.

Agree??

DOne a very simple test against my count... hm... i think I am correct.

Anyway, thanks for your respond
it should be 1 second = 1000 milli second, try this code:

public class Timing {
     public static void main( String[] args ) throws Exception {
          long startTime = System.currentTimeMillis();
          Thread.sleep( 2000 );
          long endTime = System.currentTimeMillis();
          long duration = endTime - startTime; // in milli second
          System.out.println( duration );
     }
}
Avatar of wjh7554

ASKER

kennethxu,

i cn't convert the 1026 to 1.026 or 10.26 either...

I can only see 1.00 or 1 only...

I have tried again the numberformat and also the decimalformat...

let's solve this first ..

DecimalFormat nf = new DecimalFormat("##,###.00");
long duration = endTime - startTime;
double result = duration/100;

out.println("Duration :" +duration);
out.println("Result :" +nf.format(result));

Output:
Duration : 596
Result : 5.00

How??

Avatar of wjh7554

ASKER

friend, what is this Thread.sleep(2000);??

the only changed is the duration become higher value. and I suspect the 2000 shoud be 1000, but after change it the figure doesn't look what I suppose to be getting..
>> double result = duration/100;
<< double result = duration/100.0;


>> Thread.sleep(2000);??
sleep 2000 milli second, I use this to illustrate you it is 2 second, not 20 second.
Avatar of wjh7554

ASKER

solved.

How come ??
you add .0 at behind only.

This is the normal trouble shoot method right? Ha... OK copy that.

Kennethxu, serious speaking.
startTime : 1046152354318
endTime   : 1046152355599
Duration  :          1281
===========================

So, if this is mili = 1000, then I have 1.28 seconds respond time which is imposible.

And if I said it's 12.8 seocnds, then it's logical because I am coounting when I click the refresh button.

Opps, I entered the counting in the JSP page


<%@ page language="java" import ="java.sql.*,
                      java.text.*,
                      java.math.*,  
                      java.io.*,
                      java.lang.System,
                      java.util.*,
                      com.wrox.cars.*"  %>

<%@ page contentType="text/html" errorPage="handle_error.jsp" %>

<jsp:useBean id="zz" class="com.wrox.cars.zz" />          

<html>
<body  
<br>
<%
long startTime=System.currentTimeMillis();

out.println("Start Time :"+startTime);

connect();
ResultSet rszz = zz.viewDoc();
int counter=0;

while (rszz.next()) {
..
//<% rszz.getString(ABC); %>
...
...

}

long endTime=System.currentTimeMillis();
out.println("EndTime :"+endTime);
long duration = endTime - startTime;

double result = duration/100.0;
DecimalFormat nf = new DecimalFormat("##,###.00");

out.println("Duration :" +duration);
out.println("Result :" +nf.format(result));

%>

</body>
</html>
*************

So, the logic no problem right??    
>> you add .0 at behind only.
according Java Language Specification, the result of integer devide integer is integer, any reminder is ignored.

>> So, the logic no problem right??      
your logic is correct, but it is double result = duration/1000.0;

1. when the first time you access a changed jsp page, server will have to compile your jsp page first, that takes long time on a slow machine.
2. from you hit refresh, browser initiate network connection, send request to server, the server parse the request and then call your jsp page, that takes time too.
3. after browser received the data from server, it again takes time to display it on your screen, it take a lot longer time when you have a big page.
those are all contribute to the delay between you hit refresh and see the page and not counted in the logic. In fact, there is no way to count them.

the result is telling you the your bottleneck is not at your jsp page, rather somewhere else.

how many rows are you trying to display in the page.
Avatar of wjh7554

ASKER

>>how many rows are you trying to display in the page.

consistantly 90 records.

If the item that you've mentioned above can't be count. Then it seems that my purpose to put in timing on counting can't be achieve.

Actually, we are planning to upgrade the server (AS400) capabilities on handling this JSP ans Java stuff.. But before that I need to gather some solid evidence on how slow is the respond time of each page have been called.

I agree with you regarding some of the factors that may cause the respond become higher.

Anyway, the solution that you suggested to me is to measure the timing in between, right?
So, that mean (refer to my code) it's counting the time spent to get connect, retrieve info and also close connection might take.

So, if 1012, it's only take 1.012 second (yes, divide by 1000). The actual situation need 10 seconds.

Ken, can give me some idea how to capture the exact timing that needed to display a pages, a jsp page (include the time neede in between the connectivitiy among browser and server and etc...)

>> Anyway, the solution that you suggested to me is to measure the timing in between, right?
right!

>>So, that mean (refer to my code) it's counting the time spent to get connect, retrieve info and also close connection might take.
yes, if you mean databse connection.
I'm not sure if you are using connection pool. A real world application use connection pool, which will be much faster, milli-seconds vs seconds, then not using connection pool.

>>The actual situation need 10 seconds.
that's too much. except jsp re-compile that only happens once, other overhead shouldn't be any longer then 1 second. try to ping your server from client machine to see the response time to see if you have network problem.

>> can give me some idea how to capture the exact timing that needed to display a pages.
there is no way for server side program to detect http/network/broswer overhead. but there are some load/performance test tools can help you.
you can find a list of those tools: http://www.softwareqatest.com/qatweb1.html#LOAD
some are free, like TestMaker, Apache JMeter, those tools are not easy to use, I personally don't have much experience with them.
Avatar of wjh7554

ASKER

kennethxu, thanks a lot for your time.

I got what you mean already.

Thanks.....