By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

calculating the response time of different sites

freddieMaize
P: 85
Hi there,

I need to hit few list of sites from my application and find the response time of those site and sort them (which ever is opening first will sit at the top). I have done the same using java (server side). Now my requirement is do in the client side (AJAX possibly). I’m having two challenges. Below is what I have done so far,

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3. function call()
  4. {   
  5.     var d = new Date();
  6.     var xmlHttp;
  7.                 try
  8.                 {  // Firefox, Opera 8.0+, Safari 
  9.                     xmlHttp=new XMLHttpRequest();
  10.                 }
  11.                 catch (e)
  12.                 {  // Internet Explorer  
  13.                     try
  14.                     {    
  15.                         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  
  16.                     }
  17.                     catch (e)
  18.                     {   
  19.                         try
  20.                         {     
  21.                             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
  22.                         }
  23.                         catch (e)
  24.                         {    
  25.                             alert("Your browser does not support AJAX!");   
  26.                             return false;    
  27.                         } 
  28.                     } 
  29.                 }
  30.                 xmlHttp.onreadystatechange=function()
  31.                 {
  32.                     if(xmlHttp.readyState==4)
  33.                         {       
  34.                             alert(xmlHttp.responseText);
  35.                            /* if(xmlHttp.responseText != undefined);
  36.                             var res_min = d.getMinutes();
  37.                             var res_sec = d.getSeconds();
  38.                             var res_time = res_min*60+res_sec;
  39.                             alert(res_time);
  40. response_time=res_time-req_time*/                      
  41.                         }
  42.                     }                        
  43.  
  44.                         var req_min = d.getMinutes();
  45.                         var req_sec = d.getSeconds();
  46.                         var req_time = req_min*60+req_sec;
  47.                         alert(req_time);
  48.  
  49.                     var url="http://localhost:8080/";
  50.                    // url=url+"?q="+q;
  51.                     xmlHttp.open("GET",url,true);
  52.                     xmlHttp.send(); 
  53.  
  54. }
  55. </script> 
Challenge 1: I’m using “var url="http://localhost:8080/";” because I’m unable to use urls of google, yahoo or bytes here. Getting “Access Denied” error. Cross domain scripting is not possible here.

So, any clues on cross domain scripting/proxy??

Challenge 2: “if(xmlHttp.readyState==4)”. Inside this function I get the responseText. Now all I want is, a way to find whether the url is responding or not (getting opened or not).
Reason: only if I know whether the site is accessible, (and if yes, how long?) I can proceed (since I’m calculating the response time here… like I have mentioned on the top).

I believe I have briefed my issue as much as possible. If it’s still not complete, kindly tell me on which part should I further explain. Thank you.

fREDDIE
Sep 18 '08 #1
Share this Question
Share on Google+
10 Replies


acoder
Expert Mod 15k+
P: 16,027
If you've already solved this with Java on the server-side, call that script instead passing the URL and the response should be the response time. From that, you can sort the list.
Sep 18 '08 #2

freddieMaize
P: 85
Hi acoder,

If you've already solved this with Java on the server-side, call that script instead passing the URL and the response should be the response time. From that, you can sort the list.
:)... The thing is, I want to finish off everthing in the client side in this regards. This must no way be a concern/workof for a java class. So no java and servlet stuffs are allowed...

The reason is, calling a java/servlet class will take time which must be avoided.
I mean, the time between the jsp->java->jsp cycle can be avoided if we use Ajax. This could be effective i believe (i mean, doing on the client side).

fREDDIE
Sep 18 '08 #3

acoder
Expert Mod 15k+
P: 16,027
I'm not saying not to use Ajax. What I suggested was that you call the JSP page via Ajax/XmlHttp.
Sep 18 '08 #4

freddieMaize
P: 85
I'm not saying not to use Ajax. What I suggested was that you call the JSP page via Ajax/XmlHttp.
Yeah. That is also one way of doing it. But at the end of the day when a jsp page gets compiled it is going to be a java/servlet class only right, which is want I wanted to avoid.
All I’m trying is to do is to finish of the whole stuff in a single jsp/html page. The control must not go to else where.
Sep 19 '08 #5

acoder
Expert Mod 15k+
P: 16,027
In that case, create a new Date object - this will default to the current date/time to the millisecond. When finished, create another one and compare the two.
Sep 19 '08 #6

freddieMaize
P: 85
Yeah the same is what I have planned to do. Something like
Expand|Select|Wrap|Line Numbers
  1. result= response time-request time.
Okay, I ll brief it out again.

I request to open a site (using, “xmlHttp.open("GET",url,true);”) and I find the request time mean while. (The challenge here in this step is, I’m unable to call url’s like http://freddiemaize.wordpress.com which is a external domain. JavaScript doesn’t support cross domain scripting right?).
Sep 20 '08 #7

acoder
Expert Mod 15k+
P: 16,027
For that you will have to use a proxy which is basically some server-side code which makes the request and passes back the response to the client. In effect, we're back to my earlier suggestion. It is possible to make cross-domain requests if you give privileges in a specific browser, but that would only work in one browser.
Sep 20 '08 #8

P: 1
Hi fREDDIE,
you mentioned that u calculated server's response time in java... cud u tell me how to do so? i'm relatively new to java n need this urgently for my college project submission...
Oct 14 '08 #9

acoder
Expert Mod 15k+
P: 16,027
Hi priyal, welcome to Bytes!

I would suggest you start a new thread for your question in the Java forum.
Oct 14 '08 #10

freddieMaize
P: 85
Hi fREDDIE,
you mentioned that u calculated server's response time in java... cud u tell me how to do so? i'm relatively new to java n need this urgently for my college project submission...
Hi Priyal,
Kindly let me know what you have done so far so as to help you out.

What I did was, I noted the system time, then established a connection to a site (say, http://google.com), got its contentType, checked if its not null, if yes then once again noted the system time, found the difference between the two system times which would apparently be the response time.

Hope this helps
Post back for any further questions

fREDDIE
Oct 15 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.