?
Solved

"Timing" Calculation:

Posted on 2003-02-24
15
Medium Priority
?
273 Views
Last Modified: 2010-04-01
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
Comment
Question by:wjh7554
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 14

Accepted Solution

by:
kennethxu earned 400 total points
ID: 8009540
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
 

Author Comment

by:wjh7554
ID: 8012961
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8013057
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
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!

 

Author Comment

by:wjh7554
ID: 8013705
kennethxu,

Thank you.
0
 

Author Comment

by:wjh7554
ID: 8013766
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8013824
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
 

Author Comment

by:wjh7554
ID: 8013920
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
 

Author Comment

by:wjh7554
ID: 8013995
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8014054
>> 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
 

Author Comment

by:wjh7554
ID: 8014372
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8017404
>> 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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8017426
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
 

Author Comment

by:wjh7554
ID: 8021902
>>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
 
LVL 14

Expert Comment

by:kennethxu
ID: 8022157
>> 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
 

Author Comment

by:wjh7554
ID: 8022795
kennethxu, thanks a lot for your time.

I got what you mean already.

Thanks.....
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The Summer 2017 Scholarship Winners have been announced!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question