-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NEW Add new execmetric debur URL parameter to print out exection time and peak memory usage #8733
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equivalent to not having a doc block at all |
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer protected over private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those 3 methods are implementations details and are not meant to be reuse. In theory, I could have implemented all of those directly in |
||
{ | ||
$bytes = memory_get_peak_usage(true); | ||
return File::format_size($bytes); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that really what this middleware does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh whoops that's a retrospective review. Explains why I didn't see all the options when submitting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️ I copied over one of the other files. Forgot to update the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#8770