Is there a way to determine how long it takes a web page, and all it's content, to load with PHP?
I have already tried this:
$time_start = microtime(true);
(All the content of the web page here)
$time_end = microtime(true);
$time = $time_end - $time_start;
echo $time;
However, the problem with this (as far as I can tell) is that I'm only calculating the time it takes the php script to execute. This doesn't factor in any images or videos that are on the page.
Is there a way to determine how long it takes a web page to load including images or videos using php?
Basically what I'm trying to do is test the speed of my server with out factoring in my connection speed.
You need to download firebug and then open the "net" tab and wait for the page to finish loading and it will show you the total load time of all requests.
You might consider ab (ApacheBench). It's for testing the performance of your web server, but you can run it against a particular URL if you're just concerned about one page. One advantage is that it can run from the command line and issue multiple requests in parallel, enabling you to do some kind of load testing.
If you want to factor in how long it takes to actually load on the browser, you'll need some kind of javascript solution. One approach with code is presented in the article, Optimizing Page Load Time, which is worth reading.
Google Chrome has an amazing Audit tool built in which gives a good list of ways to improve a given site as far as speed goes.
alt text http://far.id.au/audit.png
try this
// top of the page --
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
?>
// end of the page --
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in '.$total_time.' seconds.';
?>
You can either use the NavigationTiming API (https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming2/Overview.html) and beacon the timings back - this is how boomerang works (https://github.com/lognormal/boomerang)
Alternatively use a synthetic test tool like webpagetest.org to gather timings from various browsers/locations/network variations
Related
I have a website developed with PHP from scratch, and I want to know the load time of my php script (my page) in localhost.
is there any method or tools to check that ? I try to use Performance on Google Chrome, but it's very hard to understand, and with some search on Google I found the firebug plugins of firefox, but it's deleted.
is there any solution ?
I you want the mesure the loading time browser-side, as Madhawa commented, you can view it in the network tab of chrome's developer tools :
If you want the exact execution time of your script, it should be done server-side by checking the time before and after the script. Daniels link should point you to the right direction.
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
echo $start;
?>
Try this
I had a site working nicely in PHP. Then an acquaintance who works at Google got in my ear about how AJAX is the thing. So now I've rejigged the site to be in just HTML & javascript on the client-side making AJAX JSON calls to PHP scripts calling MySQL on the server side. Good caching, no page reloads. It was a good idea.
My question now, however, is this: How much faster might the site run if the server side was in a compiled language (say, C or C++) rather than PHP? (My original prototype was in C - in the terminal!) The PHP scripts are all basic security processing and then database calls to return JSON. How can you benchmark relative speeds? Just using firebug POST data in the console? Or is there a better way?
Thanks :)
if it is that simple, php interpretation will be a tiny fraction of the execution time, most of which will be taken by the connection to the db server. You can always look at Facebook's HipHop - http://developers.facebook.com/blog/post/358/ though
For benchmarking, something like
// first line of your code
$start_time = microtime( $get_as_float = TRUE );
// last line of your code
$time_end = microtime( $get_as_float = TRUE );
$execution_time = $time_end - $start_time;
echo '<!-- executed in: ', $execution_time , ' seconds -->';
will go a long way
I wouldn't use the times in Firebug as those depend on your connection speed/quality etc
I made a website recent and it's loading very slow. My Firebug Page Speed Score is 82/100. I think this is good. My website has 2 images and they have 100KB and some other small images for bullets, arrows and stuff that have not more that 50KB.
Anyway, my point is that the html is quite fast, but I have this html as a Wordpress theme and a new host (cheap one).
My question: How can I find the time for my Wordpress (or any PHP script) to echo out the page requested.
Can I know for sure if the hosting or the script that is making my page work slow?
Thank you!
time your script?
<?php
$start = microtime(true);
//your script here
$end = microtime(true);
$time = $end - $start;
echo('script took ' . $time . ' seconds to execute.');
?>
I use Fiddler for this. It is a handy tool that can accurately time the entire request and response, to and from the server.
Just open Fiddler, go to your site, select all of the requests related, and click the statistics tab.
I am looking for a possibility to check the user connection speed. It is supposed to be saved as a cookie and javascript files as well as css files will be adapted if the speed is slow.
The possibility for testing speed i have at the moment ist the following
$kb = 512;
flush();
//
echo "<!-";
$time = explode(" ",microtime());
for($x=0;$x<$kb;$x++){
echo str_pad('', 512, '.');
flush();
}
$time_end = explode(" ",microtime());
echo "->";
$start = $time[0] + $time[1];
$finish = $time_end[0] + $time_end[1];
$deltat = $finish - $start;
return round($kb / $deltat, 3);
While it works, I do not like it to put so many characters into my code also if I echo all this I can not save the result in a cookie because there has already been an output.
Could one do something like this in a different file wor something? Do you have any solution?
Thanks in advance.
Do you have any solution?
My solution is to not bother with the speed test at all. Here's why:
You stated that the reason for the test is to determine which JS/CSS files to send. You have to keep in mind that browsers will cache these files after the first download (so long as they haven't been modified). So in effect, you are sending 256K of test data to determine if you should send, say, an additional 512K?
Just send the data and it will be cached. Unless you have MBs of JS/CSS (in which case you need a site redesign, not a speed test) the download time will be doable. Speed tests should be reserved for things such as streaming video and the like.
The only idea what i can come up is a redirect.
Measure users' speed
Redirect to index
While this isn't a nice solution it only need to measure users' speed only once so i think it's excusable.
How about using javascript to time how long it takes to load a page. Then use javascript to set the cookie.
microtime in javascript http://phpjs.org/functions/microtime:472
Using jQuery
<head>
<!-- include jquery & other html snipped -->
<script>
function microtime (get_as_float) {
// http://kevin.vanzonneveld.net
// + original by: Paulo Freitas
// * example 1: timeStamp = microtime(true);
// * results 1: timeStamp > 1000000000 && timeStamp < 2000000000
var now = new Date().getTime() / 1000;
var s = parseInt(now, 10);
return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
function setCookie(c_name, value, expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
start = microtime(true);
$(window).load(function () {
// everything finished loading
end = microtime(true);
diff = end - start;
// save in a cookie for the next 30 days
setCookie('my_speed_test_cookie', diff, 30);
});
</script>
</head>
<body>
<p>some page to test how long it loads</p>
<img src="some_image_file.png">
</body>
Some pitfalls:
- The page would need to start loading first. JQuery would need to be loaded (or you can rework the above code to avoid jQuery)
testing speed on ASCII / Latin data may not give the best result, because the characters may get compressed. Besides the high level gzip compression, Some modems / lines (if not all) have basic compression that is able to detect repeating characters and tell the other end that the next 500 are repeat of ' '. I guess it would be best to use binary data that has been compressed
The problem here is that you can't really solve this nicely, and probably not in pure PHP. The approach you've taken will make the user download (512x512) = 262 144 bytes of useless data, which is much bigger than most complete pages. If the user is on a slow connection, they may assume your site is down before the speed test is over (with 10 kB/sec, it'd take half a minute before anything interesting shows up on screen!).
You could make an AJAX request for a file of a known size and time how long that takes. The problem here is that the page needs to be already loaded for that to work, so it'd only work for subsequent pages.
You could make a "loading" page (like you see on GMail when accessing it from a slow connection) that preloads the data, with a link to the low-bandwidth version (or maybe a redirect if the loading is taking too long).
Or you could save the "start" time in the cookie and make an AJAX request when the page is done loading - that would give you the actual loading time of your page; if that's, say, over 10 seconds, you may want to switch to the low-bandwidth version.
None of these, however, will get you the speed on the very first access; and sending a big empty page up front is not a very good first impression either.
you visit the first page(maybe 100kB with all external files), a session is immeadeatly started with
$_SESSION["start_time"] = time();
when page finished loading(jQuery window load or smth:) u send a request again with time,
u compute the speed (jQueryRequestTime - $_SESSION["start_time"] / PageSize) and set another session variable, the next link he clicks then can include custom css/js approved for that
ofc this is not perfect:)
After you've determined the user's speed, send javascript to the browser to set the cookie and then do a refresh or redirect in cases where the speed is below what you'd like.
The only thing I can think of would be to subscribe to a service which offers an IP to net speed lookup. These services work by building a database of IP addresses and cataloging their registered intended use. They're not always accurate, but they do provide a starting point. Look up the user's IP address against one of these and see what it returns.
Ip2Location.com provides such a database, beginning with their DB13 product.
Of course, if your goal is a mobile version of the site, user agent sniffing is a better solution.
Are there any tools available to log the page load time for a php site?
Mainly looking for something that I can see trends of load times over time, I was considering dumping them into a file using error_log(), but I don't know what I could use to parse it and display graphs
You can record the microtime at the start of execution, hold that variable until the end, check the time, subtract them, and there you have your execution time. Output buffering will be required to make this work in most cases, unless it's a situation in which a particular thing always runs last (like footer()).
$time_start = microtime_float();
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//at the start.
//at the end:
$time_end = microtime_float();
$time = round($time_end - $time_start, 4);
echo "Last uncached content render took $time seconds";
Use the Firebug extension for Firefox, it has a Net panel that shows you load times.
If you want to do load testing, apache comes with a utility called apache bench, try ab --help in a console window near you.
See PEAR Benchmark. It allows you to add benchmarks into your code. You can have it dump an HTML table on your pages, or you can loop through the data and write to a log file.