| 12345 |
anikendra |
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 |
|
|
|
15 |
App::uses('Debugger', 'Utility');
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Contains methods for Profiling memory usage.
|
|
|
19 |
*
|
|
|
20 |
*/
|
|
|
21 |
class DebugMemory {
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* An array of recorded memory use points.
|
|
|
25 |
*
|
|
|
26 |
* @var array
|
|
|
27 |
*/
|
|
|
28 |
protected static $_points = array();
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Get current memory usage
|
|
|
32 |
*
|
|
|
33 |
* @return integer number of bytes ram currently in use. 0 if memory_get_usage() is not available.
|
|
|
34 |
*/
|
|
|
35 |
public static function getCurrent() {
|
|
|
36 |
return memory_get_usage();
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Get peak memory use
|
|
|
41 |
*
|
|
|
42 |
* @return integer peak memory use (in bytes). Returns 0 if memory_get_peak_usage() is not available
|
|
|
43 |
*/
|
|
|
44 |
public static function getPeak() {
|
|
|
45 |
return memory_get_peak_usage();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Stores a memory point in the internal tracker.
|
|
|
50 |
* Takes a optional message name which can be used to identify the memory point.
|
|
|
51 |
* If no message is supplied a debug_backtrace will be done to identify the memory point.
|
|
|
52 |
*
|
|
|
53 |
* @param string $message Message to identify this memory point.
|
|
|
54 |
* @return boolean
|
|
|
55 |
*/
|
|
|
56 |
public static function record($message = null) {
|
|
|
57 |
$memoryUse = self::getCurrent();
|
|
|
58 |
if (!$message) {
|
|
|
59 |
$named = false;
|
|
|
60 |
$trace = debug_backtrace();
|
|
|
61 |
$message = Debugger::trimpath($trace[0]['file']) . ' line ' . $trace[0]['line'];
|
|
|
62 |
}
|
|
|
63 |
if (isset(self::$_points[$message])) {
|
|
|
64 |
$originalMessage = $message;
|
|
|
65 |
$i = 1;
|
|
|
66 |
while (isset(self::$_points[$message])) {
|
|
|
67 |
$i++;
|
|
|
68 |
$message = $originalMessage . ' #' . $i;
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
self::$_points[$message] = $memoryUse;
|
|
|
72 |
return true;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Get all the stored memory points
|
|
|
77 |
*
|
|
|
78 |
* @param boolean $clear Whether you want to clear the memory points as well. Defaults to false.
|
|
|
79 |
* @return array Array of memory marks stored so far.
|
|
|
80 |
*/
|
|
|
81 |
public static function getAll($clear = false) {
|
|
|
82 |
$marks = self::$_points;
|
|
|
83 |
if ($clear) {
|
|
|
84 |
self::$_points = array();
|
|
|
85 |
}
|
|
|
86 |
return $marks;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Clear out any existing memory points
|
|
|
91 |
*
|
|
|
92 |
* @return void
|
|
|
93 |
*/
|
|
|
94 |
public static function clear() {
|
|
|
95 |
self::$_points = array();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
}
|