PHP microtime 基准函数时间比较

发布于 2024-10-26 08:39:09 字数 807 浏览 5 评论 0原文

我目前正在使用这个函数来对一些 php 脚本进行基准测试,脚本获取执行所需的微时间,并将其写入服务器上的日志中,但我遇到的问题是我不知道什么是合适的时间。下面的脚本是我的一些时间,任何人都可以告诉我我想要在什么样的时间范围内吗?

位于页面开头

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

I am currently using this function to benchmark some php scripts, the script gets the microtime it takes to execute, and writes it to a log on the server, but the problem i am having is that i have no idea what are some decent times. the script is below followed by some of my times, can anyone give me an idea on what kind of times i want to be within the range of?

PLACE AT THE BEGINNING OF THE PAGE

global $start_time; $start_time = microtime();

PLACE AT THE END OF THE PAGE

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);

RESULTS FOR THE SAME PAGE

0.10285401344299
0.021783828735352
0.018580913543701
0.042204856872559

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

吻风 2024-11-02 08:39:09

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

这是我很久以前制作的一个基准测试课程。您可以使用静态方法在代码中的任意位置(开始、结束等)进行标记,然后使用另一个静态方法在页面底部打印出报告。还跟踪内存使用情况。这有点混乱,因为它使用静态方法。更好的办法可能是使用 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>';
        }
    }
?>

it depends on what you are doing. is a lot happening?

Here is a benchmarking class I made a long time ago. You make markers where ever in your code (start, end, etc) with a static method, then print out a report at the bottom of your page with another static method. also keeps track of memory usage. this is somewhat messy since it uses static methods. The better thing might be to profile your code using 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>';
        }
    }
?>
携余温的黄昏 2024-11-02 08:39:09

虽然我不确定你的问题是否正确,但一切都从 0.0 开始是可以的。因此,您必须更仔细地观察运行 0,1 的 ascipt,

只是为了稍微重构您的代码,使其看起来不那么古老。

//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);

Though I am not sure I got your question right, everything is starting from 0.0 is okay. So, you have to watch the ascipt running 0,1 more closely

just to refactor your code a bit to make it less ancient-looking.

//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);
追我者格杀勿论 2024-11-02 08:39:09

现在,您可以使用 microtime(TRUE); 并获取整数。不需要explode()和加法。

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

You can use microtime(TRUE); and get a whole number back, these days. No need for explode() and addition.

Regarding your times, you should be looking at fairly low execution times for a script like this (assuming this is all the script is actually doing). However, execution may take some more time depending on the I/O load on the system. Judging from the given script, and the execution time, I'm guessing the system you're trying to do this on might be doing a fair bit of I/O work and there's not much you can do to improve it from your PHP script.

只是在用心讲痛 2024-11-02 08:39:09

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

I'm not sure if I get you right, but it seems to me you basically don't know if you have performance problem yet or not, correct? If so - you don't. No need wasting time building pointless benchmarks. Focus on actual code. Come back to profiling when the app starts to feel slow for real.

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