Re: Re: Profiling with register_tick_function / declare

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



McNaught, Scott wrote:
> Hi Shawn,
> 
> Thanks for the reply.  I am using php5+ only, so I'm quite sure that passing
> the object by reference like that is done automatically.
> 
> Aren't all objects are automatically passed by reference in php5, so
> therefore & is deprecated?  I tested your suggestion anyway, and it appeared
> to make no difference.
> 
> The problem is to do with the scope that the "declare" directive affects.
> 
> Cheers anyway,
> 
> Scott
> 
> -----Original Message-----
> From: Shawn McKenzie [mailto:nospam@xxxxxxxxxxxxx] 
> Sent: Friday, February 08, 2008 5:43 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  Re: Profiling with register_tick_function / declare
> 
> McNaught, Scott wrote:
>> Hi there,
>>
>>  
>>
>> Is it possible to make the declare(ticks=1) statement apply to *all*
>> functions executed in a php script, regardless of scope?
>>
>>  
>>
>> I wish to write a profiler script to basically dump all the function call
>> times through the execution of a script.  So far, I can only get it to
> work
>> for functions called via global scope.
>>
>>  
>>
>> My code calls only one function from the global scope, and then a whole
> lot
>> of functions in classes are invoked. The tick function is only invoked
> once
>> from calling this one global function.
>>
>>  
>>
>> Here is my code:
>>
>>  
>>
>> <?php
>>
>> declare(ticks = 1);
>>
>>  
>>
>> /**
>>
>>  * Profiler
>>
>>  * 
>>
>>  * @version SVN: $Id$
>>
>>  * @author Scott McNaught 
>>
>>  */
>>
>> class Profiler
>>
>> {
>>
>>       protected $m_arrProfileData = array();
>>
>>       protected $m_strCurrentFunction = null;
>>
>>       protected $m_iLastTime = null;
>>
>>  
>>
>>       /**
>>
>>        * Begins profiling
>>
>>        */
>>
>>       public function start()
>>
>>       {
>>
>>             $this->m_iLastTime = time();
>>
>>       }
>>
>>  
>>
>>       /**
>>
>>        * Finishes profiling and dumps the results
>>
>>        */
>>
>>       public function end()
>>
>>       {
>>
>>             var_dump($this->m_arrProfileData);
>>
>>             $this->m_arrProfileData = array();
>>
>>             $this->m_strCurrentFunction = null;
>>
>>       }
>>
>>  
>>
>>       public function tick()
>>
>>       {
>>
>>             $arrBacktrace = debug_backtrace();
>>
>>             
>>
>>             if (!isset($arrBacktrace[1]))
>>
>>             {
>>
>>                   return;
>>
>>             }
>>
>>             
>>
>>             $strFunction = $arrBacktrace[1]['function'];
>>
>>             
>>
>>             if (isset($arrBacktrace[1]['class']))
>>
>>             {
>>
>>                   $strFunction = $arrBacktrace[1]['class'] . '::' .
>> $strFunction;
>>
>>             }
>>
>>             
>>
>>             $iTime = microtime(true);
>>
>>             
>>
>>             if (!isset($this->m_arrProfileData[$strFunction]))
>>
>>             {
>>
>>                   $this->m_arrProfileData[$strFunction] = $iTime;
>>
>>             }
>>
>>             
>>
>>             $this->m_arrProfileData[$strFunction] += $iTime -
>> $this->m_iLastTime;
>>
>>             $this->m_iLastTime = $iTime;
>>
>>             $this->m_strCurrentFunction = $strFunction;
>>
>>       }
>>
>> }
>>
>>  
>>
>> $pProfiler = new Profiler();
>>
>> register_tick_function($pProfiler, 'tick'), true);
>>
>>  
>>
>> ?>
>>
>>  
>>
>>  
>>
>> Then from index.php.
>>
>>  
>>
>> <?php
>>
>>  
>>
>> if(isset($_GET['profile']))
>>
>> {
>>
>>       include('profiler.inc.php');
>>
>> }
>>
>>  
>>
>> function execute()
>>
>> {
>>
>>       $pClass = new SomeClass();
>>
>>       $pClass->someFunction();
>>
>> }
>>
>>  
>>
>> execute(); // TICK IS CALLED FROM EXECUTING THIS 
>>
>>  
>>
>> ?>
>>
>>  
>>
>> So a tick is made from calling execute() from global scope, and that is
> it.
>> I want it to tick when someFunction() is called.
>>
>>  
>>
>> I would really appreciate any help that anyone can give me with this as I
>> have struggled with it for a while, and there is not much documentation
>> about this anywhere. 
>>
>> This could potentially benefit a lot of people if engineered right.  I am
>> trying to make a profiler include file which will be able to be used with
>> one line of file.  
>>
>>  
>>
>> This will allow people to:
>>
>> .         Get quick profiler results on demand by adding something to the
>> query string - eg - ?profile=1
>>
>> .         Get profile results for novices without having to mess around
>> installing php binaries such as APD / zend debugger etc
>>
>> .         Profile on demand on production servers without having to
> install
>> a resource intensive debugging module
>>
>>  
>>
>> Any help greatly appreciated.
>>
>>  
>>
>> Thanks,
>>
>>  
>>
>> Scott McNaught
>>
>>  
>>
>>
> First off:
> 
> register_tick_function(array(&$pProfiler, 'tick'), true);
> 
My point was the missing 'array(' in that statement.

The code seems to work for me if $pClass->someFunction() actually does
something.  If it is empty it doesn't appear in the resulting profiler
array.  For example, here is what I have that works:

<?php

$pProfiler->start();

function execute()
{
      $pClass = new SomeClass();
      $pClass->someFunction();
}

execute(); // TICK IS CALLED FROM EXECUTING THIS

$pProfiler->end();

class someClass
{
    public function someFunction()
    {
        echo "HELLO!";
    }
}

?>

This shows:

HELLO!array(4) {
  ["include"]=>
  float(2404831569.3143)
  ["Profiler::start"]=>
  float(1202415785.3143)
  ["execute"]=>
  float(1202415784.6574)
  ["someClass::someFunction"]=>
  float(1202415784.6593)
}

But with an empty someFunction() I get:

array(3) {
  ["include"]=>
  float(2404831928.8424)
  ["Profiler::start"]=>
  float(1202415964.8424)
  ["execute"]=>
  float(1202415964.4215)
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux