Skip to content

Commit

Permalink
Issue mozfr#593: add http headers with perf metrics
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pascalchevrel committed Feb 22, 2016
1 parent c031f10 commit 5ca094b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 25 deletions.
46 changes: 35 additions & 11 deletions app/classes/Transvision/Utils.php
Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api.php
Expand Up @@ -8,6 +8,8 @@
if (! $request->isValidRequest()) {
$json = $request->invalidAPICall();
include VIEWS . 'json.php';

return;
}

switch ($request->getService()) {
Expand Down
14 changes: 13 additions & 1 deletion app/inc/dispatcher.php
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions app/views/json.php
Expand Up @@ -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
));
);
11 changes: 0 additions & 11 deletions app/views/templates/base.php
@@ -1,8 +1,6 @@
<?php
namespace Transvision;

ob_start();

$check['repo'] = isset($check['repo']) ? $check['repo'] : 'aurora';
$source_locale = isset($source_locale) ? $source_locale : 'en-US';
$locale = isset($locale) ? $locale : 'fr';
Expand Down Expand Up @@ -64,7 +62,6 @@
} else {
$last_update = "<p>Data last updated: not available.</p>\n";
}

?>
<!doctype html>

Expand Down Expand Up @@ -153,11 +150,3 @@
?>
</body>
</html>

<?php

$content = ob_get_contents();

ob_end_clean();

print $content;
12 changes: 12 additions & 0 deletions tests/units/Transvision/Utils.php
Expand Up @@ -482,4 +482,16 @@ public function testRedirectToAPI($a, $b)
->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]);
}
}

0 comments on commit 5ca094b

Please sign in to comment.