diff --git a/lib/limonade.php b/lib/limonade.php index d4feb62..6a7c6d9 100644 --- a/lib/limonade.php +++ b/lib/limonade.php @@ -60,7 +60,8 @@ define('LIM_START_MICROTIME', (float)substr(microtime(), 0, 10)); define('LIM_SESSION_NAME', 'LIMONADE'.str_replace('.','x',LIMONADE)); 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_PHP', 65536); define('E_LIM_DEPRECATED', 35000); @@ -1800,21 +1801,26 @@ function end_content_for() /** * Shows current memory and execution time of the application. + * Returns only execution time if memory_get_usage() + * isn't available. + * ( That's the case before PHP5.2.1 if PHP isn't compiled with option + * --enable-memory-limit. ) * * @access public * @return array */ function benchmark() { - $current_mem_usage = memory_get_usage(); - $execution_time = microtime() - LIM_START_MICROTIME; - - return array( - 'current_memory' => $current_mem_usage, - 'start_memory' => LIM_START_MEMORY, - 'average_memory' => (LIM_START_MEMORY + $current_mem_usage) / 2, - 'execution_time' => $execution_time - ); + $res = array( 'execution_time' => (microtime() - LIM_START_MICROTIME) ); + if(defined('LIM_START_MEMORY')) + { + $current_mem_usage = memory_get_usage(); + $res['current_memory'] = $current_mem_usage; + $res['start_memory'] = LIM_START_MEMORY; + $res['average_memory'] = (LIM_START_MEMORY + $current_mem_usage) / 2; + } + + return $res; } diff --git a/tests/main.php b/tests/main.php index 4c0c196..7b1dc96 100644 --- a/tests/main.php +++ b/tests/main.php @@ -224,5 +224,18 @@ function test_main_htmlspecialchars_decode() assert_equal(limonade_htmlspecialchars_decode(''', ENT_QUOTES), '\''); assert_equal(limonade_htmlspecialchars_decode(''', 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();