PHP中的cpu_get_usage? [英] cpu_get_usage in php?

查看:60
本文介绍了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();

这向公共功能result()提供了一些数据,例如内存使用情况(使用memory_get_peak_usage)和时间消耗(microtime()).

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.

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

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

使用内置函数来计算在一段代码上花费了多少时间是很容易的,但是我一直在思考如何获取某一段的CPU消耗的方法时遇到麻烦代码.

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.

推荐答案

讨厌回答我自己的问题,但是我做了很多研究,在这里我去了... 基本上我所做的就是这个...

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

如您所见,我使用了 getrusage php内置函数.给出了包含一堆信息的数组,除了ru_utime.tv_usec(用户时间)和ru_stime.tv_usec(系统时间)外,大多数信息对我来说都是无用的.要查看脚本消耗了多少CPU能力,我们需要查看用户时间"和系统时间"值,然后如代码中所见,将其相加以获得实际上是cpu用法的时间.

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.

这篇关于PHP中的cpu_get_usage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆