【问题标题】:How to measure the download speed and size of a PHP document如何测量 PHP 文档的下载速度和大小
【发布时间】:2013-01-28 17:39:46
【问题描述】:

这是我的计划:我的网页是一个简单的文件共享系统。我想为用户显示下载速度。这不是100%,但它相对较好。我想写下下载的时间...例如:您的下载速度是300kb / s,您可以在7秒内下载此文件..


我有 2 个 PHP 文件。

阿尔法文件这样做:

ob_start();
require 'speedtest.php';
$sebesseg = ob_get_clean();

这很简单。我从 speedtest.php 中只得到一个数字 我的问题是: 我有一个变量:(int)$size = 1; 我想做他的:$time_left = $size / $sebesseg; $sebesseg 表示速度。以字节为单位的下载速度。但我不能使用 settype 或 (int)$sebesseg .. 或任何我已经知道的东西,因为它给我写了一个空变量.. :-( 我该如何解决这个问题?

【问题讨论】:

  • var_dump($sebesseg) 的输出是什么?
  • require 是一个穷人的函数调用。了解函数,您应该能够以更清晰的方式做到这一点。
  • 我的问题是。我在 speedtes.php 中使用 Javascript :-) 而且我不知道如何从 Javascript 中获取变量。这样可能更容易......不是吗? ..我只是不知道是什么问题...
  • 这不是您在 PHP 中从 JavaScript 获取值的方式。 PHP 在任何东西发送到客户端之前运行。 JavaScript 在那之后运行。查看AJAX/XMLHTTPRequest
  • 当然会返回一个字符串。但是 OP 假设它包含以字节为单位的下载速度,而您假设它返回速度测试的有效负载。这里有太多的假设符合我的口味;)

标签: php time-measurement ob-get-contents


【解决方案1】:

ob_get_clean() 将返回一个字符串。获取写入的字节数

$sebesseg = ob_get_clean();
$numberOfBytes = strlen($sebesseg);

阅读您的上一条评论后,我准备了一个简短的示例,如何使用 PHP 完成一个简单的下载速度测量脚本。下面的代码应该做你想做的事:

<?php
// get the start time as UNIX timestamp (in millis, as float)
$tstart = microtime(TRUE);

// start outout buffering
ob_start();

// display your page
include 'some-page.php';

// get the number of bytes in buffer
$bytesWritten = ob_get_length();

// flush the buffer
ob_end_flush();

// how long did the output take?
$time = microtime(TRUE) - $tstart;

// convert to bytes per second
$bytesPerSecond = $bytesWritten / $time;

// print the download speed
printf('<br/>You\'ve downloaded %s in %s seconds',
    humanReadable($bytesWritten), $time);
printf('<br/>Your download speed was: %s/s',
    humanReadable($bytesPerSecond));

/**
 * This function is from stackoverflow. I just changed the name
 *
 * http://stackoverflow.com/questions/2510434/php-format-bytes-to-kilobytes-megabytes-gigabytes
 */
function humanReadable($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    // Uncomment one of the following alternatives
    //$bytes /= pow(1024, $pow);
    $bytes /= (1 << (10 * $pow)); 

    return round($bytes, $precision) . ' ' . $units[$pow]; 
}

请注意,真正的下载速度只能在客户端测量。但是上面代码的结果应该差不多。

它也只会测量 HTML 页面本身的下载大小。图片。样式和 javascripts 将扩展页面加载的实际下载大小。但速度在大多数情况下应该与 HTML 文档相同。

【讨论】:

  • 它每次只给我相同的数字。这是我的 php 输出:boxy.tigyisolutions.hu/speedtest.php 是的。它给了我 1975 年... :-s
  • @user1978100 我在每次站点加载时都会得到不同的输出。你真正的计划是什么?
  • 我的网页是一个简单的文件共享系统。我想为用户显示下载速度。这不是100%,但它相对较好。我想写下下载的时间...例如:您的下载速度是300kb/s,您可以在7秒内下载此文件...
  • 我认为这会给出每毫秒的字节数。我建议使用time() 而不是microtime() 或在计算差异后将毫秒转换为秒。
  • @KamalKhan microtime(true) 会给你一个浮动:seconds.useconds。它比time() 更准确。
【解决方案2】:

使用函数stream_notification_callback()

例子:

function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {

    switch($notification_code) {
        case STREAM_NOTIFY_RESOLVE:
        case STREAM_NOTIFY_AUTH_REQUIRED:
        case STREAM_NOTIFY_COMPLETED:
        case STREAM_NOTIFY_FAILURE:
        case STREAM_NOTIFY_AUTH_RESULT:
            var_dump($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max);
            /* Ignore */
            break;

        case STREAM_NOTIFY_REDIRECTED:
            echo "Being redirected to: ", $message;
            break;

        case STREAM_NOTIFY_CONNECT:
            echo "Connected...";
            break;

        case STREAM_NOTIFY_FILE_SIZE_IS:
            echo "Got the filesize: ", $bytes_max;
            break;

        case STREAM_NOTIFY_MIME_TYPE_IS:
            echo "Found the mime-type: ", $message;
            break;

        case STREAM_NOTIFY_PROGRESS:
            echo "Made some progress, downloaded ", $bytes_transferred, " so far";
            break;
    }
    echo "\n";
}

$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));

file_get_contents("http://php.net/contact", false, $ctx);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 2014-01-15
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多