php 中的 cpu_get_usage ?

发布于 2024-12-19 17:30:20 字数 422 浏览 0 评论 0原文

我创建了一个基准类,允许用户插入例如

$timer->checkpoint('1');

检查一些代码的时间、内存消耗等......并且在代码的末尾,如果她/他想测试它,她/他必须插入

$result=$timer->result();

此函数会向公共函数 result() 提供一些数据,例如内存使用情况(使用 memory_get_peak_usage)和时间消耗(microtime())。

这一切对我来说都很好。

但是如何使用现有内置 php 函数的组合来获得一个可以视为 CPU 消耗的值

使用内置函数计算某段代码花费了多少时间非常容易,但我一直在思考如何获取某段代码的 CPU 消耗情况。

I have created a benchmark class that allows the user to insert for example

$timer->checkpoint('1');

to check out some code for time,memory consumption and such.... and at the end of the code if she/he wants to test it she/he has to insert

$result=$timer->result();

this gives out some data to public function result() like e.g. memory usage (using memory_get_peak_usage) and time consumption (microtime()).

This all works just fine for me.

But how can I use the combination of existing built-in php functions to get a value that can be considered a CPU consumption?

It has been quite easy to calculate how much time has been spent on a certain piece of code using the built-in functions, but I've been having trouble thinking of a way how to get the CPU consumption for a certain piece of code.

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

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

发布评论

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

评论(3

儭儭莪哋寶赑 2024-12-26 17:30:20

不想回答我自己的问题,但我做了很多研究,现在我开始......
基本上我所做的就是这样...

        $dat=getrusage();
        foreach ($dat as $key => $val) {
            $usertime= $dat['ru_utime.tv_usec'];
            $systemtime= $dat['ru_stime.tv_usec'];
            $finalresultcpu= ($systemtime + $usertime);
        }
        $this->_cpuTrackers[$name]=$finalresultcpu;
        return $this;
                                              }

正如你所看到的,我使用了 getrusage php 内置函数。它给出了一个包含大量信息的数组,其中大多数对我来说毫无用处,除了 ru_utime.tv_usec(用户时间)和 ru_stime.tv_usec(系统时间)。要查看脚本消耗了多少 CPU 功率,我们需要查看“用户时间”和“系统时间”值,正如您在代码中看到的那样,将其相加即可获取实际上是 CPU 使用情况的时间。

Hate to answer my own question but I did a lot of research, and here I go...
Basically what I did was this...

        $dat=getrusage();
        foreach ($dat as $key => $val) {
            $usertime= $dat['ru_utime.tv_usec'];
            $systemtime= $dat['ru_stime.tv_usec'];
            $finalresultcpu= ($systemtime + $usertime);
        }
        $this->_cpuTrackers[$name]=$finalresultcpu;
        return $this;
                                              }

As you can see I used a getrusage php built-in function. Which gives out an array with a whole bunch of info, of which most was pretty useless to me, except ru_utime.tv_usec(user time) and ru_stime.tv_usec(system time). To see how much CPU power the script has consumed, we need to look at the 'user time' and 'system time' values and as you can see in the code, add it to get the time which is in fact a cpu usage.

双马尾 2024-12-26 17:30:20
function cpu_time(){
    static $R=0;
    $r=$R;
    $R=getrusage();
    $R= $R['ru_utime.tv_sec']*1000000+$R['ru_utime.tv_usec']+
        $R['ru_stime.tv_sec']*1000000+$R['ru_stime.tv_usec'];
    return $R-$r;
}

用法:

cpu_time(); // optionally initiating, to measure time from this point,
            // not from the script's beginning
// doing some stuff here
echo cpu_time()." elapsed\n";
// doing yet some stuff here
echo cpu_time()." elapsed\n";

在简单的情况下,它给出的结果与 microtime 相同,但精度较低。
microtime 具有相同的功能:

function stopwatch(){
    static $time=0;
    $prev=$time;
    $time=microtime(true);
    return $time-$prev;
}

当然,不要像这样使用:

stopwatch();
f(); // some function, which calls `stopwatch` in itself too
echo stopwatch();
function cpu_time(){
    static $R=0;
    $r=$R;
    $R=getrusage();
    $R= $R['ru_utime.tv_sec']*1000000+$R['ru_utime.tv_usec']+
        $R['ru_stime.tv_sec']*1000000+$R['ru_stime.tv_usec'];
    return $R-$r;
}

Usage:

cpu_time(); // optionally initiating, to measure time from this point,
            // not from the script's beginning
// doing some stuff here
echo cpu_time()." elapsed\n";
// doing yet some stuff here
echo cpu_time()." elapsed\n";

In simple cases it gives the same, as microtime, but with less precision.
The same function for microtime:

function stopwatch(){
    static $time=0;
    $prev=$time;
    $time=microtime(true);
    return $time-$prev;
}

Of coarse, not to use like this:

stopwatch();
f(); // some function, which calls `stopwatch` in itself too
echo stopwatch();
守不住的情 2024-12-26 17:30:20

怎么样?

$pid = getmypid();
$cpu = exec("ps hup $pid|awk '{print $3}'");

And how about..?

$pid = getmypid();
$cpu = exec("ps hup $pid|awk '{print $3}'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文