Skip to content
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

feat: add {memory_usage} replacement #8008

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Views/welcome_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
<footer>
<div class="environment">

<p>Page rendered in {elapsed_time} seconds</p>
<p>Page rendered in {elapsed_time} seconds using {memory_usage} MB of memory.</p>

<p>Environment: <?= ENVIRONMENT ?></p>

Expand Down
22 changes: 13 additions & 9 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
// Must be run after filters to preserve the Response headers.
$this->pageCache->make($this->request, $this->response);

// Update the performance metrics
$body = $this->response->getBody();
if ($body !== null) {
$output = $this->displayPerformanceMetrics($body);
$this->response->setBody($output);
}

// Save our current URI as the previous URI in the session
// for safer, more accurate use with `previous_url()` helper function.
$this->storePreviousURL(current_url(true));
Expand Down Expand Up @@ -780,11 +773,15 @@ protected function generateCacheName(Cache $config): string
}

/**
* Replaces the elapsed_time tag.
* Replaces the elapsed_time and memory_usage tag.
*/
public function displayPerformanceMetrics(string $output): string
{
return str_replace('{elapsed_time}', (string) $this->totalTime, $output);
return str_replace(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an after Filter at some point on the Welcome route - no business being in the main app class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought the same thing.
And probably Web Page Caching also should be a filter.

['{elapsed_time}', '{memory_usage}'],
[(string) $this->totalTime, number_format(memory_get_peak_usage() / 1024 / 1024, 3)],
$output
);
}

/**
Expand Down Expand Up @@ -1098,6 +1095,13 @@ public function spoofRequestMethod()
*/
protected function sendResponse()
{
// Update the performance metrics
$body = $this->response->getBody();
if ($body !== null) {
$output = $this->displayPerformanceMetrics($body);
$this->response->setBody($output);
}

$this->response->send();
}

Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Others
- **Autoloader:** Autoloading performance when using Composer has been improved.
Adding the ``App`` namespace in the ``autoload.psr4`` setting in **composer.json**
may also improve the performance of your app. See :ref:`autoloader-application-namespace`.
- **CodeIgniter:** Added a pseudo-variable ``{memory_usage}`` to show your memory
usage in your view files, which was supported by CodeIgniter 3.

Message Changes
***************
Expand Down