Created
October 4, 2010 18:37
-
-
Save mtrudel/610210 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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