RomânăEnglish
 
exteon.ro - Manual
Home < Products < PHP Tools < web3tracer < Manual < 3. Usage < 3.1. Profile PHP scripts
3.1. Profile PHP scripts

In order to profile PHP scripts, you must instruct the php profiler to generate a trace file. The trace file contains the tree of all the calls your program executes, and can then be further processed to show you a visual representation for your program's execution time or memory usage.

 

The WEB3TRACER_OUTPUT_PROCESSED output mode returns the processed call tree in a native PHP array variable, similar but not identical to XHProf PHP profiler's; for the output format reference view read Processed output format


The output data can then be written to files using the provided "writers", available in the lib folder. Writers for Callgrind and XHProf formats are provided.

 

To generate the trace data, you must use in your code a pair of functions: web3tracer_enable and web3tracer_disable; the first one starts, the other stops, the PHP profiler. This is very similar to the XHProf approach. These are the only 2 functions the module exposes to PHP. To write the processed data another writer function is needed, by first including the relevant writer file. So, to profile a piece of code, all that's needed is in the form:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
    web3tracer_enable();
    
    {
    a();
    }
    
    $data=web3tracer_disable(WEB3TRACER_OUTPUT_PROCESSED);
	var_dump($data);
    include_once('../lib/callgrindWriter.php');
    include_once('../lib/xhprofWriter.php');
    web3tracer_callgrindWrite($data);
    web3tracer_xhprofWrite($data);
?>

 

A more elaborate example PHP file, ready for both devel and production sampling would look like:

 

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
    /*
     * Let's define this constant that tells us we're
     * running on a development server. If this is not 
     * present we'll asume we're on production
     */
    define('APP_DEVEL_SERVER',1);
    
    /*
     * If we're on devel, we use the profile _GET parameter
     * to initialize the profiling. If we're in production
     * mode, we profile one out of 1000 requests and log it
     * to /var/profiles for later analysis. For devel, one
     * should make sure PHP folder is writable, for 
     * production, that /var/profiles exists and is
     * writable.
     */
    $start_profile=false;
    $profile_output_dir='';
    if(
        defined('APP_DEVEL_SERVER')&&
        constant('APP_DEVEL_SERVER')
    ){
        if(isset($_GET['profile'])) {
            $start_profile=true;
        }
    } else {
        $start_profile=!rand(0,999);
        $profile_output_dir='/var/profiles/';
    }
    
    if($start_profile){
        web3tracer_enable();
    }
    

    {
    
        YOUR_CODE_HERE();
    
    }
    
    /*
     * Here we stop the profile and instruct web3tracer to
     * deposit the result in callgrind format, in a file
     * of the form callgrind.index_php_2012-09-16_21-48-22
     */
    if($start_profile){
        $data=web3tracer_disable(WEB3TRACER_OUTPUT_PROCESSED);
        include_once('../lib/callgrindWriter.php');
        web3tracer_callgrindWrite($data,null,$profile_output_dir);
    }
?>

 

The above file is also included in the samples folder of the download.

 

Important notes:

  • The profile must start and end in the same function frame

Since ellaborate computations are made for recursion handling, we enforce that the web3tracer_enable and web3tracer_disable must be called in the same function, or the global code. Failing to do so will result in a WEB3TRACER_ERROR_NESTING result on web3tracer_disable; see the section on constants for more details.


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