From 0529467402781af1b15797facd952dc45413ba8d Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 09:32:21 +0900 Subject: [PATCH 1/4] refactor: move logic for "Update the performance metrics" --- system/CodeIgniter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index d854dac5ec11..48cb8595aad7 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -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)); @@ -1098,6 +1091,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(); } From 12e724518692cc08f270a9b636a4610c8727b3ea Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 09:39:49 +0900 Subject: [PATCH 2/4] feat: implement {memory_usage} replacement It was in CI3. https://codeigniter.com/userguide3/libraries/benchmark.html#displaying-memory-consumption --- system/CodeIgniter.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 48cb8595aad7..daa8f26c47ba 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -773,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 + ); } /** From b4a1e524cd0ca44226d75a3e27685ae6bd92756e Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 09:41:17 +0900 Subject: [PATCH 3/4] feat: show memory usage in Welcome page --- app/Views/welcome_message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Views/welcome_message.php b/app/Views/welcome_message.php index 6d5da23f1224..396f7f44d3fe 100644 --- a/app/Views/welcome_message.php +++ b/app/Views/welcome_message.php @@ -292,7 +292,7 @@