From fd671e72e5b53a0af8c35cc55394023f04be0d13 Mon Sep 17 00:00:00 2001 From: Tom Maiaroto Date: Wed, 4 Jan 2012 19:30:49 -0800 Subject: [PATCH] coloring some things. format changes. adding variable byte size calculation for shits and giggles. it is not super accurate, but may be of use. --- extensions/helper/li3perf.php | 109 ++++++++++++++++++++++++++++-- views/elements/variables.html.php | 16 ++++- webroot/css/li3-perf.css | 6 ++ 3 files changed, 121 insertions(+), 10 deletions(-) diff --git a/extensions/helper/li3perf.php b/extensions/helper/li3perf.php index 77c2417..0fe8643 100644 --- a/extensions/helper/li3perf.php +++ b/extensions/helper/li3perf.php @@ -3,11 +3,13 @@ class Li3perf extends \lithium\template\helper\Html { - public function printVars($array) { + public function printVars($var) { $html = "
";
 		
-		if (@is_array($array)) {
-			$html .= var_dump($array);
+		if (@is_array($var)) {
+			$html .= var_dump($var);
+		} else {
+			$html .= $var;
 		}
 		
 		$html .= "
"; @@ -15,28 +17,121 @@ public function printVars($array) { return $html; } - public function byteSize($bytes) { + /** + * Formats a size in bytes as kb, mb, or gb. + * + * @param int $bytes + * @param array $size_labels Keyed for 'k', 'm', and 'g' the labels for the sizes. + * @return type string + */ + public function byteSize($bytes, $size_labels=array()) { + $size_labels += array('k' => 'K', 'm' => 'M', 'g' => 'G'); + $size = $bytes / 1024; if($size < 1024) { $size = number_format($size, 2); - $size .= 'K'; + $size .= $size_labels['k']; } else { if($size / 1024 < 1024) { $size = number_format($size / 1024, 2); - $size .= 'M'; + $size .= $size_labels['m']; } else if ($size / 1024 / 1024 < 1024) { $size = number_format($size / 1024 / 1024, 2); - $size .= 'G'; + $size .= $size_labels['g']; } } return $size; } + /** + * Calculates the size (memory use) of a variable. + * NOTE: This is not 100% accurate by any means, but should give a good ballpark. + * It works by recording the current memory usage and then getting the memory usage + * after copying a variable. The difference between these two values is approximately the + * memory usage. There is no PHP function to do this otherwise. + * + * There's several ways people calculate the size of a variable and it all depends on if the + * variable is an array, object, string, or integer. This is the most accurate way I've seen + * when using APC as a control. APC stores variables in an optimized fashion and even a null + * value stored in APC takes up 584 bytes. So even that isn't an accurate measure...However, + * the values that this method returns are slightly higher than APC in general. + * The value is a lot lower when trying to determine the size of an integer and it should be. + * How accurate is this method or even APC's memory usage compared to PHP's? + * Likely not accurate at all...But this is the best ballpark guess that we have. + * + * Xdebug profiling may be another real good way to determine memory usage of the overall + * application, but I saw no functions that return memory size of invidual variabes. + * It may be possible with Xdebug...But this method doesn't require it to be installed. + * + * Another note, a string of 'hello' may be assumed to be 5 bytes but in PHP it can be more. + * Not only can strings be multi-byte encoded, but PHP also carries extra overhead due to + * it's non-strict type nature. PHP is not efficient at storing things in memory and that's ok. + * But let's let PHP tell us it's memory usage instead of assuming based on how much space + * something takes up in a database or an assumption of a character being equal to one byte. + * + * @param mixed $var The variable you want to know the size for + * @return int Size in bytes + */ + public function varSize($var=null) { + + $current_mem = memory_get_usage(); + $tmp_var = $this->_varCopy($var); + $variable_size = memory_get_usage() - $current_mem; + unset($tmp_var); + return $variable_size; + + } + + /** + * Used in determining the size of a variable. + * + * @see varSize() + * @param mixed $src The variable + * @return mixed A copy of the variable + */ + private function _varCopy($src=0) { + if (is_string($src)) { + return str_replace('SOME_NEVER_OCCURING_VALUE_145645645734534523', 'XYZ', $src); + } + + if (is_numeric($src)) { + return ($src + 0); + } + + if (is_bool($src)) { + return ($src?TRUE:FALSE); + } + if (is_null($src)) { + return NULL; + } + + if (is_object($src)) { + $new = (object) array(); + foreach ($src as $key => $val) { + $new->$key = $this->_varCopy($val); + } + return $new; + } + + if (!is_array($src)) { + print_r(gettype($src) . "\n"); + return $src; + } + + $new = array(); + + foreach ($src as $key => $val) { + $new[$key] = $this->_varCopy($val); + } + + return $new; + } + } ?> \ No newline at end of file diff --git a/views/elements/variables.html.php b/views/elements/variables.html.php index cabd068..b331b43 100644 --- a/views/elements/variables.html.php +++ b/views/elements/variables.html.php @@ -1,6 +1,16 @@ - +li3perf->byteSize($this->li3perf->varSize($vars['request']), array('k' => 'Kb', 'm' => 'Mb', 'g' => 'Gb')); +$variable_size = $this->li3perf->byteSize($this->li3perf->varSize($vars['view']), array('k' => 'Kb', 'm' => 'Mb', 'g' => 'Gb')); +?>

Request Params

li3perf->printVars($vars['request']); ?> -

View Template Variables []

-li3perf->printVars($vars['view']); ?> \ No newline at end of file +

View Template Variables

+

There variable occupying about in memory.

+ $v) { + echo '$' . $k . '
'; + echo $this->li3perf->printVars($v); +} +?> \ No newline at end of file diff --git a/webroot/css/li3-perf.css b/webroot/css/li3-perf.css index 4c46862..9c4e758 100644 --- a/webroot/css/li3-perf.css +++ b/webroot/css/li3-perf.css @@ -66,4 +66,10 @@ a.li3-perf-link img { /* Content */ #li3-perf-vars, #li3-perf-log, #li3-perf-timing, #li3-perf-queries { display: none; +} + +span.li3-perf-stat-value { + font-weight: bold; + font-size: 14px; + color: #FF59FF; } \ No newline at end of file