dongqiuwei8667 2011-12-07 09:51
浏览 47
已采纳

php中的cpu_get_usage?

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.

  • 写回答

3条回答 默认 最新

  • doujing8435 2011-12-11 20:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式