Skip to content

Commit bb5ba7b

Browse files
committedDec 18, 2021
Update, try to catch memory-limit strange behaviour on some hostings
- add debug mode - output some debug info about memory processing - increase buffers-flusing line for apache/fpm/nginx
1 parent 3b63a14 commit bb5ba7b

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed
 

‎bench.php

+60-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Author : Sergey Dryabzhinsky #
1111
# Company : Rusoft Ltd, Russia #
1212
# Date : Dec 05, 2021 #
13-
# Version : 1.0.44 #
13+
# Version : 1.0.45-dev #
1414
# License : Creative Commons CC-BY license #
1515
# Website : https://github.com/rusoft/php-simple-benchmark-script #
1616
# Website : https://git.rusoft.ru/open-source/php-simple-benchmark-script #
@@ -32,10 +32,10 @@ function print_pre($msg) {
3232
flush();
3333
}
3434

35-
$scriptVersion = '1.0.44';
35+
$scriptVersion = '1.0.45-dev';
3636

3737
// Special string to flush buffers, nginx for example
38-
$flushStr = '<!-- '.str_repeat(" ", 4096).' -->';
38+
$flushStr = '<!-- '.str_repeat(" ", 8192).' -->';
3939

4040
if (php_sapi_name() != 'cli') {
4141
// Hello, nginx!
@@ -121,6 +121,8 @@ function print_pre($msg) {
121121
/* Default PHP memory limit in Mb */
122122
$defaultMemoryLimit = 256;
123123

124+
$debugMode = 0;
125+
124126
$recalculateLimits = 1;
125127

126128
$printDumbTest = 0;
@@ -140,6 +142,13 @@ function print_pre($msg) {
140142
$defaultTimeLimit = $t;
141143
}
142144

145+
if ($x = (int)getenv('PHP_DEBUG_MODE')) {
146+
$debugMode = $x;
147+
}
148+
if (isset($_GET['debug_mode']) && $x = (int)$_GET['debug_mode']) {
149+
$debugMode = $x;
150+
}
151+
143152
if ($m = (int)getenv('PHP_MEMORY_LIMIT')) {
144153
$defaultMemoryLimit = $m;
145154
}
@@ -189,8 +198,10 @@ function print_pre($msg) {
189198
$selectedTests = explode(',', $_GET['run_tests']);
190199
}
191200

201+
192202
// http://php.net/manual/ru/function.getopt.php example #2
193203
$shortopts = "h";
204+
$shortopts .= "x";
194205
$shortopts .= "d";
195206
$shortopts .= "D";
196207
$shortopts .= "L";
@@ -202,6 +213,7 @@ function print_pre($msg) {
202213

203214
$longopts = array(
204215
"help",
216+
"debug",
205217
"dont-recalc",
206218
"dumb-test-print",
207219
"list-tests",
@@ -238,6 +250,7 @@ function print_pre($msg) {
238250
. 'Usage: ' . basename(__FILE__) . ' [-h|--help] [-d|--dont-recalc] [-D|--dumb-test-print] [-L|--list-tests] [-I|--system-info] [-S|--do-not-task-set] [-m|--memory-limit=256] [-t|--time-limit=600] [-T|--run-test=name1 ...]' . PHP_EOL
239251
. PHP_EOL
240252
. ' -h|--help - print this help and exit' . PHP_EOL
253+
. ' -x|--debug - enable debug mode, raise output level' . PHP_EOL
241254
. ' -d|--dont-recalc - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
242255
. ' -D|--dumb-test-print - print dumb test time, for debug purpose' . PHP_EOL
243256
. ' -L|--list-tests - output list of available tests and exit' . PHP_EOL
@@ -258,6 +271,7 @@ function print_pre($msg) {
258271
. 'Usage: ' . basename(__FILE__) . ' [-h] [-d] [-D] [-L] [-I] [-S] [-m 256] [-t 600] [-T name1 ...]' . PHP_EOL
259272
. PHP_EOL
260273
. ' -h - print this help and exit' . PHP_EOL
274+
. ' -x - enable debug mode, raise output level' . PHP_EOL
261275
. ' -d - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
262276
. ' -D - print dumb test time, for debug purpose' . PHP_EOL
263277
. ' -L - output list of available tests and exit' . PHP_EOL
@@ -288,6 +302,11 @@ function print_pre($msg) {
288302
$recalculateLimits = 0;
289303
break;
290304

305+
case 'x':
306+
case 'debug':
307+
$debugMode = 1;
308+
break;
309+
291310
case 'D':
292311
case 'dumb-test-print':
293312
$printDumbTest = 1;
@@ -523,9 +542,17 @@ function convert_si($size)
523542
*/
524543
function getPhpMemoryLimitBytes()
525544
{
545+
global $debugMode;
526546
// http://stackoverflow.com/a/10209530
527547
$memory_limit = strtolower(ini_get('memory_limit'));
548+
if ($debugMode) {
549+
print_pre("<<< DEBUG >>> getPhpMemoryLimitBytes(): ini_get memory_limit = '{$memory_limit}'\n");
550+
}
528551
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
552+
if ($debugMode) {
553+
$ve = var_export($matches, true);
554+
print_pre("<<< DEBUG >>> getPhpMemoryLimitBytes(): parse via preg_math:\n{$ve}\n");
555+
}
529556
if ($matches[2] == 'g') {
530557
$memory_limit = intval($matches[1]) * 1024 * 1024 * 1024; // nnnG -> nnn GB
531558
} else if ($matches[2] == 'm') {
@@ -536,6 +563,9 @@ function getPhpMemoryLimitBytes()
536563
$memory_limit = intval($matches[1]); // nnn -> nnn B
537564
}
538565
}
566+
if ($debugMode) {
567+
print_pre("<<< DEBUG >>> getPhpMemoryLimitBytes(): result memory_limit = '{$memory_limit}'\n");
568+
}
539569
return $memory_limit;
540570
}
541571

@@ -568,10 +598,23 @@ function getSystemMemInfo()
568598
*/
569599
function getSystemMemoryFreeLimitBytes()
570600
{
601+
global $debugMode;
571602
$info = getSystemMemInfo();
603+
604+
if ($debugMode) {
605+
$ve = var_export($info, true);
606+
print_pre("<<< DEBUG >>> getSystemMemoryFreeLimitBytes(): system memory info:\n{$ve}'\n");
607+
}
608+
572609
if (isset($info['MemAvailable'])) {
610+
if ($debugMode) {
611+
print_pre("<<< DEBUG >>> getSystemMemoryFreeLimitBytes(): return MemAvailable\n");
612+
}
573613
return $info['MemAvailable'];
574614
}
615+
if ($debugMode) {
616+
print_pre("<<< DEBUG >>> getSystemMemoryFreeLimitBytes(): return MemFree + Cached + Buffers\n");
617+
}
575618
return $info['MemFree'] + $info['Cached'] + $info['Buffers'];
576619
}
577620

@@ -824,8 +867,17 @@ function mymemory_usage()
824867
}
825868
}
826869

827-
$memoryLimit = min(getPhpMemoryLimitBytes(), getSystemMemoryFreeLimitBytes());
870+
$memoryLimitPhp = getPhpMemoryLimitBytes();
871+
$memoryLimitSystem = getSystemMemoryFreeLimitBytes();
872+
if ($debugMode) {
873+
print_pre("<<< DEBUG >>> Available memory in system: " . convert($memoryLimitSystem) . PHP_EOL);
874+
print_pre("<<< DEBUG >>> Available memory for php : " . convert($memoryLimitPhp) . PHP_EOL);
875+
}
876+
$memoryLimit = min($memoryLimitPhp, $memoryLimitSystem);
828877
$memoryLimitMb = convert($memoryLimit);
878+
if ($debugMode) {
879+
print_pre("<<< DEBUG >>> Selected memory for php : " . $memoryLimitMb . PHP_EOL);
880+
}
829881

830882
// Adjust array tests limits
831883
if ($memoryLimit < $testMemoryFull) {
@@ -834,6 +886,10 @@ function mymemory_usage()
834886
. " is less than minimum required: " . convert($testMemoryFull)
835887
. ".\n Recalculate tests parameters to fit in memory limits."
836888
. "\n$line" . PHP_EOL);
889+
if ($debugMode) {
890+
print_pre("<<< DEBUG >>> Original memory limit for php : " . convert($originMemoryLimit) . PHP_EOL);
891+
print_pre("<<< DEBUG >>> Calculated memory limit for php: " . convert($defaultMemoryLimit) . PHP_EOL);
892+
}
837893

838894
$factor = 1.0 * ($testMemoryFull - $memoryLimit) / $testMemoryFull;
839895

0 commit comments

Comments
 (0)
Failed to load comments.