• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

"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..
0
wjh7554
Asked:
wjh7554
  • 8
  • 7
1 Solution
 
kennethxuCommented:
java.lang.System.currentTimeMillis() is your friend :)

long startTime = System.currentTimeMillis();
//do the task
long endTime = System.currentTimeMillis();
long duration = endTime - startTime; // in milli second

>> Then how am i going to time the timer??
it depend on which section of the task you want to measure.

>> Orrrrrrrrrrrrrrrrrrrrrrr, it's up to me to decide....
yes
0
 
wjh7554Author Commented:
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???
0
 
kennethxuCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wjh7554Author Commented:
kennethxu,

Thank you.
0
 
wjh7554Author Commented:
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
0
 
kennethxuCommented:
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 );
     }
}
0
 
wjh7554Author Commented:
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??

0
 
wjh7554Author Commented:
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..
0
 
kennethxuCommented:
>> 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.
0
 
wjh7554Author Commented:
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??    
0
 
kennethxuCommented:
>> 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.
0
 
kennethxuCommented:
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.
0
 
wjh7554Author Commented:
>>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...)

0
 
kennethxuCommented:
>> 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.
0
 
wjh7554Author Commented:
kennethxu, thanks a lot for your time.

I got what you mean already.

Thanks.....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now