fsockopen 普遍很慢吗?
我正在尝试用 PHP 执行下游带宽速度测试。我不知道为什么 wget 会以 400 Mbps 的速度下载 1 MB 的数据,而 fsockopen 会以 170 Mbps 的速度下载。我正在使用 fsockopen,因为所有服务器都支持它。
有谁知道为什么吗? 400 Mbps 与 170 Mbps 的差异相当大。
是否有更好的性能选择?
<?php
$url = 'http://ftp.sunet.se/pub/Linux/distributions/ubuntu/ubuntu/dists/hardy-backports/Contents-i386.gz';
$size = 1024*1024; // Get amount of bytes
$parts = parse_url($url);
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
if (!$fp) throw new Exception("Problem with $url, $errstr");
$out = "GET " . $parts['path'] ." HTTP/1.1\r\n"
. "Host: ". $parts['host'] ."\r\n"
. "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$found_body = false;
$response = '';
$start = microtime(true);
$timeout = 30;
while (!feof($fp)) {
if ((microtime(true) - $start) > $timeout) break;
if (strlen($response) > $size) break;
//$row = fgets($fp, 128); // Grab block of 128
$row = fgets($fp);
if ($found_body) {
$response .= $row;
} else if ($row == "\r\n") {
$found_body = true;
$tsStart = microtime(true);
continue;
}
}
$time_elapsed = microtime(true) - $tsStart;
$speed = strlen($response) / $time_elapsed * 8 / 1000000;
echo round($speed, 2) . ' Mbps ('. strlen($response) .' bytes in '. round($time_elapsed, 3) .' s)<br />';
?>
I am trying to perform a downstream bandwidth speed test in PHP. I dunno why wget would download 1 Mbyte data in 400 Mbps while fsockopen does it in 170 Mbps. I am using fsockopen since it is supported on all servers.
Does any one know why? 400 Mbps vs. 170 Mbps is quite a big difference.
Is there a better option for performance?
<?php
$url = 'http://ftp.sunet.se/pub/Linux/distributions/ubuntu/ubuntu/dists/hardy-backports/Contents-i386.gz';
$size = 1024*1024; // Get amount of bytes
$parts = parse_url($url);
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
if (!$fp) throw new Exception("Problem with $url, $errstr");
$out = "GET " . $parts['path'] ." HTTP/1.1\r\n"
. "Host: ". $parts['host'] ."\r\n"
. "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$found_body = false;
$response = '';
$start = microtime(true);
$timeout = 30;
while (!feof($fp)) {
if ((microtime(true) - $start) > $timeout) break;
if (strlen($response) > $size) break;
//$row = fgets($fp, 128); // Grab block of 128
$row = fgets($fp);
if ($found_body) {
$response .= $row;
} else if ($row == "\r\n") {
$found_body = true;
$tsStart = microtime(true);
continue;
}
}
$time_elapsed = microtime(true) - $tsStart;
$speed = strlen($response) / $time_elapsed * 8 / 1000000;
echo round($speed, 2) . ' Mbps ('. strlen($response) .' bytes in '. round($time_elapsed, 3) .' s)<br />';
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
慢的不是
fsockopen
- 一般而言,而是 PHP。尝试增加fgets()
调用中的缓冲区大小,并使用正则表达式来查找标头的末尾。使用当前代码,您执行读取循环的次数过多,网络 IO 不再是您的瓶颈。It's not
fsockopen
that's slow - it's PHP in general. Try increasing the buffer size in yourfgets()
calls and use a regular expression to find the end of the headers. With your current code, you're executing your read loop too many times and network IO is no longer your bottleneck.使用 file_get_contents( )为此。
It would be much easier (and probably faster) to use file_get_contents() for this.