Skip to content

Commit

Permalink
Merge pull request #8733 from open-sausages/pulls/4/add-exectmetric-u…
Browse files Browse the repository at this point in the history
…rl-debug-param

NEW Add new execmetric debur URL parameter to print out exection time and peak memory usage
  • Loading branch information
bergice authored Jan 29, 2019
2 parents a589bcb + c4bf06f commit d2c8530
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
8 changes: 8 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ SilverStripe\Core\Manifest\VersionProvider:
Name: httpconfig-dev
Only:
environment: dev
After:
- 'requestprocessors'
---
# Set dev level to disabled with a higher forcing level
SilverStripe\Control\Middleware\HTTPCacheControlMiddleware:
defaultState: 'disabled'
defaultForcingLevel: 3

SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director:
properties:
Middlewares:
ExecMetricMiddleware: '%$SilverStripe\Control\Middleware\ExecMetricMiddleware'
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 | | Display the execution time and peak memory usage for the request |

## Classes and Objects

Expand Down
67 changes: 67 additions & 0 deletions src/Control/Middleware/ExecMetricMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace SilverStripe\Control\Middleware;

use SilverStripe\Assets\File;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Dev\Debug;

/**
* Secures requests by only allowing a whitelist of Host values
*/
class ExecMetricMiddleware implements HTTPMiddleware
{
/**
* @inheritdoc
*/
public function process(HTTPRequest $request, callable $delegate)
{
if (!$this->showMetric($request)) {
return $delegate($request);
}

$start = microtime(true);
try {
return $delegate($request);
} finally {
$end = microtime(true);
Debug::message(
sprintf(
"Execution time: %s, Peak memory usage: %s\n",
$this->formatExecutionTime($start, $end),
$this->formatPeakMemoryUsage()
),
false
);
}
}

private function showMetric(HTTPRequest $request)
{
return Director::isDev() && array_key_exists('execmetric', $request->getVars());
}

/**
* Convert the provided start and end time to a interval in secs.
* @param float $start
* @param float $end
* @return string
*/
private 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
*/
private function formatPeakMemoryUsage()
{
$bytes = memory_get_peak_usage(true);
return File::format_size($bytes);
}
}

0 comments on commit d2c8530

Please sign in to comment.