-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbench.php
1865 lines (1645 loc) · 56.5 KB
/
bench.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
################################################################################
# PHP Benchmark Performance Script #
# 2010 Code24 BV #
# 2015-2023 Rusoft #
# #
# Author : Alessandro Torrisi #
# Company : Code24 BV, The Netherlands #
# Author : Sergey Dryabzhinsky #
# Company : Rusoft Ltd, Russia #
# Date : Sep 22, 2023 #
# Version : 1.0.57 #
# License : Creative Commons CC-BY license #
# Website : https://github.com/rusoft/php-simple-benchmark-script #
# Website : https://git.rusoft.ru/open-source/php-simple-benchmark-script #
# #
################################################################################
*/
$scriptVersion = '1.0.57';
// Special string to flush buffers, nginx for example
$flushStr = '<!-- '.str_repeat(" ", 8192).' -->';
// Used in hacks/fixes checks
$phpversion = explode('.', PHP_VERSION);
$messagesCnt = 0;
$rawValues4json = false;
$totalOps = 0;
if (php_sapi_name() == 'cli') {
// Terminal color sequence
$colorReset = "\033[0m";
$colorRed = "\033[31m";
$colorGreen = "\033[32m";
$colorYellow = "\033[33m";
$colorGray = "\033[30m";
$term = getenv('TERM');
if (in_array($term, array('xterm', 'urxvt', 'linux', 'screen'))) {
// Concrete terms, or limited terms
// pass
} else if (strpos($term, '-color') !== false) {
// Special string
// pass
} else if (strpos($term, '-256color') !== false) {
// Special string
// pass
} else {
// not pass
$colorReset = '';
$colorRed = '';
$colorGreen = '';
$colorYellow = '';
$colorGray = '';
}
} else {
// Html colors
$colorReset = '</span>'; // just closing tag
$colorRed = '<span style="color:red">';
$colorGreen = '<span style="color:green">';
$colorYellow = '<span style="color:orange">';
$colorGray = '<span style="color:gray">';
}
/** ------------------------------- Main Defaults ------------------------------- */
$has_igb = "{$colorYellow}no{$colorReset}";
if (extension_loaded('igbinary')) {
$has_igb = "{$colorGreen}yes{$colorReset}";
@include("igbinary.inc");
}
$has_msg = "{$colorYellow}no{$colorReset}";
if (extension_loaded('msgpack')) {
$has_msg = "{$colorGreen}yes{$colorReset}";
@include("msgpack.inc");
}
if (extension_loaded('zstd')) {
@include_once("compression.inc");
}
if (extension_loaded('lz4')) {
@include_once("compression.inc");
}
if (extension_loaded('brotli')) {
@include_once("compression.inc");
}
if (extension_loaded('gd')) {
@include_once("php-gd-imagick-common.inc");
@include_once("php-gd.inc");
}
if (extension_loaded('imagick')) {
@include_once("php-gd-imagick-common.inc");
@include_once("php-imagick.inc");
}
$originMemoryLimit = @ini_get('memory_limit');
$originTimeLimit = @ini_get('max_execution_time');
/* Default execution time limit in seconds */
$defaultTimeLimit = 600;
/*
Default PHP memory limit in Mb.
It's for ALL PHP structures!
Memory allocator works with blocks by X_Mb.
Some we need a little more.
*/
$defaultMemoryLimit = 130;
$useColors = 1;
$debugMode = 0;
$printJson = 0;
$printMachine = 0;
$recalculateLimits = 1;
$printDumbTest = 0;
$outputTestsList = 0;
$showOnlySystemInfo = 0;
$selectedTests = array();
/* ----------------- Fetch environ or GET params */
if ($t = (int)getenv('PHP_TIME_LIMIT')) {
$defaultTimeLimit = $t;
}
if (isset($_GET['time_limit']) && $t = (int)$_GET['time_limit']) {
$defaultTimeLimit = $t;
}
if ($x = (int)getenv('DONT_USE_COLORS')) {
$useColors = $x == 0;
}
if (isset($_GET['dont_use_colors']) && $x = (int)$_GET['dont_use_colors']) {
$useColors = $x == 0;
}
if ($x = (int)getenv('PHP_DEBUG_MODE')) {
$debugMode = $x;
}
if (isset($_GET['debug_mode']) && $x = (int)$_GET['debug_mode']) {
$debugMode = $x;
}
if ($x = (int)getenv('PRINT_JSON')) {
$printJson = $x;
}
if (isset($_GET['print_json']) && $x = (int)$_GET['print_json']) {
$printJson = $x;
}
if ($printJson) $printMachine = 0;
if ($x = (int)getenv('PRINT_MACHINE')) {
$printMachine = $x;
}
if (isset($_GET['print_machine']) && $x = (int)$_GET['print_machine']) {
$printMachine = $x;
}
if ($printMachine) $printJson = 0;
if ($m = (int)getenv('PHP_MEMORY_LIMIT')) {
$defaultMemoryLimit = $m;
}
if (isset($_GET['memory_limit']) && $m = (int)$_GET['memory_limit']) {
$defaultMemoryLimit = $m;
}
if ((int)getenv('DONT_RECALCULATE_LIMITS')) {
$recalculateLimits = 0;
}
if (isset($_GET['dont_recalculate_limits']) && (int)$_GET['dont_recalculate_limits']) {
$recalculateLimits = 0;
}
if ((int)getenv('PRINT_DUMB_TEST')) {
$printDumbTest = 1;
}
if (isset($_GET['print_dumb_test']) && (int)$_GET['print_dumb_test']) {
$printDumbTest = 1;
}
if ((int)getenv('LIST_TESTS')) {
$outputTestsList = 1;
}
if (isset($_GET['list_tests']) && (int)$_GET['list_tests']) {
$outputTestsList = 1;
}
if ((int)getenv('SYSTEM_INFO')) {
$showOnlySystemInfo = 1;
}
if (isset($_GET['system_info']) && (int)$_GET['system_info']) {
$showOnlySystemInfo = 1;
}
if ($r = getenv('RUN_TESTS')) {
$selectedTests = explode(',', $r);
}
if (!empty($_GET['run_tests'])) {
$selectedTests = explode(',', $_GET['run_tests']);
}
/* common functions */
function print_pre($msg) {
global $printJson, $printMachine, $messagesCnt;
if ($printMachine) {
print($msg);
} else if ($printJson) {
$msg = trim(str_replace("\n", " ", $msg));
if (function_exists('json_encode')) {
$msg = json_encode($msg);
} else {
$msg = '"'.$msg.'"';
}
print('"message_'.$messagesCnt.'": '.$msg.','.PHP_EOL);
} else {
if (php_sapi_name() != 'cli') {
print('<pre>'.$msg.'</pre>');
} else {
print($msg);
}
}
flush();
$messagesCnt++;
}
function print_norm($msg) {
global $printJson, $messagesCnt;
if ($printJson) {
$msg = trim(str_replace("\n", " ", $msg));
if (function_exists('json_encode')) {
$msg = json_encode($msg);
} else {
$msg = '"'.$msg.'"';
}
print('"message_'.$messagesCnt.'": '.$msg.','.PHP_EOL);
} else {
print($msg);
}
flush();
$messagesCnt++;
}
if (!function_exists('gethostname')) {
// 5.3.0+ only
function gethostname() {
on_start();
$last_str = system(`hostname -f`, $errcode);
if ($last_str !== false) {
return $last_str;
}
return '';
}
}
/* global command line options */
if (php_sapi_name() == 'cli') {
// http://php.net/manual/ru/function.getopt.php example #2
$shortopts = "h";
$shortopts .= "x";
$shortopts .= "d";
$shortopts .= "C";
$shortopts .= "J";
$shortopts .= "M";
$shortopts .= "D";
$shortopts .= "L";
$shortopts .= "I";
$shortopts .= "m:"; // Обязательное значение
$shortopts .= "t:"; // Обязательное значение
$shortopts .= "T:"; // Обязательное значение
$longopts = array(
"help",
"debug",
"dont-use-colors",
"print-json",
"print-machine",
"dont-recalc",
"dumb-test-print",
"list-tests",
"system-info",
"memory-limit:", // Обязательное значение
"time-limit:", // Обязательное значение
"run-test:", // Обязательное значение
);
$hasLongOpts = true;
if ((int)$phpversion[0] > 5) {
$options = getopt($shortopts, $longopts);
} elseif ((int)$phpversion[0] == 5 && (int)$phpversion[1] >= 3) {
$options = getopt($shortopts, $longopts);
} else {
$options = getopt($shortopts);
$hasLongOpts = false;
}
if ($options) {
// First - simple options that do not do any output
foreach ($options as $okey => $oval) {
switch ($okey) {
case 'd':
case 'dont-recalc':
$recalculateLimits = 0;
break;
case 'x':
case 'debug':
$debugMode = 1;
break;
case 'C':
case 'dont-use-colors':
$useColors = 0;
break;
case 'J':
case 'print-json':
$printJson = 1;
$printMachine = 0;
break;
case 'M':
case 'print-machine':
$printMachine = 1;
$printJson = 0;
break;
case 'D':
case 'dumb-test-print':
$printDumbTest = 1;
break;
case 'L':
case 'list-tests':
$outputTestsList = 1;
break;
case 'I':
case 'system-info':
$showOnlySystemInfo = 1;
break;
} // switch key
} // for options
// Drop colors here
if (!$useColors || $printJson || $printMachine) {
$colorReset = '';
$colorRed = '';
$colorGreen = '';
$colorYellow = '';
$colorGray = '';
}
// Start JSON output here
if ($printJson) print("{ " . PHP_EOL);
foreach ($options as $okey => $oval) {
switch ($okey) {
case 'h':
case 'help':
if ($hasLongOpts) {
print_pre(
PHP_EOL
. 'PHP Benchmark Performance Script, version ' . $scriptVersion . PHP_EOL
. PHP_EOL
. 'Usage: ' . basename(__FILE__) . ' [-h|--help] [-x|--debug] [-C|--dont-use-colors] [-J|--print-json] [-M|--print-machine] [-d|--dont-recalc] [-D|--dumb-test-print] [-L|--list-tests] [-I|--system-info] [-S|--do-not-task-set] [-m|--memory-limit=130] [-t|--time-limit=600] [-T|--run-test=name]' . PHP_EOL
. PHP_EOL
. ' -h|--help - print this help and exit' . PHP_EOL
. ' -x|--debug - enable debug mode, raise output level' . PHP_EOL
. ' -C|--dont-use-colors - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not in JSON/machine mode.' . PHP_EOL
. ' -J|--print-json - enable printing only in JSON format, useful for automated tests. disables print-machine.' . PHP_EOL
. ' -M|--print-machine - enable printing only in machine parsable format, useful for automated tests. disables print-json.' . PHP_EOL
. ' -d|--dont-recalc - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
. ' -D|--dumb-test-print - print dumb test time, for debug purpose' . PHP_EOL
. ' -L|--list-tests - output list of available tests and exit' . PHP_EOL
. ' -I|--system-info - output system info but do not run tests and exit' . PHP_EOL
. ' -m|--memory-limit <Mb> - set memory_limit value in Mb, defaults to 130 (Mb)' . PHP_EOL
. ' -t|--time-limit <sec> - set max_execution_time value in seconds, defaults to 600 (sec)' . PHP_EOL
. ' -T|--run-test <name> - run selected tests, test names from --list-tests output, can be defined multiple times' . PHP_EOL
. PHP_EOL
. 'Example: php ' . basename(__FILE__) . ' -m=64 -t=30' . PHP_EOL
. PHP_EOL
);
} else {
print_pre(
PHP_EOL
. 'PHP Benchmark Performance Script, version ' . $scriptVersion . PHP_EOL
. PHP_EOL
. 'Usage: ' . basename(__FILE__) . ' [-h] [-x] [-C] [-J] [-M] [-d] [-D] [-L] [-I] [-S] [-m 130] [-t 600] [-T name]' . PHP_EOL
. PHP_EOL
. ' -h - print this help and exit' . PHP_EOL
. ' -x - enable debug mode, raise output level' . PHP_EOL
. ' -C - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not in JSON/machine mode.' . PHP_EOL
. ' -J - enable printing only in JSON format, useful for automated tests. disables print-machine.' . PHP_EOL
. ' -M - enable printing only in machine parsable format, useful for automated tests. disables print-json.' . PHP_EOL
. ' -d - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
. ' -D - print dumb test time, for debug purpose' . PHP_EOL
. ' -L - output list of available tests and exit' . PHP_EOL
. ' -I - output system info but do not run tests and exit' . PHP_EOL
. ' -m <Mb> - set memory_limit value in Mb, defaults to 130 (Mb)' . PHP_EOL
. ' -t <sec> - set max_execution_time value in seconds, defaults to 600 (sec)' . PHP_EOL
. ' -T <name> - run selected tests, test names from -L output, can be defined multiple times' . PHP_EOL
. PHP_EOL
. 'Example: php ' . basename(__FILE__) . ' -m 64 -t 30' . PHP_EOL
. PHP_EOL
);
}
if ($printJson) {
print("\"messages_count\": {$messagesCnt},\n");
print("\"end\":true\n}" . PHP_EOL);
}
exit(0);
break;
case 'm':
case 'memory-limit':
if (is_numeric($oval)) {
$defaultMemoryLimit = (int)$oval;
} else {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Option '$okey' has not numeric value '$oval'! Skip." . PHP_EOL);
}
break;
case 't':
case 'time-limit':
if (is_numeric($oval)) {
$defaultTimeLimit = (int)$oval;
} else {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Option '$okey' has not numeric value '$oval'! Skip." . PHP_EOL);
}
break;
case 'T':
case 'run-test':
// Multiple values are joined into array
if (!empty($oval)) {
$selectedTests = (array)$oval;
} else {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Option '$okey' has no value! Skip." . PHP_EOL);
}
break;
case 'd':
case 'dont-recalc':
case 'x':
case 'debug':
case 'C':
case 'dont-use-colors':
case 'J':
case 'print-json':
case 'M':
case 'print-machine':
case 'D':
case 'dumb-test-print':
case 'L':
case 'list-tests':
case 'I':
case 'system-info':
// Done in previous cycle
break;
default:
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Unknown option '$okey'!" . PHP_EOL);
}
}
} // if options
} // if sapi == cli
// Drop colors here too
if (!$useColors || $printJson || $printMachine) {
$colorReset = '';
$colorRed = '';
$colorGreen = '';
$colorYellow = '';
$colorGray = '';
}
if (php_sapi_name() != 'cli') {
// Hello, nginx!
header('X-Accel-Buffering: no', true);
if ($printJson) {
header('Content-Type: application/json', true);
} else {
header('Content-Type: text/html; charset=utf-8', true);
}
flush();
} else {
$flushStr = '';
}
$tz = ini_get('date.timezone');
if (!$tz) ini_set('date.timezone', 'Europe/Moscow');
ini_set('display_errors', 0);
@ini_set('error_log', null);
ini_set('implicit_flush', 1);
ini_set('output_buffering', 0);
ob_implicit_flush(1);
// Disable explicit error reporting
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Check XDebug
$xdebug = (int)ini_get('xdebug.default_enable');
if ($xdebug) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} You need to disable Xdebug extension! It greatly slow things down! And mess with PHP internals.".PHP_EOL);
}
// Check OpCache
if (php_sapi_name() != 'cli') {
$opcache = (int)ini_get('opcache.enable');
if ($opcache) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} You may want to disable OpCache extension! It can greatly affect the results! Make it via .htaccess, VHost or FPM config.".PHP_EOL);
}
$apcache = (int)ini_get('apc.enabled');
} else {
$opcache = (int)ini_get('opcache.enable_cli');
if ($opcache) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} You may want to disable Cli OpCache extension! It can greatly affect the results! Run php with param: -dopcache.enable_cli=0".PHP_EOL);
}
$apcache = (int)ini_get('apc.enable_cli');
}
$xcache = (int)ini_get('xcache.cacher');
$eaccel = (int)ini_get('eaccelerator.enable');
$mbover = (int)ini_get('mbstring.func_overload');
if ($mbover != 0) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} You must disable mbstring string functions overloading! It greatly slow things down! And messes with results.".PHP_EOL);
}
$obd_set = (int)!in_array(ini_get('open_basedir'), array('', null));
if ($obd_set != 0) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} You should unset `open_basedir` parameter! It may slow things down!".PHP_EOL);
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Parameter `open_basedir` in effect! Script may not able to read system CPU and Memory information. Memory adjustment for tests may not work.\n");
}
$dropDead = false;
// No php < 4
if ((int)$phpversion[0] < 4) {
$dropDead = true;
}
// No php <= 4.3
if ((int)$phpversion[0] == 4 && (int)$phpversion[1] < 3) {
$dropDead = true;
}
if ($dropDead) {
print_pre("{$colorRed}<<< ERROR >>>{$colorReset} Need PHP 4.3+! Current version is " . PHP_VERSION .PHP_EOL);
if ($printJson) {
print("\"messages_count\": {$messagesCnt},\n");
print("\"end\":true\n}".PHP_EOL);
}
exit(1);
}
if (!defined('PHP_MAJOR_VERSION')) {
define('PHP_MAJOR_VERSION', (int)$phpversion[0]);
}
if (!defined('PHP_MINOR_VERSION')) {
define('PHP_MINOR_VERSION', (int)$phpversion[1]);
}
if ($debugMode) {
ini_set('display_errors', 1);
error_reporting(E_ALL);
}
$set = set_time_limit($defaultTimeLimit);
if ($set === false) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Execution time limit not droppped to '{$defaultTimeLimit}' seconds!\nScript will have only '{$originTimeLimit}' seconds to run." . PHP_EOL);
}
$set = ini_set('memory_limit', $defaultMemoryLimit . 'M');
if ($set === false) {
print_pre("{$colorYellow}<<< WARNING >>>{$colorReset} Memory limit not set to '{$defaultMemoryLimit}'!" . PHP_EOL);
}
/** ------------------------------- Main Constants ------------------------------- */
$line = str_pad("-", 91, "-");
$padHeader = 89;
$padInfo = 19;
$padLabel = 30;
$emptyResult = array(0, '-.---', '-.-- ', '-.-- ', 0);
$cryptSalt = null;
$cryptAlgoName = 'default';
// That gives around 130Mb memory use and reasonable test time
$testMemoryFull = 130 * 1024 * 1024;
// That gives around 8Mb memory use to run every tests
$testMemoryMin = 5 * 1024 * 1024;
// Arrays are matrix [$dimention] x [$dimention]
$arrayDimensionLimit = 600;
// That limit gives around 128Mb too
$stringConcatLoopRepeat = 5;
$runOnlySelectedTests = !empty($selectedTests);
$stringTest = " the quick <b>brown</b> fox jumps <i>over</i> the lazy dog and eat <span>lorem ipsum</span><br/> Valar morghulis <br/>\n\rабыр\nвалар дохаэрис <span class='alert alert-danger'>У нас закончились ложки, Нео!</span> ";
$regexPattern = '/[\s,]+/';
/** ---------------------------------- Tests limits - to recalculate -------------------------------------------- */
// Gathered on this machine
$loopMaxPhpTimesMHz = 3800;
// How much time needed for tests on this machine
$loopMaxPhpTimes = array(
'4.4' => 324,
'5.2' => 248,
'5.3' => 211,
'5.4' => 199,
'5.5' => 200,
'5.6' => 204,
'7.0' => 106,
'7.1' => 104,
'7.2' => 98,
'7.3' => 89,
'7.4' => 89,
'8.0' => 83,
'8.1' => 82,
'8.2' => 79,
);
// Simple and fast test times, used to adjust all test times and limits
$dumbTestMaxPhpTimes = array(
'4.4' => 1.041,
'5.2' => 0.771,
'5.3' => 0.737,
'5.4' => 0.769,
'5.5' => 0.770,
'5.6' => 0.781,
'7.0' => 0.425,
'7.1' => 0.425,
'7.2' => 0.412,
'7.3' => 0.339,
'7.4' => 0.340,
'8.0' => 0.324,
'8.1' => 0.323,
'8.2' => 0.294,
);
// Nice dice roll
// Should not be longer than 600 seconds
$testsLoopLimits = array(
'01_math' => 2000000,
// That limit gives around 90Mb
'02_string_concat' => 5000000,
'03_1_string_number_concat' => 5000000,
'03_2_string_number_format' => 5000000,
'04_string_simple' => 1300000,
'05_string_mb' => 130000,
'06_string_manip' => 1300000,
'07_regex' => 1300000,
'08_1_hashing' => 1300000,
'08_2_crypt' => 10000,
'09_json_encode' => 1300000,
'10_json_decode' => 1300000,
'11_serialize' => 1300000,
'12_unserialize' => 1300000,
'11_igb_serialize' => 1300000,
'12_igb_unserialize' => 1300000,
'11_msgpack_pack' => 1300000,
'12_msgpack_unpack' => 1300000,
'13_array_loop' => 250,
'14_array_loop' => 250,
'15_clean_loops' => 200000000,
'16_loop_ifelse' => 100000000,
'17_loop_ternary' => 100000000,
'18_1_loop_def' => 50000000,
'18_2_loop_undef' => 50000000,
'19_type_func' => 4000000,
'20_type_cast' => 4000000,
'21_loop_except' => 10000000,
'22_loop_nullop' => 60000000,
'23_loop_spaceship' => 60000000,
'26_1_public' => 10000000,
'26_2_getset' => 10000000,
'26_3_magic' => 10000000,
'27_simplexml' => 50000,
'28_domxml' => 50000,
'29_datetime' => 500000,
'30_intl_number_format' => 20000,
'31_intl_message_format' => 200000,
'32_intl_calendar' => 300000,
'33_phpinfo_generate' => 10000,
'34_gd_qrcode' => 1000,
'35_imagick_qrcode' => 1000,
'36_zlib_compress' => 5000000,
'36_gzip_compress' => 5000000,
'36_bzip2_compress' => 500000,
'36_lz4_compress' => 5000000,
'36_zstd_compress' => 5000000,
'36_brotli_compress' => 1000000,
);
// Should not be more than X Mb
// Different PHP could use different amount of memory
// There defined maximum possible
$testsMemoryLimits = array(
'01_math' => 4,
'02_string_concat' => 90,
'03_1_string_number_concat' => 4,
'03_2_string_number_format' => 4,
'04_string_simple' => 4,
'05_string_mb' => 4,
'06_string_manip' => 4,
'07_regex' => 4,
'08_1_hashing' => 4,
'08_2_crypt' => 4,
'09_json_encode' => 4,
'10_json_decode' => 4,
'11_serialize' => 4,
'12_unserialize' => 4,
'11_igb_serialize' => 4,
'12_igb_unserialize' => 4,
// php-5.3
'13_array_loop' => 54,
'14_array_loop' => 62,
// opcache, php-7.4
'15_clean_loops' => 14,
'16_loop_ifelse' => 14,
'17_loop_ternary' => 14,
'18_1_loop_def' => 14,
'18_2_loop_undef' => 14,
'19_type_func' => 14,
'20_type_cast' => 14,
'21_loop_except' => 14,
'22_loop_nullop' => 14,
'23_loop_spaceship' => 14,
'26_1_public' => 14,
'26_2_getset' => 14,
'26_3_magic' => 14,
'27_simplexml' => 14,
'28_domxml' => 14,
'29_datetime' => 14,
'30_intl_number_format' => 14,
'31_intl_message_format' => 14,
'32_intl_calendar' => 14,
'33_phpinfo_generate' => 14,
'34_gd_qrcode' => 14,
'35_imagick_qrcode' => 8,
'36_zlib_compress' => 4,
'36_gzip_compress' => 4,
'36_bzip2_compress' => 4,
'36_lz4_compress' => 4,
'36_zstd_compress' => 4,
'36_brotli_compress' => 4,
);
/** ---------------------------------- Common functions -------------------------------------------- */
/**
* Gt pretty OS release name, if available
*/
function get_current_os()
{
$osFile = '/etc/os-release';
$result = PHP_OS;
if (@is_readable($osFile)) {
$f = fopen($osFile, 'r');
while (!feof($f)) {
$line = trim(fgets($f, 1000000));
if (strpos($line, 'PRETTY_NAME=') === 0) {
$s = explode('=', $line);
$result = array_pop($s);
$result = str_replace('"','', $result);
}
}
}
return $result;
}
function get_microtime()
{
$time = microtime(true);
if (is_string($time)) {
list($f, $i) = explode(' ', $time);
$time = intval($i) + floatval($f);
}
return $time;
}
function convert($size)
{
$unit = array('b', 'kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb');
if ($size <= 0) $i = 0;
else $i = floor(log($size, 1024));
if ($i < 0) $i = 0;
return @round($size / pow(1024, $i), 2) . ' ' . $unit[$i];
}
function prefix_si($size)
{
$unit = array(' ', 'k', 'M', 'G', 'T', 'P', 'E', -3 => 'm', -6 => 'u');
$i = floor(log($size, 1000));
if ($i < 0) {
if ($i <= -6) {
$i = -6;
} elseif ($i <= -3) {
$i = -3;
} else {
$i = 0;
}
}
return $unit[$i];
}
function convert_si($size)
{
$i = floor(log($size, 1000));
if ($i < 0) {
if ($i <= -6) {
$i = -6;
} elseif ($i <= -3) {
$i = -3;
} else {
$i = 0;
}
}
return @round($size / pow(1000, $i), 2);
}
/**
* Return memory_limit in bytes
*
* @return int
*/
function getPhpMemoryLimitBytes()
{
global $debugMode, $colorGray, $colorReset;
// http://stackoverflow.com/a/10209530
$memory_limit = strtolower(ini_get('memory_limit'));
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getPhpMemoryLimitBytes(): ini_get memory_limit = '{$memory_limit}'\n");
}
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
if ($debugMode) {
$ve = var_export($matches, true);
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getPhpMemoryLimitBytes(): parse via preg_math:\n{$ve}\n");
}
if ($matches[2] == 'g') {
$memory_limit = intval($matches[1]) * 1024 * 1024 * 1024; // nnnG -> nnn GB
} else if ($matches[2] == 'm') {
$memory_limit = intval($matches[1]) * 1024 * 1024; // nnnM -> nnn MB
} else if ($matches[2] == 'k') {
$memory_limit = intval($matches[1]) * 1024; // nnnK -> nnn KB
} else {
$memory_limit = intval($matches[1]); // nnn -> nnn B
}
}
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getPhpMemoryLimitBytes(): result memory_limit = '{$memory_limit}'\n");
}
return $memory_limit;
}
/**
* Return array (dict) with system memory info.
* All values in bytes.
* http://stackoverflow.com/a/1455610
*/
function getSystemMemInfo()
{
global $debugMode, $colorGray, $colorReset;
$meminfo = array();
if (! @is_readable("/proc/meminfo")) {
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} Can't read /proc/meminfo!" . PHP_EOL);
}
return $meminfo;
}
$data = explode("\n", file_get_contents("/proc/meminfo"));
foreach ($data as $line) {
if (empty($line)) {
continue;
}
list($key, $val) = explode(":", $line);
$_val = explode(" ", strtolower(trim($val)));
$val = intval($_val[0]);
if (isset($_val[1]) && $_val[1] == 'kb') {
$val *= 1024;
}
$meminfo[$key] = trim($val);
}
return $meminfo;
}
/**
* Return system memory FREE+CACHED+BUFFERS bytes (may be free)
*
* @return int
*/
function getSystemMemoryFreeLimitBytes()
{
global $debugMode, $colorGray, $colorReset;
$info = getSystemMemInfo();
if ($debugMode) {
$ve = var_export($info, true);
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getSystemMemoryFreeLimitBytes(): system memory info:\n{$ve}'\n");
}
if (empty($info)) {
return -1;
}
if (isset($info['MemAvailable'])) {
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getSystemMemoryFreeLimitBytes(): return MemAvailable: {$info['MemAvailable']}\n");
}
return $info['MemAvailable'];
}
$available = $info['MemFree'] + $info['Cached'] + $info['Buffers'];
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} getSystemMemoryFreeLimitBytes(): return MemFree + Cached + Buffers: {$available}\n");
}
return $available;
}
/**
* Read /proc/cpuinfo, fetch some data
*/
function getCpuInfo($fireUpCpu = false)
{
global $debugMode, $colorGray, $colorReset;
$cpu = array(
'model' => '',
'vendor' => '',
'cores' => 0,
'available' => 0,
'mhz' => 0.0,
'max-mhz' => 0.0,
'min-mhz' => 0.0,
'mips' => 0.0
);
if (! @is_readable('/proc/cpuinfo')) {
if ($debugMode) {
print_pre("{$colorGray}<<< DEBUG >>>{$colorReset} Can't read /proc/cpuinfo!" . PHP_EOL);
}
$cpu['model'] = 'Unknown';
$cpu['vendor'] = 'Unknown';
$cpu['cores'] = 1;
$cpu['available'] = 1;
return $cpu;
}
if ($fireUpCpu) {
// Fire up CPU, Don't waste much time here
$i = 30000000;
while ($i--) ;
}
if (@is_readable('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq')) {
$cpu['mhz'] = ((int)file_get_contents('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq'))/1000.0;
}
// Code from https://github.com/jrgp/linfo/blob/master/src/Linfo/OS/Linux.php
// Adopted
$cpuData = explode("\n", file_get_contents('/proc/cpuinfo'));
foreach ($cpuData as $line) {
$line = explode(':', $line, 2);
if (!array_key_exists(1, $line)) {
continue;
}