duration.class.inc.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Mesures operations duration
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class Duration
  25. {
  26. static $m_bEnabled = false;
  27. static public function Enable()
  28. {
  29. self::$m_bEnabled = true;
  30. }
  31. protected $m_fStarted = null;
  32. public function __construct()
  33. {
  34. if (!self::$m_bEnabled) return;
  35. $this->m_fStarted = MyHelpers::getmicrotime();
  36. }
  37. // Get the duration since startup, and reset the counter for the next measure
  38. //
  39. public function Scratch($sMeasure)
  40. {
  41. if (!self::$m_bEnabled) return;
  42. $fStopped = MyHelpers::getmicrotime();
  43. $fDuration = $fStopped - $this->m_fStarted;
  44. $this->Report($sMeasure.': '.round($fDuration, 3));
  45. $this->m_fStarted = MyHelpers::getmicrotime();
  46. }
  47. protected function Report($sText)
  48. {
  49. echo "DURATION... $sText<br/>\n";
  50. }
  51. }
  52. // Prototype, to be finalized later
  53. // Reports the function duration
  54. // One single thing to do: construct it
  55. class FunctionDuration
  56. {
  57. protected $m_sFunction = null;
  58. public function __construct()
  59. {
  60. $this->m_sFunction = 'my_function_name_in_call_stack';
  61. $this->m_fStarted = MyHelpers::getmicrotime();
  62. }
  63. public function __destruct()
  64. {
  65. $this->Scratch('Exiting ');
  66. }
  67. }
  68. ?>