o2system /
gear
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Gear; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | /** |
||
| 19 | * O2System Gear Profiler |
||
| 20 | * |
||
| 21 | * @package O2System\Gear |
||
| 22 | */ |
||
| 23 | class Profiler |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Profiler::$startTime |
||
| 27 | * |
||
| 28 | * Profiler Start Time |
||
| 29 | * |
||
| 30 | * @var float |
||
| 31 | */ |
||
| 32 | private $startTime; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Profiler::$startMemory |
||
| 36 | * |
||
| 37 | * Profiler Start Memory Usage |
||
| 38 | * |
||
| 39 | * @var float |
||
| 40 | */ |
||
| 41 | private $startMemory; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Profiler::$metrics |
||
| 45 | * |
||
| 46 | * Profiler Metrics Stack |
||
| 47 | * |
||
| 48 | * @var Profiler\Metrics |
||
| 49 | */ |
||
| 50 | private $metrics; |
||
| 51 | |||
| 52 | // ------------------------------------------------------------------------ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Profiler::__construct |
||
| 56 | * |
||
| 57 | * @return Profiler |
||
| 58 | */ |
||
| 59 | public function __construct() |
||
| 60 | { |
||
| 61 | $this->startTime = defined('STARTUP_TIME') |
||
| 62 | ? STARTUP_TIME |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 63 | : microtime(true); |
||
| 64 | |||
| 65 | $this->startMemory = defined('STARTUP_MEMORY') |
||
| 66 | ? STARTUP_MEMORY |
||
|
0 ignored issues
–
show
|
|||
| 67 | : memory_get_usage(true); |
||
| 68 | |||
| 69 | $this->metrics = new Profiler\Metrics(); |
||
| 70 | |||
| 71 | $this->watch('Starting Profiler Service'); |
||
| 72 | } |
||
| 73 | |||
| 74 | // ------------------------------------------------------------------------ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Profiler::watch |
||
| 78 | * |
||
| 79 | * @param string $marker |
||
| 80 | */ |
||
| 81 | public function watch($marker) |
||
| 82 | { |
||
| 83 | // Stop Last Benchmark |
||
| 84 | $this->metrics->push(new Profiler\DataStructures\Metric($marker)); |
||
| 85 | } |
||
| 86 | |||
| 87 | // ------------------------------------------------------------------------ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Profiler::setStartTime |
||
| 91 | * |
||
| 92 | * @param float $startTime |
||
| 93 | * |
||
| 94 | * @return Profiler |
||
| 95 | */ |
||
| 96 | public function setStartTime($startTime) |
||
| 97 | { |
||
| 98 | $this->startTime = $startTime; |
||
| 99 | |||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | // ------------------------------------------------------------------------ |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Profiler::setStartMemory |
||
| 107 | * |
||
| 108 | * @param float $startMemory |
||
| 109 | * |
||
| 110 | * @return Profiler |
||
| 111 | */ |
||
| 112 | public function setStartMemory($startMemory) |
||
| 113 | { |
||
| 114 | $this->startMemory = $startMemory; |
||
| 115 | |||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | // ------------------------------------------------------------------------ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Profiler::getMetrics |
||
| 123 | * |
||
| 124 | * @return \O2System\Gear\Profiler\Metrics |
||
| 125 | */ |
||
| 126 | public function getMetrics() |
||
| 127 | { |
||
| 128 | return $this->metrics; |
||
| 129 | } |
||
| 130 | |||
| 131 | // ------------------------------------------------------------------------ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Profiler::getTotalExecution |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | public function getTotalExecution() |
||
| 139 | { |
||
| 140 | return $this->metrics->bottom(); |
||
| 141 | } |
||
| 142 | } |