Skip to content

Commit

Permalink
Fix fatal error if memory_get_usage() isn't available. Now benchmark(…
Browse files Browse the repository at this point in the history
…) only return execution time in this case.

memory_get_usage() might be unavailable before PHP5.2.1 if PHP wasn't compiled with option `--enable-memory-limit`.
Add tests to control `benchmark()` return.
  • Loading branch information
Fabrice Luraine committed May 30, 2011
1 parent 29edde0 commit ae734f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/limonade.php
Expand Up @@ -60,7 +60,8 @@
define('LIM_START_MICROTIME', (float)substr(microtime(), 0, 10)); define('LIM_START_MICROTIME', (float)substr(microtime(), 0, 10));
define('LIM_SESSION_NAME', 'LIMONADE'.str_replace('.','x',LIMONADE)); define('LIM_SESSION_NAME', 'LIMONADE'.str_replace('.','x',LIMONADE));
define('LIM_SESSION_FLASH_KEY', '_lim_flash_messages'); define('LIM_SESSION_FLASH_KEY', '_lim_flash_messages');
define('LIM_START_MEMORY', memory_get_usage()); if(function_exists('memory_get_usage'))
define('LIM_START_MEMORY', memory_get_usage());
define('E_LIM_HTTP', 32768); define('E_LIM_HTTP', 32768);
define('E_LIM_PHP', 65536); define('E_LIM_PHP', 65536);
define('E_LIM_DEPRECATED', 35000); define('E_LIM_DEPRECATED', 35000);
Expand Down Expand Up @@ -1800,21 +1801,26 @@ function end_content_for()


/** /**
* Shows current memory and execution time of the application. * Shows current memory and execution time of the application.
* Returns only execution time if <code>memory_get_usage()</code>
* isn't available.
* ( That's the case before PHP5.2.1 if PHP isn't compiled with option
* <code>--enable-memory-limit</code>. )
* *
* @access public * @access public
* @return array * @return array
*/ */
function benchmark() function benchmark()
{ {
$current_mem_usage = memory_get_usage(); $res = array( 'execution_time' => (microtime() - LIM_START_MICROTIME) );
$execution_time = microtime() - LIM_START_MICROTIME; if(defined('LIM_START_MEMORY'))

{
return array( $current_mem_usage = memory_get_usage();
'current_memory' => $current_mem_usage, $res['current_memory'] = $current_mem_usage;
'start_memory' => LIM_START_MEMORY, $res['start_memory'] = LIM_START_MEMORY;
'average_memory' => (LIM_START_MEMORY + $current_mem_usage) / 2, $res['average_memory'] = (LIM_START_MEMORY + $current_mem_usage) / 2;
'execution_time' => $execution_time }
);
return $res;
} }




Expand Down
13 changes: 13 additions & 0 deletions tests/main.php
Expand Up @@ -224,5 +224,18 @@ function test_main_htmlspecialchars_decode()
assert_equal(limonade_htmlspecialchars_decode('&#39;', ENT_QUOTES), '\''); assert_equal(limonade_htmlspecialchars_decode('&#39;', ENT_QUOTES), '\'');
assert_equal(limonade_htmlspecialchars_decode('&#039;', ENT_QUOTES), '\''); assert_equal(limonade_htmlspecialchars_decode('&#039;', ENT_QUOTES), '\'');
} }

function test_main_benchmark()
{
$bench = benchmark();
assert_true(is_array($bench));
assert_true(array_key_exists('execution_time', $bench));
if(function_exists('memory_get_usage'))
{
assert_true(defined('LIM_START_MEMORY'));
assert_true(array_key_exists('start_memory', $bench));
assert_equal(LIM_START_MEMORY, $bench['start_memory']);
}
}


end_test_case(); end_test_case();

0 comments on commit ae734f7

Please sign in to comment.