From 70a38a045723cc98fefe9016ed5da3725d730a74 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 17 Jan 2019 16:30:00 +1300 Subject: [PATCH] NEW Add new execmetric debur URL parameter to print out exection time and peak memory usage --- .../07_Debugging/02_URL_Variable_Tools.md | 3 +- src/Control/HTTPApplication.php | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/docs/en/02_Developer_Guides/07_Debugging/02_URL_Variable_Tools.md b/docs/en/02_Developer_Guides/07_Debugging/02_URL_Variable_Tools.md index 3217c114d84..e8ae2cbcc37 100644 --- a/docs/en/02_Developer_Guides/07_Debugging/02_URL_Variable_Tools.md +++ b/docs/en/02_Developer_Guides/07_Debugging/02_URL_Variable_Tools.md @@ -28,7 +28,8 @@ session variables, used templates and much more. | isDev | | 1 | | Put the site into [development mode](../), enabling debugging messages to the browser on a live server. For security, you'll be asked to log in with an administrator log-in. Will persist for the current browser session. | | isTest | | 1 | | See above. | | debug | | 1 | | Show a collection of debugging information about the director / controller operation | - | debug_request | | 1 | | Show all steps of the request from initial [HTTPRequest](api:SilverStripe\Control\HTTPRequest) to [Controller](api:SilverStripe\Control\Controller) to Template Rendering | + | debug_request | | 1 | | Show all steps of the request from initial [HTTPRequest](api:SilverStripe\Control\HTTPRequest) to [Controller](api:SilverStripe\Control\Controller) to Template Rendering | + | execmetric | | 1 | | Dispaly the execution time and peak memory usage for the request | ## Classes and Objects diff --git a/src/Control/HTTPApplication.php b/src/Control/HTTPApplication.php index 35f453d6da4..017a6941085 100644 --- a/src/Control/HTTPApplication.php +++ b/src/Control/HTTPApplication.php @@ -5,6 +5,7 @@ use SilverStripe\Control\Middleware\HTTPMiddlewareAware; use SilverStripe\Core\Application; use SilverStripe\Core\Kernel; +use SilverStripe\Dev\Debug; /** * Invokes the HTTP application within an ErrorControlChain @@ -18,6 +19,8 @@ class HTTPApplication implements Application */ protected $kernel; + protected $showExecMetric = false; + public function __construct(Kernel $kernel) { $this->kernel = $kernel; @@ -42,6 +45,7 @@ public function getKernel() public function handle(HTTPRequest $request) { $flush = array_key_exists('flush', $request->getVars()) || ($request->getURL() === 'dev/build'); + $this->showExecMetric = Director::isDev() && array_key_exists('execmetric', $request->getVars()); // Ensure boot is invoked return $this->execute($request, function (HTTPRequest $request) { @@ -59,6 +63,8 @@ public function handle(HTTPRequest $request) */ public function execute(HTTPRequest $request, callable $callback, $flush = false) { + $start = microtime(true); + try { return $this->callMiddleware($request, function ($request) use ($callback, $flush) { // Pre-request boot @@ -69,6 +75,50 @@ public function execute(HTTPRequest $request, callable $callback, $flush = false return $ex->getResponse(); } finally { $this->getKernel()->shutdown(); + + if ($this->showExecMetric) { + $end = microtime(true); + Debug::message( + sprintf( + "Execution time: %s, Peak memory usage: %s\n", + $this->formatExecutionTime($start, $end), + $this->formatPeakMemoryUsage() + ), + false + ); + } } } + + /** + * Convert the provided start and end time to a interval in secs. + * @param float $start + * @param float $end + * @return string + */ + protected function formatExecutionTime($start, $end) + { + $diff = round($end - $start, 4); + return $diff . ' seconds'; + } + + /** + * Get the peak memory usage formatted has a string and a meaningful unit. + * @return string + */ + protected function formatPeakMemoryUsage() + { + $bytes = memory_get_peak_usage(true); + $precision = 2; + + $units = array('B', 'KB', 'MB', 'GB', 'TB'); + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + + $bytes /= pow(1024, $pow); + + return round($bytes, $precision) . ' ' . $units[$pow]; + } }