-
Notifications
You must be signed in to change notification settings - Fork 27
/
common.inc
593 lines (517 loc) · 14.8 KB
/
common.inc
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
<?php
/**
* Common php test functions
* Php 4.4+
*/
/** ---------------------------------- Tests functions -------------------------------------------- */
function test_01_Math()
{
global $testsLoopLimits, $totalOps;
$mathFunctions = array('abs', 'acos', 'asin', 'atan', 'decbin', 'dechex', 'decoct', 'floor', 'exp', 'log1p', 'sin', 'tan', 'is_finite', 'is_nan', 'sqrt', 'rad2deg');
foreach ($mathFunctions as $key => $function) {
if (!function_exists($function)) {
unset($mathFunctions[$key]);
}
}
$count = $testsLoopLimits['01_math'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($mathFunctions as $function) {
$r = call_user_func_array($function, array($i));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_02_String_Concat()
{
global $testsLoopLimits, $stringConcatLoopRepeat, $totalOps;
$count = $testsLoopLimits['02_string_concat'];
$time_start = get_microtime();
for ($r = 0; $r < $stringConcatLoopRepeat; ++$r) {
$s = '';
for ($i = 0; $i < $count; ++$i) {
$s .= ' - Valar dohaeris';
// Work-around for opCache 8.0+ code elimination, maybe
$s .= ($i % 111) ? PHP_EOL : '!';
}
}
$totalOps += $count * $stringConcatLoopRepeat;
$memory = mymemory_usage();
unset($s);
return format_result_test(get_microtime() - $time_start, $count * $stringConcatLoopRepeat, $memory);
}
function test_03_1_String_Number_Concat()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['03_1_string_number_concat'];
$time_start = get_microtime();
$s = '';
for ($i = 0; $i < $count; ++$i) {
$f = $i * 1.0;
$s = 'This is number ' . $i . ' string concat. Число: ' . $f . PHP_EOL;
// Work-around for opCache 8.0+ code elimination, maybe
$s .= (($i % 333 == 0) ? '!' : '');
}
$totalOps += $count;
$memory = mymemory_usage();
unset($s);
return format_result_test(get_microtime() - $time_start, $count, $memory);
}
function test_03_2_String_Number_Format()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['03_2_string_number_format'];
$time_start = get_microtime();
$s = '';
for ($i = 0; $i < $count; ++$i) {
$f = $i * 1.0;
$s = "This is number {$i} string format. Число: {$f}\n";
// Work-around for opCache 8.0+ code elimination, maybe
$s .= (($i % 333 == 0) ? '!' : '');
}
$totalOps += $count;
$memory = mymemory_usage();
unset($s);
return format_result_test(get_microtime() - $time_start, $count, $memory);
}
function test_04_String_Simple_Functions()
{
global $stringTest, $testsLoopLimits, $totalOps;
$stringFunctions = array('strtoupper', 'strtolower', 'strrev', 'strlen', 'str_rot13', 'ord', 'trim');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
$count = $testsLoopLimits['04_string_simple'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_05_String_Multibyte()
{
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
if (!function_exists('mb_strlen')) {
return $emptyResult;
}
$stringFunctions = array('mb_strtoupper', 'mb_strtolower', 'mb_strlen', 'mb_strwidth');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
$count = $testsLoopLimits['05_string_mb'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_06_String_Manipulation()
{
global $stringTest, $testsLoopLimits, $totalOps;
$stringFunctions = array('addslashes', 'chunk_split', 'metaphone', 'strip_tags', 'soundex', 'wordwrap');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
$count = $testsLoopLimits['06_string_manip'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_07_Regex()
{
global $stringTest, $regexPattern, $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['07_regex'];
$time_start = get_microtime();
$stringFunctions = array('preg_match', 'preg_split');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($regexPattern, $stringTest));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_08_1_Hashing()
{
global $stringTest, $testsLoopLimits, $totalOps;
$stringFunctions = array('crc32', 'md5', 'sha1');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
$count = $testsLoopLimits['08_1_hashing'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_08_2_Crypt()
{
global $stringTest, $cryptSalt, $testsLoopLimits, $totalOps;
$stringFunctions = array('crypt');
foreach ($stringFunctions as $key => $function) {
if (!function_exists($function)) {
unset($stringFunctions[$key]);
}
}
$count = $testsLoopLimits['08_2_crypt'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($stringFunctions as $function) {
$r = call_user_func_array($function, array($stringTest, $cryptSalt));
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_09_Json_Encode()
{
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
if (!function_exists('json_encode')) {
return $emptyResult;
}
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
$obj = new stdClass();
$obj->fieldStr = 'value';
$obj->fieldInt = 123456;
$obj->fieldFloat = 123.456;
$obj->fieldArray = array(123456);
$obj->fieldNull = null;
$obj->fieldBool = false;
$data[] = $obj;
$count = $testsLoopLimits['09_json_encode'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($data as $value) {
$r = json_encode($value);
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_10_Json_Decode()
{
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
if (!function_exists('json_decode')) {
return $emptyResult;
}
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
$obj = new stdClass();
$obj->fieldStr = 'value';
$obj->fieldInt = 123456;
$obj->fieldFloat = 123.456;
$obj->fieldArray = array(123456);
$obj->fieldNull = null;
$obj->fieldBool = false;
$data[] = $obj;
foreach ($data as $key => $value) {
$data[$key] = json_encode($value);
}
$count = $testsLoopLimits['10_json_decode'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($data as $value) {
$r = json_decode($value);
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_11_Serialize()
{
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
if (!function_exists('serialize')) {
return $emptyResult;
}
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
$obj = new stdClass();
$obj->fieldStr = 'value';
$obj->fieldInt = 123456;
$obj->fieldFloat = 123.456;
$obj->fieldArray = array(123456);
$obj->fieldNull = null;
$obj->fieldBool = false;
$data[] = $obj;
$count = $testsLoopLimits['11_serialize'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($data as $value) {
$r = serialize($value);
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_12_Unserialize()
{
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
if (!function_exists('unserialize')) {
return $emptyResult;
}
$data = array(
$stringTest,
123456,
123.456,
array(123456),
null,
false,
);
$obj = new stdClass();
$obj->fieldStr = 'value';
$obj->fieldInt = 123456;
$obj->fieldFloat = 123.456;
$obj->fieldArray = array(123456);
$obj->fieldNull = null;
$obj->fieldBool = false;
$data[] = $obj;
foreach ($data as $key => $value) {
$data[$key] = serialize($value);
}
$count = $testsLoopLimits['12_unserialize'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($data as $value) {
$r = unserialize($value);
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_13_Array_Fill()
{
global $testsLoopLimits, $arrayDimensionLimit, $totalOps;
$arrayTestLoopLimit = $testsLoopLimits['13_array_loop'];
$time_start = get_microtime();
$memory = 0;
for ($n = 0; $n < $arrayTestLoopLimit; ++$n) {
$x = array();
for ($i = 0; $i < $arrayDimensionLimit; ++$i) {
for ($j = 0; $j < $arrayDimensionLimit; ++$j) {
$x[$i][$j] = $i * $j;
}
}
if (!$memory) $memory = mymemory_usage();
unset($x);
}
$totalOps += pow($arrayDimensionLimit, 2) * $arrayTestLoopLimit;
return format_result_test(get_microtime() - $time_start, pow($arrayDimensionLimit, 2) * $arrayTestLoopLimit, $memory);
}
function test_14_Array_Range()
{
global $testsLoopLimits, $arrayDimensionLimit, $totalOps;
$arrayTestLoopLimit = $testsLoopLimits['14_array_loop'];
$time_start = get_microtime();
$memory = 0;
for ($n = 0; $n < $arrayTestLoopLimit; ++$n) {
$x = array()+range(0, $arrayDimensionLimit);
for ($i = 0; $i < $arrayDimensionLimit; $i++) {
$x[$i] = array()+range(0, $arrayDimensionLimit);
}
if (!$memory) $memory = mymemory_usage();
unset($x);
}
$totalOps += $arrayDimensionLimit * $arrayTestLoopLimit;
return format_result_test(get_microtime() - $time_start, $arrayDimensionLimit * $arrayTestLoopLimit, $memory);
}
function test_14_Array_Unset()
{
global $testsLoopLimits, $arrayDimensionLimit, $totalOps;
$xx = range(0, $arrayDimensionLimit);
for ($i = 0; $i < $arrayDimensionLimit; $i++) {
$xx[$i] = array()+range(0, $arrayDimensionLimit);
}
$arrayTestLoopLimit = $testsLoopLimits['14_array_loop'];
$time_start = get_microtime();
$memory = 0;
for ($n = 0; $n < $arrayTestLoopLimit; ++$n) {
$x = array()+$xx;
if (!$memory) $memory = mymemory_usage();
for ($i = $arrayDimensionLimit-1; $i >= 0; $i--) {
for ($j = 0; $j < $arrayDimensionLimit; $j++) {
unset($x[$i][$j]);
}
unset($x[$i]);
}
unset($x);
}
unset($xx);
$totalOps += pow($arrayDimensionLimit, 2) * $arrayTestLoopLimit;
return format_result_test(get_microtime() - $time_start, pow($arrayDimensionLimit, 2) * $arrayTestLoopLimit, $memory);
}
function test_15_Clean_Loops()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['15_clean_loops'];
$time_start = get_microtime();
// @warning Since 8.0 may be eliminated as dead-code if opcache loaded and enabled
for ($i = 0; $i < $count; ++$i) ;
$i = 0;
// @warning Since 8.0 may be eliminated as dead-code if opcache loaded and enabled
while ($i++ < $count) ;
$totalOps += $count * 2;
return format_result_test(get_microtime() - $time_start, $count * 2, mymemory_usage());
}
function test_16_Loop_IfElse()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['16_loop_ifelse'];
$time_start = get_microtime();
$a = 0;
for ($i = 0; $i < $count; $i++) {
$d = $i % 5;
if ($d == 1) {
$a++;
} elseif ($d == 2) {
$a--;
} else if ($d == 3) {
$a++;
} else {
$a--;
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_17_Loop_Ternary()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['17_loop_ternary'];
$time_start = get_microtime();
$a = 0;
for ($i = 0; $i < $count; $i++) {
$r = ($i % 2 == 1)
? (($i % 3 == 1)
? (($i % 5 == 1)
? 3
: 2)
: 1)
: 0;
$a += $r;
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_18_1_Loop_Defined_Access()
{
global $testsLoopLimits, $totalOps;
$a = array(0 => 1, 1 => 0);
$r = 0;
$count = $testsLoopLimits['18_1_loop_def'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
$r += $a[$i % 2];
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_18_2_Loop_Undefined_Access()
{
global $testsLoopLimits, $totalOps;
$a = array(1 => 1, 3 => 1);
$r = 0;
$count = $testsLoopLimits['18_2_loop_undef'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
$r += @$a[$i % 5] ? 0 : 1;
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_19_Type_Functions()
{
global $testsLoopLimits, $totalOps;
$ia = array('123456', '0.000001', '0x123', '0644', 456, 123.456);
$fa = array('123456.7890', '123.456e7', '3E-12', '0.0000001', 456, 123.456);
$count = $testsLoopLimits['19_type_func'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($ia as $n) {
$r = intval($n);
}
foreach ($fa as $n) {
$r = floatval($n);
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_20_Type_Casting()
{
global $testsLoopLimits, $totalOps;
$ia = array('123456', '0.000001', '0x123', '0644', 456, 123.456);
$fa = array('123456.7890', '123.456e7', '3E-12', '0.0000001', 456, 123.456);
$count = $testsLoopLimits['20_type_cast'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
foreach ($ia as $n) {
$r = (int)$n;
}
foreach ($fa as $n) {
$r = (float)$n;
}
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}
function test_33_PhpInfo_Generate()
{
global $testsLoopLimits, $totalOps;
$count = $testsLoopLimits['33_phpinfo_generate'];
$time_start = get_microtime();
for ($i = 0; $i < $count; $i++) {
ob_start();
phpinfo();
ob_get_clean();
}
$totalOps += $count;
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
}