Register
It is currently Sat Oct 25, 2014 1:40 pm

Calculate http request time script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 PostPosted: Wed May 27, 2009 4:27 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
Hi
I am new for linux and bash.
I need a script which calculate the time of http site response.
For example I will give the http address (bashscripts.org) then it will try to reach the site and writes how much does it take to reach.

Thank you


Top
 Profile  
 PostPosted: Wed May 27, 2009 7:39 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
simplest solution is using unix timestamp (a.k.a. Epoch time).

Code:
#!/bin/bash
start=$(date +%s)
curl http://www.google.com
stop=$(date +%s)
let total_time=stop-start
echo $total_time


This is just to measure seconds thou. If you need it more exact i would recommend using something like PHP or Perl to sort it out.

There are lots of stuff on google how to measure page loads using php microtime() function.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu May 28, 2009 1:10 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
Hi
Thank you.
I have the latest version of ubuntu.
I need to implement it with bash script.

I get this error:
curl is currently not installed.
Also I write "sudo apt-get install curl", it says package is missing.
How can I find and install package?


Top
 Profile  
 PostPosted: Thu May 28, 2009 4:06 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Just the way it says,
"sudo apt-get install curl"

But you might want to do some changes to that script.

I believe curl posts a header that tells you alot of information like file size and such.
Might even tell you how long it took to load.

If you don't want that spam, then you'll need to do something like this with a redirector.
Code:
curl http://www.google.com 2>&1 /dev/null


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu May 28, 2009 7:08 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
I get this error:
curl is currently not installed.

How can I install curl


Top
 Profile  
 PostPosted: Thu May 28, 2009 7:54 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I'm no ubuntu expert, but I do believe that "curl" should be available via apt-get.

You just need to install it, which is done by running "sudo apt-get install curl".
sudo is to run things as root (simply explained)
apt-get is the package manager which debian and ubuntu uses.
install is just a parameter to tell apt-get what to do.
curl is the package name.

If you're having trouble installing it I would recommend that you google it or use this line: sudo apt-cache search curl
Then find it in the list of packages and use that package name instead of curl in the previous line.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Jun 02, 2009 6:56 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
Hi I managed to run the code.
I copied it in a.sh
then from terminal I wrote "sh a.sh"

Script running but I get nothing.
Just header...


Top
 Profile  
 PostPosted: Tue Jun 02, 2009 6:59 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
I get these:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.tr/">here</A>.
</BODY></HTML>
script.sh: 5: let: not found


Top
 Profile  
 PostPosted: Tue Jun 02, 2009 10:54 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
That's because you run it with sh.
The shebang at the top tells you this is a script made to run with bash. 'let' is a bash feature :).

Seems like it doesn't like the 2>&1.. you can use &> instead since i've actually tested that.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Jun 10, 2009 1:02 am   

Joined: Wed May 27, 2009 4:23 am
Posts: 6
Hi
I get 0 (zero) always as a time.
For existing and non-existing web sites.


Top
 Profile  
 PostPosted: Wed Jul 22, 2009 1:31 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Why not keep it simple? (time output is at the end. you can redirect the output if needed)

Code:
linux-ti33:~> time lynx -dump bashscripts.org                           

                              [bashscripts.jpg]
                              [1][enter.jpg]   

   #!/bin/bash
   # Passphrase encryption program
   # Reads input from text editor and encrypts to screen.
   clear                                                 
   echo "       Passphrase Encryption Program";         
   echo "--------------------------------------------------"; echo "";
   which $EDITOR &>/dev/null                                         
       if [ $? != "0" ];                                             
             then                                                     
             echo "It appears that you do not have a text editor set in
   your .bashrc file.";                                               
             echo "What editor would you like to use ? " ;             
             read EDITOR ; echo "";
       fi
   echo "Enter the name/comment for this message :"
   read comment
   $EDITOR passphraseencryption
   gpg --armor --comment "$comment" --no-options --output
   passphraseencryption.gpg --symmetric passphraseencryption
   shred -u passphraseencryption ; clear
   echo "Outputting passphrase encrypted message"; echo "" ; echo "" ;
   cat passphraseencryption.gpg ; echo "" ; echo "" ;
   shred -u passphraseencryption.gpg ;
   exit

References

   1. http://bashscripts.org/forum/

real    0m1.222s
user    0m0.004s
sys     0m0.004s


Would that work, or do you need to factor in image load times too?


Top
 Profile YIM  
 PostPosted: Wed Jul 22, 2009 3:03 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well my solution will probably not give you anything but zero's since it's in seconds. Bash isn't that particular about time.

a simple solution thou is either to use "ab" which is a load tester shipped with apache (I believe) or just write a php script which can do this for you.

If you google "load timer php" you'll find loads of examples using microtime() to check time. A simple fopen or a file_get_contents("url"); might suffice to test the load time.

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP