-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest6_runner.php
142 lines (99 loc) · 3.88 KB
/
test6_runner.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
<?php
//You shouldn't have max_exectution_time set high enough to run these benchmarks
ini_set('max_execution_time', 90000);
opcache_reset();
$isCli = php_sapi_name() == 'cli';
function cliPrint($text, $newLine = true) {
$isCli = php_sapi_name() == 'cli';
if ($isCli) {
echo $text;
if ($newLine) echo "\n";
}
}
cliPrint('Starting benchmarks');
$html = '';
//Increasing these numbers will inprove accuracy but increase the time this script takes to run.
//Number of times to run each test before taking an average, the higher the better
$runs = 10;
//Number of iterations (HTTP requests to mimic) in each test. This will make each test longer
//use 100 or more for a more accurate result
$iterations = 250;
//Containers to be tested (dir names)
$containers = ['pimple', 'phalcon', 'dice', 'symfonydi'];
//Default ini file to use for tests
$defaultIni = getcwd() . DIRECTORY_SEPARATOR . 'php.ini';
$inis = array_fill_keys($containers, $defaultIni);
//Phalcon needs its own ini file to load the phalcon.so extension
$inis['phalcon'] = getcwd() . DIRECTORY_SEPARATOR . 'php-phalcon.ini';
$cwd = getcwd();
function average($array, $dp = 4) {
sort($array, SORT_NUMERIC);
$smallest = $array[0];
$num = 0;
$total = 0;
//Discard any values that were over 20% slower than the smallest as something likely happened to cause a blip in speed. A single
//slow result would skew the results using a standard mean.
foreach ($array as $val) {
if ($val <= $smallest * 1.2) {
$num++;
$total += $val;
}
}
return round($total / $num, $dp);
}
//Run a PHP script via exec, using the specified php.ini
function runScript($file, $iniFile, $args = []) {
exec('php -c ' . $iniFile . ' ' . $file . ' ' . implode(' ', $args), $output, $exitCode);
return $output;
}
$html .= '<h2>Test 6 - Scalability</h2>';
$html .= '<p>This test measures the entire script execution time for the PHP process to launch, construct/configure the container and then
have the container construct a specified number of objects. Fast containers with a slow startup time will score worse with fewer objects but improve in the rankings
as the number of objects is increased. Slower containers with fast startup times will rank highly with fewer objects but will lose out to faster containers once the number of objects gets high enough</p>
';
//Calculate the average overhead of running $iterations php scripts with a specified ini file
$blankScript = 'blank.php';
$overheads = [];
for ($i = 0; $i < $runs; $i++) {
$t1 = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
runScript($blankScript, $defaultIni);
}
$t2 = microtime(true);
$overheads[] = $t2 - $t1;
}
$overhead = average($overheads);
$html .= 'Overhead time: ' . $overhead;
$html .= '<table>';
$html .= '<thead><tr><th>Container</th>';
//The number of J objects on each iteration for the tests. Each J object consists of 10 objects in total
$objects = [1, 5, 10, 20, 50, 100, 150];
foreach ($objects as $object) {
$html .= '<th>' . $object *10 . ' objects</th>';
}
$html .= '</thead>';
cliPrint('Starting benchmark');
foreach ($containers as $container) {
cliPrint('Benchmarking container: ' . $container);
$html .= '<tr>';
$html .= '<td>' . $container .'</td>';
foreach ($objects as $objectcount) {
$results = [];
for ($i = 0; $i < $runs; $i++) {
cliPrint('Benchmarking container: ' . $container . ' with ' . $objectcount*10 . ' objects run ' . $i . '/' . $runs);
$t1 = microtime(true);
for ($j = 0; $j < $iterations; $j++) {
$output = runScript($container . '/test6a.php', $inis[$container], [$objectcount]);
}
$t2 = microtime(true);
$test = json_decode($output[0]);
if (!is_object($test)) print_r($output);
$results[] = $t2 - $t1;
}
$result = average($results);
$html .= '<td>' . ($result - $overhead) . '</td>';
}
$html .= '</tr>';
}
if (!$isCli) echo $html;
else file_put_contents('test6_results.html', $html);