PHP microtime基准功能时间比较

时间:2011-03-27 07:15:52

标签: php performance benchmarking

我目前正在使用此函数来测试一些php脚本,脚本获取执行所需的microtime,并将其写入服务器上的日志,但我遇到的问题是我不知道是什么体面的时代。脚本在下面跟着我的一些时间,有人能给我一个关于我想要在什么样的时间范围内的想法吗?

在页面开始时放置

global $start_time; $start_time = microtime();

在页面末点放置

global $start_time;
$ra_start = explode(' ', $start_time);
$ra_end = explode(' ', microtime());
$cpu_time = ($ra_end[1]+$ra_end[0]) - ($ra_start[1]+$ra_start[0]);
$f = fopen('/home/mcbeav/cpu_usage.log', 'a', 1);
// time seconds request by_ip
fwrite($f, date('m-d-Y H:m')."\t".$cpu_time."\t".$_SERVER['SERVER_NAME']."\t".$_SERVER['PHP_SELF']."\t".$_SERVER['REMOTE_ADDR']."\n");
fclose($f);

相同页面的结果

0.10285401344299
0.021783828735352
0.018580913543701
0.042204856872559

4 个答案:

答案 0 :(得分:12)

这取决于你在做什么。发生了很多事?

这是我很久以前制作的基准测试课程。您可以使用静态方法在代码(开始,结束等)中创建标记,然后使用另一种静态方法在页面底部打印报告。还记录内存使用情况。这有点乱,因为它使用静态方法。更好的方法是使用XDebug配置代码:

<?php

    // time and memory benchmarking library
    class benchmark {

        // benchmark marker array
        protected static $benchmark_markers = array();
        // benchmark total duration
        protected static $total_duration = 0;

        // prevents new implimentation
        protected function __construct() {}

        // create new benchmark marker
        public static function create_benchmark_marker($marker_name) {
            $current_time = self::get_microtime();
            // get duration since last marker
            $duration = 0;
            if (self::$benchmark_markers) {
                $last_time = end(self::$benchmark_markers);
                $duration = $current_time - $last_time['end_time'];
            }
            // add to total duration
            self::$total_duration += $duration;
            // add benchmark marker to static array
            self::$benchmark_markers[] = array('name' => $marker_name, 'end_time' => $current_time, 'duration' => $duration, 'memory' => memory_get_usage());
        }

        // report benchmarking
        public static function print_report() {
            self::print_report_head();
            // output each marker line
            foreach (self::$benchmark_markers as $marker_values) {
                if ($marker_values['duration']) {
                    self::print_marker($marker_values, $last_marker_name);
                }
                $last_marker_name = $marker_values['name'];
            }
            self::print_report_foot();
        }

        // get high-precision microtime
        protected static function get_microtime() {
            return preg_replace('/^0(.+?) (.+?)$/', '$2$1', microtime());
        }

        protected static function print_report_head() {
            echo '<table style="clear: both; border-style: none; border-spacing: 1px; background-color: #ccc; font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
                <tr>
                <th style="background-color: #ddd;">Benchmark Range</th>
                <th style="background-color: #ddd;">Seconds</th>
                <th style="background-color: #ddd;">% of Total</th>
                <th style="background-color: #ddd;">Memory Usage</th>
                </tr>';
        }

        protected static function print_marker($marker_values, $last_marker_name) {
            echo '<tr>
                <td style="background-color: #eee;">' . $last_marker_name . ' -> ' . $marker_values['name'] . '</td>
                <td style="text-align: right; background-color: #eee;">' . round($marker_values['duration'], 6) . '</td>
                <td style="text-align: right; background-color: #eee;">' . round(($marker_values['duration'] / self::$total_duration) * 100, 2) . '%</td>
                <td style="text-align: right; background-color: #eee;">' . number_format($marker_values['memory']) . '</td>
                </tr>';
        }

        protected static function print_report_foot() {
            echo '<tr>
                <td style="background-color: #eee;">Total/Peak</td>
                <td style="text-align: right; background-color: #eee;">' . round(self::$total_duration, 6) . '</td>
                <td style="text-align: right; background-color: #eee;">100%</td>
                <td style="text-align: right; background-color: #eee;">' . number_format(memory_get_peak_usage()) . '</td>
                </tr>
                </table>';
        }
    }
?>

答案 1 :(得分:2)

虽然我不确定我的问题是否正确,但一切都从0.0开始是好的。所以,你必须更密切地观察ascipt运行0,1

稍微重构一下你的代码,使其不那么古老。

//at the beginning
$start_time = microtime(1);

/at the end
$cpu_time = microtime(1) - $start_time;
$data = date('m-d-Y H:m')."\t".$cpu_time."\t".$_SERVER['SERVER_NAME']."\t".$_SERVER['PHP_SELF']."\t".$_SERVER['REMOTE_ADDR']."\n");
file_put_contents('/home/mcbeav/cpu_usage.log',$data,FILE_APPEND);

答案 2 :(得分:1)

这些天你可以使用microtime(TRUE);并获得整数。不需要爆炸()和添加。

关于你的时间,你应该看到像这样的脚本相当低的执行时间(假设这是所有脚本实际上都在做)。但是,执行可能需要一些时间,具体取决于系统上的I / O负载。从给定的脚本和执行时间来判断,我猜你正在尝试执行此操作的系统可能正在进行相当多的I / O工作,并且您无法从PHP脚本中进行改进

答案 3 :(得分:1)

我不确定我是否帮助你,但在我看来你基本上不知道你是否有性能问题,对吗?如果是这样 - 你没有。无需浪费时间建立毫无意义的基准。专注于实际代码。当应用程序开始感觉真的很慢时,回到分析。