RE: [PHP] How to get cpu consumption of a php script
| From: | Tommy Pham | Date: | Mon, 17 Jan 2011 10:50:05 +0000 |
| Subject: | RE: [PHP] How to get cpu consumption of a php script | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310819@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: ash@ashleysheridan.co.uk [mailto:ash@ashleysheridan.co.uk]
> Sent: Monday, January 17, 2011 2:36 AM
> To: Tanoor Dieng; php-general@lists.php.net
> Subject: Re: [PHP] How to get cpu consumption of a php script
>
> I can't think of anything except using top or ps. If you can't guarantee what
> process runs what individual script, are you able to just track them all
> globally, and check for differences as you start and stop scripts. Perhaps run
> certain scripts under different users which should give them their own pid (i
> believe, but not tested this) that let's you track them more easily.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
> ----- Reply message -----
> From: "Tanoor Dieng" <newstanoor@gmail.com>
> Date: Mon, Jan 17, 2011 10:23
> Subject: [PHP] How to get cpu consumption of a php script
> To: <php-general@lists.php.net>
>
> Hi everybody,
> I'm a php developper for a high traffic website. Our web servers run with
> lightty?
>
> We have some issues with the cpu consumption of our servers.
>
> The problem is that, we don't know exactly which script consumes so much
> cpu.
> So, we cant to figure out how much cpu does a php script take.
>
> We have tried 3 methods:
>
> 1) Microtime: microtime at the beginning and at the end and make the diff.
> The problem with that is that it takes the execution time of the script, but in
> our script we make:
> - connection to database
> - connection to memcache
> - connection to session server
>
> All of this is not taken into account, so microtime is not that accurate
>
> 2) Shell command: we get the pid of the process executing the script and
> make a 'top' on this pid . The problem is that the php-cgi manages many
> threads so it's not accurate too.
>
> Is there other methods for dealing with this problem.
>
> Thanks,
> best regards,
>
> Tanoor.
This is untested. Within the same script, you could use [1] or similar and combine with shellexec
ps/top to get what you need. I think this would give you a better result.
Regards,
Tommy
[1] php.net/function.getmypid