RomânăEnglish
 
exteon.ro - Manual
Home < Products < PHP Tools < web3tracer < Manual < 3. Usage < 3.2. Using KCachegrind < 3.2.1. Recursion example
3.2.1. Recursion example

The following PHP profiler example script recurses between functions a(), b() and c(), having a usleep in each as payload (the script is also included in the examples folder):

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
    web3tracer_enable();
    
    {
    a();
    }
    
    $profile_output_name=
        $profile_output_dir.
        'callgrind.'.
        str_replace('.','_',
            str_replace('/','_',$_SERVER['PHP_SELF'])).
        '_'.
        date('Y-m-d_H-i-s');
    web3tracer_disable(
        $profile_output_name,
        WEB3TRACER_OUTPUT_CALLGRIND);
        
    function a(){
        usleep(1000);
        if(++$GLOBALS['cycle1']<5){
            $GLOBALS['cycle2']=0;
            b();
            c();
        }
    }
    function b(){
        usleep(2000);
        if(++$GLOBALS['cycle2']<3){
            b();
        }
    }
    function c(){
        usleep(3000);
        a();
    }
?>

 

Running it generates the following call graph:

 

web3tracer PHP profiler generated execution graph for a recursive script

 

We can observe here:

  • All times are expressed in nanoseconds; this is because KCachegrind doesn't play with float values, and there are many times a call can take less than one microsecond. Kudos to the XHProf team for the excellent time measuring functions.
  • The date() function which generates the trace filename takes 5 miliseconds! Yeap, for anyone who didn't know, date() is a huge time hog!
  • The b => b recursion has been decoupled into <CYCLE b> =[12x]> b, keeping the times spent in each call (3ms) intact
  • The a => c => a cycle has been decoupled (c => a call moved upwards) into <CYCLE a> =[5x]> a =[4x]> c, keeping the right times, so c is correctly credited with 16ms
  • Although the c => a => c sequence of calls is also a cycle, decoupling the c calls (a => c) is not necessary, as the c => a link has already been decoupled by the PHP profler (see above point). Therefore, no <CYCLE c> node has been generated, keeping maximum readability of the call graph.

Along with the very small overhead, the correct handling of recursion is one of the strong points of web3tracer PHP profiler.

 

You can download the trace file here and play with it in KCachegrind. (You may have to copy this and paste in a local file if your browser opens this as text). Be sure to check the next examples for a real world example of using the PHP profiler as well as another synthetic example of PHP memory usage profiling.


(c) SC EXTEON SRL 2006-2009, All Rights Reserved
Powered by   Web3 CMS