PHP getrusage () returns incorrect information?

I am trying to determine processor usage for my PHP scripts. I found this article , which describes in detail how to find the time of using the processor and user (section 4).

However, when I tried the examples, I got completely different results.

first example :

sleep(3); $data = getrusage(); echo "User time: ". ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo "System time: ". ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); 

Results in:

 User time: 29.53 System time: 2.71 

Example 2 :

 for($i=0;$i<10000000;$i++) { } // Same echo statements 

Results:

 User time: 16.69 System time: 2.1 

Example 3 :

 $start = microtime(true); while(microtime(true) - $start < 3) { } // Same echo statements 

Results:

 User time: 34.94 System time: 3.14 

Obviously, none of the data is correct, with the possible exception of system time in the third example. So what am I doing wrong? I would really like to be able to use this information, but it must be reliable.

I am using Ubuntu Server 8.04 LTS (32-bit) and this is the result of php -v :

 PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan 6 2010 22:01:14) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies 
+4
source share
2 answers

You can use the system time command to check this information from the outside:

 /usr/bin/time PHP .php 

which will print something like:

 0.03user 0.00system 0:03.04elapsed 0%CPU (0avgtext+0avgdata 32752maxresident)k 0inputs+0outputs (0major+2234minor)pagefaults 0swaps 

Of course, don't forget that getrusage () information is the processor time, and microtime () is the wall clock. The program can run for 10 minutes according to the clock on the wall, but internally it can only use a few seconds of processor time. They then compete for CPU time with all background programs running on the system, resource conflicts, and regular households.

There are too many factors with which you can get the exact time for such short periods. Performing three runs of the while(microtime()) version of your loop, I got the following timings:

user: 0.98, 0.09, 0.90 sys: 0.12, 0.05, 0.94

Obviously, pretty variance. Even simple <? print_r(getrusage()) ?> <? print_r(getrusage()) ?> has utime / stimes ranging from 0 to 0.03.

Try using your loops for longer periods of time and do something in them to increase processor utilization. Your numbers are too small right now to measure accurately.

+2
source

Thanks to the advice of Marc B, I was able to find out that ridiculously short times cause errors in getrusage() calculations.

I created a workaround to drop these inaccurate numbers. Here is the code:

 define('SCRIPT_START', microtime(1)); register_shutdown_function('record_activity'); /* Do work here */ function record_activity() { $data = getrusage(); $cpuu = ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); $cpus = ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); $renderedtime = round(microtime(1) - SCRIPT_START, 6); // Have some log function to put this info somewhere insert_record($renderedtime, //Only pass user CPU time if it less than or equal to rendered time ($renderedtime >= $cpuu ? $cpuu : NULL ), //Only pass system CPU time if it less than or equal to rendered time ($renderedtime >= $cpus ? $cpus : NULL )); } 

We hope that it will be useful for everyone who is experiencing the same problem.

0
source

Source: https://habr.com/ru/post/1308272/


All Articles