Base for a static organization website

DebugMemory.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 2.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('Debugger', 'Utility');
  15. /**
  16. * Contains methods for Profiling memory usage.
  17. *
  18. */
  19. class DebugMemory {
  20. /**
  21. * An array of recorded memory use points.
  22. *
  23. * @var array
  24. */
  25. protected static $_points = array();
  26. /**
  27. * Get current memory usage
  28. *
  29. * @return integer number of bytes ram currently in use. 0 if memory_get_usage() is not available.
  30. */
  31. public static function getCurrent() {
  32. return memory_get_usage();
  33. }
  34. /**
  35. * Get peak memory use
  36. *
  37. * @return integer peak memory use (in bytes). Returns 0 if memory_get_peak_usage() is not available
  38. */
  39. public static function getPeak() {
  40. return memory_get_peak_usage();
  41. }
  42. /**
  43. * Stores a memory point in the internal tracker.
  44. * Takes a optional message name which can be used to identify the memory point.
  45. * If no message is supplied a debug_backtrace will be done to identify the memory point.
  46. *
  47. * @param string $message Message to identify this memory point.
  48. * @return boolean
  49. */
  50. public static function record($message = null) {
  51. $memoryUse = self::getCurrent();
  52. if (!$message) {
  53. $named = false;
  54. $trace = debug_backtrace();
  55. $message = Debugger::trimpath($trace[0]['file']) . ' line ' . $trace[0]['line'];
  56. }
  57. if (isset(self::$_points[$message])) {
  58. $originalMessage = $message;
  59. $i = 1;
  60. while (isset(self::$_points[$message])) {
  61. $i++;
  62. $message = $originalMessage . ' #' . $i;
  63. }
  64. }
  65. self::$_points[$message] = $memoryUse;
  66. return true;
  67. }
  68. /**
  69. * Get all the stored memory points
  70. *
  71. * @param boolean $clear Whether you want to clear the memory points as well. Defaults to false.
  72. * @return array Array of memory marks stored so far.
  73. */
  74. public static function getAll($clear = false) {
  75. $marks = self::$_points;
  76. if ($clear) {
  77. self::$_points = array();
  78. }
  79. return $marks;
  80. }
  81. /**
  82. * Clear out any existing memory points
  83. *
  84. * @return void
  85. */
  86. public static function clear() {
  87. self::$_points = array();
  88. }
  89. }