Skip to content

Commit

Permalink
Merge pull request #8008 from kenjis/add-memory-usage-in-welcome
Browse files Browse the repository at this point in the history
feat: add `{memory_usage}` replacement
  • Loading branch information
kenjis authored Oct 12, 2023
2 parents bfeb6c5 + f515488 commit 6d97128
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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(
['{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

0 comments on commit 6d97128

Please sign in to comment.