BashScripts.org
http://bashscripts.org/forum/

Calculate http request time script
http://bashscripts.org/forum/viewtopic.php?f=8&t=806
Page 1 of 1

Author:  couper [ Wed May 27, 2009 4:27 am ]
Post subject:  Calculate http request time script

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

Author:  fredrik.eriksson [ Wed May 27, 2009 7:39 am ]
Post subject:  Re: Calculate http request time script

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

Author:  couper [ Thu May 28, 2009 1:10 am ]
Post subject:  Re: Calculate http request time script

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?

Author:  fredrik.eriksson [ Thu May 28, 2009 4:06 am ]
Post subject:  Re: Calculate http request time script

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

Author:  couper [ Thu May 28, 2009 7:08 am ]
Post subject:  Re: Calculate http request time script

I get this error:
curl is currently not installed.

How can I install curl

Author:  fredrik.eriksson [ Thu May 28, 2009 7:54 am ]
Post subject:  Re: Calculate http request time script

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

Author:  couper [ Tue Jun 02, 2009 6:56 am ]
Post subject:  Re: Calculate http request time script

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...

Author:  couper [ Tue Jun 02, 2009 6:59 am ]
Post subject:  Re: Calculate http request time script

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

Author:  fredrik.eriksson [ Tue Jun 02, 2009 10:54 am ]
Post subject:  Re: Calculate http request time script

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

Author:  couper [ Wed Jun 10, 2009 1:02 am ]
Post subject:  Re: Calculate http request time script

Hi
I get 0 (zero) always as a time.
For existing and non-existing web sites.

Author:  jeo [ Wed Jul 22, 2009 1:31 pm ]
Post subject:  Re: Calculate http request time script

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?

Author:  fredrik.eriksson [ Wed Jul 22, 2009 3:03 pm ]
Post subject:  Re: Calculate http request time script

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

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com