测试客户端加载网页速度
$kb = 10240;
echo "测试文件 $kb Kb...<br /><!-";
flush();
$time = explode(" ", microtime());
$start = $time[0] + $time[1];
for ($x = 0; $x < $kb; $x++) {
echo str_pad('', 1024, '.');
flush();
}
$time = explode(" ", microtime());
$finish = $time[0] + $time[1];
$deltat = $finish - $start;
$speed = round($kb / $deltat, 3);
if ($speed < 1024) {
echo "-> 测试完毕,消耗 $deltat 秒. <br />您的链接速度为 " . $speed . "Kb/s";
} else {
echo "-> 测试完毕,消耗 $deltat 秒. <br />您的链接速度为 " . $speed / 1024 . "Mb/s";
}
服务器使用ping命令测试延迟。(默认与客户端ip之间)
function global_ping($ip)
{
$out = array();
exec('ping ' . $ip . ' -n 1', $out);
echo '<br />';
$value = substr($out[2], strpos($out[2], "ms") - 4, 8) . "<br /><br />";
$str = '';
if (preg_match_all('/[0-9]/', $value, $arr)) {
foreach ($arr[0] as $ar) {
$str = $str . $ar;
}
}
if ($str == '') {
$str = '<b>time out!</b>';
}
return $str;
}
function global_getip()
{
global $ip;
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} elseif ($_SERVER['REMOTE_ADDR']) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = "Unknow";
}
if ($ip == '::1') {
$ip = '127.0.0.1';
}
return $ip;
}
echo global_ping(global_getip()) . "ms";