Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.9

Bug #2957 zero devision when calling getAllSectionsInformations
Submitted: 2004-12-13 14:23 UTC
From: maimonoded at yahoo dot com Assigned: matthias
Status: Closed Package: Benchmark
PHP Version: 4.3.9 OS: windows
Roadmaps: (Not assigned)    
Subscription  


 [2004-12-13 14:23 UTC] maimonoded at yahoo dot com
Description: ------------ package version 1.2.1 when calling to getAllSectionsInformations after a script that runs in less then a second recives: Warning: Division by zero in \Benchmark\Profiler.php on line 173 Reproduce code: --------------- <?php require_once('Benchmark/Profiler.php'); $profiler =& new Benchmark_Profiler(false); function myFunction() { global $profiler; $profiler->enterSection('myFunction'); echo "in"; $profiler->leaveSection('myFunction'); return; } echo "out 1"; myFunction(); echo "out 2"; $info = $profiler->getAllSectionsInformations(); ?> Expected result: ---------------- out 1inout 2 Actual result: -------------- out 1inout 2 Warning: Division by zero in c:\apachefriends\xampp\php\pear\Benchmark\Profiler.php on line 173

Comments

 [2004-12-17 12:04 UTC] matthias
You forgot to strt/stop the profiling. If you do that, the error goes away. Your code should be: <?php require_once('Benchmark/Profiler.php'); $profiler =& new Benchmark_Profiler(false); function myFunction() { global $profiler; $profiler->enterSection('myFunction'); echo "in"; $profiler->leaveSection('myFunction'); return; } $profiler->start(); echo "out 1"; myFunction(); echo "out 2"; $profiler->stop(); $info = $profiler->getAllSectionsInformations(); ?> The error should be handeled more nicely, though. I'm not sure how to do it yet but I will think about it. Suggestions are appricated.
 [2005-01-14 14:17 UTC] toggg
You could also have set auto to true: $profiler =& new Benchmark_Profiler(true); Anyway, you can trap with a boolean meaning started: *** /usr/share/pear/Benchmark/Profiler.php 2005-01-07 15:45:42.000000000 +0100 --- Profiler.php 2005-01-14 14:57:27.583338856 +0100 *************** *** 138,143 **** --- 138,151 ---- var $_maxStringLength = 0; /** + * Start done + * + * @var boolean + * @access private + */ + var $_start = false; + + /** * Constructor, starts profiling recording * * @access public *************** *** 158,164 **** * @access private */ function _Benchmark_Profiler() { ! if (isset($this->auto)) { $this->stop(); $this->display(); } --- 166,172 ---- * @access private */ function _Benchmark_Profiler() { ! if ($this->auto) { $this->stop(); $this->display(); } *************** *** 212,217 **** --- 220,228 ---- * @access public */ function getAllSectionsInformations() { + if ($this->_start) { + $this->stop(); + } $informations = array(); foreach($this->_sections as $section => $time) { *************** *** 334,339 **** --- 345,354 ---- * @access public */ function enterSection($name) { + if (!$this->_start && ($name !== 'Global')) { + $this->start(); + } + $this->_start = true; if (count($this->_stack)) { if (isset($this->_callers[$name][$this->_stack[count($this->_stack) - 1]["name"]])) { $this->_callers[$name][$this->_stack[count($this->_stack) - 1]["name"]]++; *************** *** 365,370 **** --- 380,388 ---- * @access public */ function leaveSection($name) { + if ($name === 'Global') { + $this->_start = false; + } $microtime = $this->_getMicrotime(); $x = array_pop($this->_stack); It's also fixing another bug in the destructor method. By me it was allways making an extra stop ! (4.3.9) Let me know if it could be OK this way. Bye.
 [2005-02-11 13:29 UTC] matthias
I will look into it next week. I'm not sure yet if $this->start() should be called automatically. Maybe just fail silently or (probably better) throw a nice error.
 [2005-02-19 02:08 UTC] matthias
This bug has been fixed in CVS. In case this was a documentation problem, the fix will show up at the end of next Sunday (CET) on pear.php.net. In case this was a pear.php.net website problem, the change will show up on the website in short time. Thank you for the report, and for helping us make PEAR better.