Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix fatal error if memory_get_usage() isn't available. Now benchmark(…

…) 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...
commit ae734f79da140f0f561f9c6f39b7964d5a487a23 1 parent 29edde0
Fabrice Luraine authored
Showing with 29 additions and 10 deletions.
  1. +16 −10 lib/limonade.php
  2. +13 −0 tests/main.php
View
26 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 <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
@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;
}
View
13 tests/main.php
@@ -224,5 +224,18 @@ function test_main_htmlspecialchars_decode()
assert_equal(limonade_htmlspecialchars_decode('&#39;', 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();
Please sign in to comment.
Something went wrong with that request. Please try again.