From 5ca094bf59879a73567eaa119103413053905640 Mon Sep 17 00:00:00 2001 From: Pascal Chevrel Date: Thu, 18 Feb 2016 18:02:17 +0100 Subject: [PATCH] Issue #593: add http headers with perf metrics - New header: Transvision-perf - Does not depend on DEBUG of PERF_CHECK to be checked because we don't impact the server logs with http headers and it allows getting perf metrics on production easily --- app/classes/Transvision/Utils.php | 46 +++++++++++++++++++++++-------- app/controllers/api.php | 2 ++ app/inc/dispatcher.php | 14 +++++++++- app/views/json.php | 4 +-- app/views/templates/base.php | 11 -------- tests/units/Transvision/Utils.php | 12 ++++++++ 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/app/classes/Transvision/Utils.php b/app/classes/Transvision/Utils.php index edfcb6a6..7ea5f811 100644 --- a/app/classes/Transvision/Utils.php +++ b/app/classes/Transvision/Utils.php @@ -340,26 +340,50 @@ public static function getOrSet($arr, $value, $fallback) } /** - * Utility function to log the memory used by a script - * and the time needed to generate the page + * Utility function to return the memory used by a script + * and the time needed to compute the data. + * + * @return array [Memory peak in bytes, Memory peak in MB, Computation time] + */ + public static function getScriptPerformances() + { + $memory_peak_B = memory_get_peak_usage(true); + $memory_peak_MB = round(($memory_peak_B / (1024 * 1024)), 2); + $computation_time = round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 4); + + return [$memory_peak_B, $memory_peak_MB, $computation_time]; + } + + /** + * Utility function to log to stderr the memory used by a script + * and the time needed to generate the page. + * This is used only when the constants DEBUG and PERF_CHECK are set to True + * because we don't want to fill our logs with debug data on production. * * @return void */ public static function logScriptPerformances() { + list($memory_peak_B, $memory_peak_MB, $computation_time) = self::getScriptPerformances(); + if (DEBUG && PERF_CHECK) { - $memory = 'Memory peak: ' - . memory_get_peak_usage(true) - . ' (' - . round((memory_get_peak_usage(true) / (1024 * 1024)), 2) - . 'MB)'; - $render_time = 'Elapsed time (s): ' - . round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 4); - error_log($memory); - error_log($render_time); + error_log("Memory peak: {$memory_peak_B} ({$memory_peak_MB}MB)"); + error_log("Elapsed time (s): {$computation_time}"); } } + /** + * Utility function to log the memory used by a script + * and the time needed to generate the page as an HTTP header. + * + * @return void + */ + public static function addPerformancesHTTPHeader() + { + list($memory_peak_B, $memory_peak_MB, $computation_time) = self::getScriptPerformances(); + header("Transvision-perf: Memory: {$memory_peak_B} ({$memory_peak_MB}MB); Time: {$computation_time}s"); + } + /** * Generate a red to green color from a numeric value * diff --git a/app/controllers/api.php b/app/controllers/api.php index 0102e81c..e7e0bb48 100644 --- a/app/controllers/api.php +++ b/app/controllers/api.php @@ -8,6 +8,8 @@ if (! $request->isValidRequest()) { $json = $request->invalidAPICall(); include VIEWS . 'json.php'; + + return; } switch ($request->getService()) { diff --git a/app/inc/dispatcher.php b/app/inc/dispatcher.php index 2369a123..f623e750 100644 --- a/app/inc/dispatcher.php +++ b/app/inc/dispatcher.php @@ -115,15 +115,27 @@ $content = ob_get_contents(); ob_end_clean(); + ob_start(); // display the page require_once VIEWS . 'templates/base.php'; + $content = ob_get_contents(); + ob_end_clean(); } else { + ob_start(); if (isset($view)) { include VIEWS . $view . '.php'; } else { include CONTROLLERS . $controller . '.php'; } + $content = ob_get_contents(); + ob_end_clean(); } - +ob_start(); +// Log script performance in the HTTP headers sent to the browser +Utils::addPerformancesHTTPHeader(); +$perf_header = ob_get_contents(); // Log script performance in PHP integrated developement server console Utils::logScriptPerformances(); +ob_end_clean(); +print $perf_header . $content; +die; diff --git a/app/views/json.php b/app/views/json.php index ff337410..2142bea0 100644 --- a/app/views/json.php +++ b/app/views/json.php @@ -10,7 +10,7 @@ // We die here because we never want to send anything more after the JSON file $json_data = new Json; -die($json_data->outputContent( +print $json_data->outputContent( $json, isset($_GET['callback']) ? $_GET['callback'] : false -)); +); diff --git a/app/views/templates/base.php b/app/views/templates/base.php index c756081f..d83b0260 100644 --- a/app/views/templates/base.php +++ b/app/views/templates/base.php @@ -1,8 +1,6 @@ Data last updated: not available.

\n"; } - ?> @@ -153,11 +150,3 @@ ?> - -string($obj->redirectToAPI()) ->isEqualTo($b); } + + public function testGetScriptPerformances() + { + $obj = new _Utils(); + $data = $obj->getScriptPerformances(); + $this + ->array($data) + ->size->isEqualTo(3) + ->integer($data[0]) + ->float($data[1]) + ->float($data[2]); + } }