-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8733 from open-sausages/pulls/4/add-exectmetric-u…
…rl-debug-param NEW Add new execmetric debur URL parameter to print out exection time and peak memory usage
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |