Miscellaneous Code

  Home arrow Miscellaneous Code arrow Function Watcher
MISCELLANEOUS CODE

Function Watcher
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2004-06-11

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    If you've ever tried to optimize PHP code, sometimes finding the bottlenecks can be pretty difficult without the use of a really good debugger available for PHP. The purpose of this script is to provide a method of showing where a script spends most of its time.

    By : jinxidoru

    <?
    /**
    * The best way to use this script is to place
    * it in a separate file, say: watcher.php.
    * At the top of the file you want to profile,
    * include watcher.php and follow the include
    * with the statement:
    * declare(ticks=1);
    *
    * At the end of the script, call watch_function_output()
    * to see the results. The value returned in
    * the output for each function call is the
    * number of system ticks spent in each given
    * function/method. These ticks relate to
    * the number of lower-level operations the
    * PHP engine must perform. For more information,
    * check the php.net documentation under
    * declare() or contact me:
    * Michael Bailey "jinxidoru@byu.net"
    *
    * You are welcome to use this code to any end,
    * be it for good or evil. Though, if it be for
    * evil, I'd be interested in knowing what you're
    * doing for the sake of curiousity.
    */


    // tick function for watching functions
    // if $ret_funcs is true, a tick is not registered,
    // but instead, the function list is returned.
    function watch_functions_tick($ret_funcs=false) {
    static $funcs = array();

    // do tick
    if (!$ret_funcs) {

    // get the current function from debug_backtrace
    $trace = debug_backtrace();
    $trace = $trace[1];
    $func = $trace['function'];
    if ($trace['class']) $func = $trace['class'].$trace['type'].$func;

    // as long as the given function is a valid one, add it to the array
    if ($func !== 'watch_functions_tick' && $func != 'unknown' && $func) {
    $funcs[$func]++;
    $funcs['__TICK_COUNT__']++;
    }

    // return the function array
    } else {
    return $funcs;
    }
    }

    // output the function list with the number of ticks each
    // function received as well as the percentage of the total
    // ticks it represents
    function watch_functions_output() {

    // retrieve the function list
    $funcs = watch_functions_tick(true);
    $tick_count = $funcs['__TICK_COUNT__'];
    arsort($funcs);

    // add the percentage onto each function entry
    foreach ($funcs AS $k => $v) {
    $funcs[$k] = sprintf('%d (%.2f%%)', $funcs[$k], $funcs[$k]*100/$tick_count);
    }

    // display the function list
    print "<pre>";
    print_r($funcs);
    print "</pre>";
    }

    // start the ticking
    register_tick_function('watch_functions_tick');
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap