Skip to content

Commit 6d97128

Browse files
authored
Merge pull request #8008 from kenjis/add-memory-usage-in-welcome
feat: add `{memory_usage}` replacement
2 parents bfeb6c5 + f515488 commit 6d97128

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

app/Views/welcome_message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
<footer>
293293
<div class="environment">
294294

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

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

system/CodeIgniter.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
535535
// Must be run after filters to preserve the Response headers.
536536
$this->pageCache->make($this->request, $this->response);
537537

538-
// Update the performance metrics
539-
$body = $this->response->getBody();
540-
if ($body !== null) {
541-
$output = $this->displayPerformanceMetrics($body);
542-
$this->response->setBody($output);
543-
}
544-
545538
// Save our current URI as the previous URI in the session
546539
// for safer, more accurate use with `previous_url()` helper function.
547540
$this->storePreviousURL(current_url(true));
@@ -780,11 +773,15 @@ protected function generateCacheName(Cache $config): string
780773
}
781774

782775
/**
783-
* Replaces the elapsed_time tag.
776+
* Replaces the elapsed_time and memory_usage tag.
784777
*/
785778
public function displayPerformanceMetrics(string $output): string
786779
{
787-
return str_replace('{elapsed_time}', (string) $this->totalTime, $output);
780+
return str_replace(
781+
['{elapsed_time}', '{memory_usage}'],
782+
[(string) $this->totalTime, number_format(memory_get_peak_usage() / 1024 / 1024, 3)],
783+
$output
784+
);
788785
}
789786

790787
/**
@@ -1098,6 +1095,13 @@ public function spoofRequestMethod()
10981095
*/
10991096
protected function sendResponse()
11001097
{
1098+
// Update the performance metrics
1099+
$body = $this->response->getBody();
1100+
if ($body !== null) {
1101+
$output = $this->displayPerformanceMetrics($body);
1102+
$this->response->setBody($output);
1103+
}
1104+
11011105
$this->response->send();
11021106
}
11031107

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Others
119119
- **Autoloader:** Autoloading performance when using Composer has been improved.
120120
Adding the ``App`` namespace in the ``autoload.psr4`` setting in **composer.json**
121121
may also improve the performance of your app. See :ref:`autoloader-application-namespace`.
122+
- **CodeIgniter:** Added a pseudo-variable ``{memory_usage}`` to show your memory
123+
usage in your view files, which was supported by CodeIgniter 3.
122124

123125
Message Changes
124126
***************

0 commit comments

Comments
 (0)