Skip to content

Commit

Permalink
NEW Add new execmetric debur URL parameter to print out exection time…
Browse files Browse the repository at this point in the history
… and peak memory usage
  • Loading branch information
Maxime Rainville committed Jan 17, 2019
1 parent 074cf04 commit 70a38a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions src/Control/HTTPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,8 @@ class HTTPApplication implements Application
*/
protected $kernel;

protected $showExecMetric = false;

public function __construct(Kernel $kernel)
{
$this->kernel = $kernel;
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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];
}
}

0 comments on commit 70a38a0

Please sign in to comment.