/** * Return an array of events to listen to. * * @return array */ public function implementedEvents() { $before = function ($name) { return function () use($name) { DebugTimer::start($name, __d('debug_kit', $name)); }; }; $after = function ($name) { return function () use($name) { DebugTimer::stop($name); }; }; $both = function ($name) use($before, $after) { return [['priority' => 0, 'callable' => $before('Event: ' . $name)], ['priority' => 999, 'callable' => $after('Event: ' . $name)]]; }; return ['Controller.initialize' => [['priority' => 0, 'callable' => function () { DebugMemory::record(__d('debug_kit', 'Controller initialization')); }], ['priority' => 0, 'callable' => $before('Event: Controller.initialize')], ['priority' => 999, 'callable' => $after('Event: Controller.initialize')]], 'Controller.startup' => [['priority' => 0, 'callable' => $before('Event: Controller.startup')], ['priority' => 999, 'callable' => $after('Event: Controller.startup')], ['priority' => 999, 'callable' => function () { DebugMemory::record(__d('debug_kit', 'Controller action start')); DebugTimer::start(__d('debug_kit', 'Controller action')); }]], 'Controller.beforeRender' => [['priority' => 0, 'callable' => function () { DebugTimer::stop(__d('debug_kit', 'Controller action')); }], ['priority' => 0, 'callable' => $before('Event: Controller.beforeRender')], ['priority' => 999, 'callable' => $after('Event: Controller.beforeRender')], ['priority' => 999, 'callable' => function () { DebugMemory::record(__d('debug_kit', 'View Render start')); DebugTimer::start(__d('debug_kit', 'View Render start')); }]], 'View.beforeRender' => $both('View.beforeRender'), 'View.afterRender' => $both('View.afterRender'), 'View.beforeLayout' => $both('View.beforeLayout'), 'View.afterLayout' => $both('View.afterLayout'), 'View.beforeRenderFile' => [['priority' => 0, 'callable' => function ($event, $filename) { DebugTimer::start(__d('debug_kit', 'Render {0}', $filename)); }]], 'View.afterRenderFile' => [['priority' => 0, 'callable' => function ($event, $filename) { DebugTimer::stop(__d('debug_kit', 'Render {0}', $filename)); }]], 'Controller.shutdown' => [['priority' => 0, 'callable' => $before('Event: Controller.shutdown')], ['priority' => 0, 'callable' => function () { DebugTimer::stop(__d('debug_kit', 'View Render start')); DebugMemory::record(__d('debug_kit', 'Controller shutdown')); }], ['priority' => 999, 'callable' => $after('Event: Controller.shutdown')]]]; }
/** * Wrapper around _isAuthorized to allow for timing * * @param array $user an array of user data. Can also be null * @param string $action The action to check access for * @return bool whether or not the user is authorized for access */ public function isAuthorized($user, $action) { $timerExists = Configure::read('debug') && class_exists('\\DebugKit\\DebugTimer'); if ($timerExists) { \DebugKit\DebugTimer::start(get_called_class() . '->isAuthorized()'); } $return = $this->performCheck($user, $action); if ($timerExists) { \DebugKit\DebugTimer::stop(get_called_class() . '->isAuthorized()'); } return $return; }
/** * Helper method for doing non-GET requests. * * @param string $method HTTP method. * @param string $url URL to request. * @param mixed $data The request body. * @param array $options The options to use. Contains auth, proxy etc. * * @return \Cake\Network\Http\Response */ protected function _doRequest($method, $url, $data, $options) { $request = $this->_createRequest($method, $url, $data, $options); $timerKey = 'debug_http.call.' . $url; if (Configure::read('debug')) { DebugTimer::start($timerKey, $method . ' ' . $url); } $response = $this->send($request, $options); if (Configure::read('debug')) { DebugTimer::stop($timerKey); ClientCallPanel::addCall($request, $response, DebugTimer::elapsedTime($timerKey)); } return $response; }
/** * @param $identifier * @param array $parameters * @param null $context * @throws MacroException * @return mixed */ public function runMacro($identifier, array $parameters = [], $context = null, array $options = []) { $options = Hash::merge(['validate' => false], $options); if (Plugin::loaded('DebugKit')) { DebugTimer::start(__d('macro', 'Macro: {0}', $identifier)); } $macroParts = explode('::', $identifier); $name = $macroParts[0]; $method = isset($macroParts[1]) ? $macroParts[1] : 'run'; $this->getMacroRegistry()->reset(); $config = []; /** @var Macro $macro */ try { $macro = $this->getMacroRegistry()->load($name, $config); if ($context) { $macro->context($context); } } catch (MacroException $missing) { if (!$options['validate']) { throw $missing; } return $missing; } $callable = [$macro, $method]; if (!is_callable($callable)) { $exception = new MissingMacroMethodException('Unknown method \'' . $method . '\' in macro ' . get_class($macro)); if (!$options['validate']) { throw $exception; } return $exception; } $result = call_user_func_array($callable, $parameters); $elapsedTime = null; if (Plugin::loaded('DebugKit')) { $elapsedTime = DebugTimer::elapsedTime(__d('macro', 'Macro: {0}', $identifier), 10) * 1000; DebugTimer::stop(__d('macro', 'Macro: {0}', $identifier)); } DebugMacro::record($identifier, $parameters, $context, $options, $result, $elapsedTime); if ($options['validate']) { return true; } return $result; }
/** * afterSave, stop the timer started from a save. * * @param Cake\Event\Event $event The afterSave event * @return void */ public function afterSave(Event $event) { $alias = $event->subject()->alias(); DebugTimer::stop($alias . '_save'); }
/** * {@inheritDoc} */ public function clearGroup($group) { $this->_track('delete'); DebugTimer::start('Cache.clearGroup ' . $group); $result = $this->_engine->clearGroup($group); DebugTimer::stop('Cache.clearGroup ' . $group); return $result; }
/** * Leave $profile. * * @param \Twig_Profiler_Profile $profile Profile. * * @return void */ public function leave(\Twig_Profiler_Profile $profile) { parent::leave($profile); $name = 'Twig Template: ' . substr($profile->getName(), strlen(ROOT) + 1); DebugTimer::stop($name); }
/** * test getting all the set timers. * * @return void */ public function testGetTimers() { DebugTimer::start('test1', 'this is my first test'); DebugTimer::stop('test1'); usleep(50); DebugTimer::start('test2'); DebugTimer::stop('test2'); $timers = DebugTimer::getAll(); $this->assertEquals(3, count($timers)); $this->assertTrue(is_float($timers['test1']['time'])); $this->assertTrue(isset($timers['test1']['message'])); $this->assertTrue(isset($timers['test2']['message'])); }