Calculating download times with PHP Curl
I'm using PHP Curl to measure download times for a webpage. I know this isn't a good representation of actual download times for a browser; but I'm trying to get as close as possible using this method.
My webpage has 3 files associated with it
- index.html
- my_pic.gif
- style.css
I'm doing a Curl on each of those files then adding the download times together to get the total download time.
My question is how should I do the calculations to get as close as possible to simulating a browser download?
The Curl options for measuring download times are:
CURLINFO_TOTAL_TIME
CURLINFO_NAMELOOKUP_TIME
CURLINFO_CONNECT_TIME
CURLINFO_PRETRANSFER_TIME
CURLINFO_STARTTRANSFER_TIME
Would a browser go through all of those steps for each of the 3 files above?开发者_如何转开发
For Example:
Let's say I do a CURLINFO_TOTAL_TIME on index.html.
Should I also do a CURLINFO_TOTAL_TIME on my_pic.gif and style.css? Then add those numbers to the CURLINFO_TOTAL_TIME on index.html.
OR
Should I do a CURLINFO_TOTAL_TIME minus CURLINFO_STARTTRANSFER_TIME on my_pic.gif and style.css? Then add those numbers to the CURLINFO_TOTAL_TIME on index.html.
Personally I would do this:
$t1 = microtime(true);
// do all the curl requests
$t2 = microtime(true) - $t1;
echo $t2;
Getting the exact result will be difficult, because every browser handles it slightly differently.
Name lookup is only done once per domain, so if my_pic.gif and style.css reside on the same domain lookup already happened for index.html.
Secondly, most browsers will request multiple files at once, so my_pic.gif and style.cs would be requested at the same time and only the longer download would count. Of course with bigger files on would need to account for user cable speed, but that should not be the problem here.
A good estimation should be: TOTAL_TIME(index.html) + max( TOTAL_TIME(my_pic.gif) - NAMELOOKUP_TIME(my_pic.gif), TOTAL_TIME(style.css) - NAMELOOKUP_TIME(style.css) )
If you want to test how long you really need for that download, you can get the firefox extension YSLOW. It will tell you how long the whole page took to download and draw a nice graph of which element was downloaded when and how long it took.
精彩评论