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
developerWorks - FREE Tools! |
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |