Jump to content

Recommended Posts

Hiya.

 

I'd like to grab the load time of an external PHP document. That is, to have a script load the page contents as if it were just another user behind a browser and return in seconds how long that took. Right now I can determine that the server is up and get a ping-like response time, but that isn't much use to me.

 

I don't know where to start, or the difficulty of what I'm asking. I'd very much appreciate any pointers!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/
Share on other sites

When you say "page contents" do you mean just the page or including all of its elements (JS, CSS, images)

the reason I ask is if you just want the html then you can do something like

<?php
$start = microtime(true);
$homepage = file_get_contents('http://www.apple.com/');
echo microtime(true)-$start;
?>

which for me is 0.67515182495117 seconds

BUT if i load http://www.apple.com/ from my browser it needs to download all the images as well, this takes about 5-7 seconds.

 

of course the speed also depends on the clients PC and your host's PC

A external page time on ROIDS!

 

$url = "http://youtube.com";

$ch = curl_init($url); 
$info = curl_getinfo($ch); 
curl_close($ch);

echo 'Took ' . $info['total_time'] . ' seconds to send a request";

//Refrences///

    //* "filetime"
     //* "total_time"
    //* "namelookup_time"
    //* "connect_time"
    //* "pretransfer_time"
    //* "speed_download"
    //* "speed_upload"
    //* "starttransfer_time"
    //* "redirect_time"

When you say "page contents" do you mean just the page or including all of its elements (JS, CSS, images)

 

Ideally I'd like to get as close as possible to the actual load time, including media the page references. Perhaps I could parse the source for image tags and load them with some function...?

 

Your snippet is certainly a much better indicator than ping though - thanks. Works great. Of course I don't want to run that every time my page loads, as whatever laggy site I'm checking will slow down my own page load. Is there a better way to avoid this than running it periodically and storing the output?

 

you can using <div> element to load external page like iframe

Load external page into <DIV>

 

You lost me!

 

A external page time on ROIDS!

 

<?php
$url = $_GET['url'];
$ch = curl_init($url);
$info = curl_getinfo($ch);
curl_close($ch);
echo $url.' took '.$info['total_time'].' seconds to send a request';
?>

Seems to return 0 seconds for any URL. E.g.

I guess you could do something like this

(replace sessions with a database)

 

<?php
session_start();
$URL = 'http://www.apple.com/';

if(!isset($_SESSION[$URL]))
{
$total = 0;
$start = microtime(true);
$homepage = file_get_contents($URL);
$total += microtime(true)-$start;

preg_match_all('/src=(["\']?)([^\1]+?)\1/m', $homepage, $result, PREG_PATTERN_ORDER);
$result = $result[2];
foreach($result as $src)
{
	$start = microtime(true);
	@file_get_contents($src);
	$total += microtime(true)-$start;
}
$_SESSION[$URL] = $total;
}

echo $_SESSION[$URL];
?>

 

You could even store the scr's with times in a database, add a timestamp and update when the timestamp is older than X

For future Googlers:

 

-- 
-- Table structure for table `sources`
-- 

CREATE TABLE `sources` (
  `source_id` int(11) NOT NULL auto_increment,
  `source_url` varchar(64) NOT NULL,
  `source_loadtime` mediumint(9) NOT NULL,
  `source_last_updated` datetime NOT NULL,
  PRIMARY KEY  (`source_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

 

<?php
include 'db.php';

function getLoad($source_url,$interval=NULL) {
#time between updates in hours
if(!$interval) {
	$interval=1;
}
#check if an update is due
$sql = 
        "SELECT DATE_ADD(source_last_updated, INTERVAL $interval HOUR) as update_due FROM sources WHERE source_url = '$source_url' LIMIT 1";
$query = mysql_query($sql);
if(!$query) {
	return "Oops! (1) ".mysql_error();
}
$row = mysql_fetch_row($query);
$now = time();
$update_due = strtotime($row['0']);
if($now>$update_due) {
	#check loadtime
	$total = 0;
	$start = microtime(true);
	$page = file_get_contents($source_url);
	$total += microtime(true)-$start;
   		preg_match_all('/src=(["\']?)([^\1]+?)\1/m', $page, $result, PREG_PATTERN_ORDER);
		$result = $result[2];
   		foreach($result as $src) {
   			$start = microtime(true);
   			@file_get_contents($src);
   			$total += microtime(true)-$start;
   		}
   		#store it
   		$loadtime = $total*10000;
   		$updated = date("Y-m-d H:i:s");
   		$sql = "UPDATE sources SET source_loadtime = $loadtime, source_last_updated = '$updated' WHERE source_url = '$source_url'";
   		$query = mysql_query($sql);
	if(!$query) {
		return "Oops! (2) ".mysql_error();
	}
   		if(mysql_affected_rows()==0) {
   			#we need a new record
   			$sql = 
                        "INSERT INTO sources (source_url, source_loadtime, source_last_updated) VALUES ('$source_url', $loadtime, '$updated')";
   			$query = mysql_query($sql);
   			if(!$query) {
   				return "Oops! (3) ".mysql_error();
   			}
   		}
	$out = substr($total, 0, 6);
} else {
	#get stored loadtime
	$sql = "SELECT source_loadtime FROM sources WHERE source_url = '$source_url'";
	$query = mysql_query($sql);
	if(!$query) {
		return "Oops! (4) ".mysql_error();
	}
	$row = mysql_fetch_row($query);
	$out = $row['0']/10000;
}
return $out;
}
$url = $_GET['url'];
echo $url." loaded in <strong>".getLoad($url)."</strong> seconds";
?>

Pawn,

I would like to say thank you, for posting back your work, its nice to see people share their work instead of just taking  :thumb-up:

 

(I have only skimmed it but it looks fine)

quick notes:

1. you may want to update if to protected against SQL injection

2. In the foreach($result as $src) loop you could also add the same sort of SQL routine, (but really isn't required)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.