Reputation: 2896
I am trying to create a simple load balance script and I was wondering if it is possible to find the response time of a server live? By that I mean is it possible to measure how long it takes for a server to respond after the request has been sent out?
What I am trying to do is fairly simple, I want to send a request to a link/server and do a count down, if the server took more than 5 seconds to reply, I would like to fall on the backup server.
Note that it doesnt have to be in pure php, I wouldnt mind using other languages such as javascript, C/C++, asp, but I prefer to do it in PHP. if it is possible to do the task, could you just point me to the right direction so I can read up on it.
Clarification
What I want to do is not to download a file and see how long it took, my servers have high load and it takes a while for them to respond when you click on a file to download, what I want to do is to measure the time it takes the server to respond (in this situation, its the time it takes the server to respond and allow the user to download the file), and if it takes longer than x
seconds, it should fall back on a backup server.
Upvotes: 0
Views: 300
Reputation: 95161
I think you should look at curl_getinfo it returns the following which is more than you need
url
content_type
http_code
header_size
request_size
filetime
ssl_verify_result
redirect_count
total_time <------------------ what you need
namelookup_time
connect_time
pretransfer_time
size_upload
size_download
speed_download <------- Speed
speed_upload
download_content_length
upload_content_length
starttransfer_time
redirect_time <------ Can cause increase in time
certinfo
Your Edit : if it takes longer than x seconds, it should fall back on a
Solution: All you need is set CURLOPT_CONNECTTIMEOUT
to x seconds
... It would terminate and you can move on to other task.
Upvotes: 5
Reputation: 1511
Similar to curl you can also use HttpRequest
and set the timeout
option to 5.
Upvotes: 0