Skip to content

Instantly share code, notes, and snippets.

@mtrudel
Created October 4, 2010 18:37
Show Gist options
  • Select an option

  • Save mtrudel/610210 to your computer and use it in GitHub Desktop.

Select an option

Save mtrudel/610210 to your computer and use it in GitHub Desktop.
class FrameProfiler {
// The root frame is the top-most frame in this trace. It is an error to try and pop it.
private $frameStack;
function FrameProfiler() {
$this->frameStack = array();
$this->enterSection("root");
}
function enterSection($name) {
$frame = array("name" => $name, "startTime" => microtime(true), "children" => array());
if ($currentFrame =& array_pop($this->frameStack)) {
array_push($currentFrame["children"], &$frame);
array_push($this->frameStack, &$currentFrame);
}
array_push($this->frameStack, &$frame);
}
function leaveSection($name) {
if (count($this->frameStack) == 1) die("Can't pop the top section of the profiling stack!");
$currentFrame =& end($this->frameStack);
if ($name !== $currentFrame["name"]) die("Profiling error! Trying to leave: " . $name . ", but actually in: " . $currentFrame["name"]);
$val =& array_pop($this->frameStack);
$val["endTime"] = microtime(true);
}
function dumpFrames() {
if (count($this->frameStack) != 1) die ("Can't dump profiling while it's in progress!");
$root = $this->frameStack[0];
$root['endTime'] = microtime(true);
return $root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment