fsockopen 普遍很慢吗?

发布于 2024-12-28 07:09:44 字数 1528 浏览 1 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2025-01-04 07:09:44

慢的不是 fsockopen - 一般而言,而是 PHP。尝试增加 fgets() 调用中的缓冲区大小,并使用正则表达式来查找标头的末尾。使用当前代码,您执行读取循环的次数过多,网络 IO 不再是您的瓶颈。

It's not fsockopen that's slow - it's PHP in general. Try increasing the buffer size in your fgets() 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.

平定天下 2025-01-04 07:09:44

使用 file_get_contents( )为此。

It would be much easier (and probably faster) to use file_get_contents() for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文