-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest1-5_runner.php
131 lines (97 loc) · 3.71 KB
/
test1-5_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
<?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 = '';
//Number of times to run each test before taking an average
$runs = 10;
cliPrint('Running each test ' . $runs . ' times');
//Containers to be tested (dir names)
$containers = ['aura', 'auryn', 'dice', 'laravel', 'league', 'njasm', 'phalcon', 'php-di', 'pimple', 'symfonydi', 'zend-di', 'zend-servicemanager'];
//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();
//The number of tests
$numTests = 5;
cliPrint('Running tests 1 - ' . $numTests);
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;
}
//Some very basic styling
$html .= '<style>td,th {padding: 5px; border: 1px solid #aaa; text-align: right;}</style>';
$testdescriptions = [1 => 'Create single object (incl autoload time)',
2 => 'Create single object (excl autoload time)',
3 => 'Create deep object graph',
4 => 'Fetch the same instance (service) from the container repeatedly',
5 => 'Inject a service into a new object repeatedly'
];
for ($test = 1; $test <= $numTests; $test++) {
$html .= '<h2>Test ' . $test . ' - ' . $testdescriptions[$test] . '</h2>';
$html .= '<table>';
cliPrint('Starting test:' . $test);
$containerInfo = [];
$html .= '<thead><tr><th>Container</th><th>Time</th><th>Memory</th><th>Files</th></thead>';
foreach ($containers as $container) {
cliPrint('');
cliPrint('Benchmarking container:' . $container);
$memory = [];
$time = [];
$files = [];
$output = [];
for ($i = 0; $i < $runs; $i++) {
cliPrint($container . ' test' . $test . ' : ' . ($i+1) . '/' . $runs);
$output = runScript('./' . $container . '/test' . $test . '.php', $inis[$container]);
$result = json_decode($output[0]);
if (!is_object($result)) echo $container . $test . '<br />';
$time[] = $result->time;
$memory[] = $result->memory;
$files[] = $result->files;
}
$containerInfo[] = ['name' => $container, 'time' => average($time), 'memory' => average($memory), 'files' => average($files)];
}
//Sort the results by time
usort($containerInfo, function($a, $b) {
if ($a['time'] == $b['time']) return ($a['memory'] < $b['memory']) ? -1 : 1;
return ($a['time'] < $b['time']) ? -1 : 1;
});
foreach ($containerInfo as $containerDetail) {
$html .= '<tr>';
$html .= '<td>' . $containerDetail['name'] .'</td>';
$html .= '<td>' . $containerDetail['time'] . '</td>';
$html .= '<td>' . $containerDetail['memory'] . '</td>';
$html .= '<td>' . $containerDetail['files'] . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
}
if (!$isCli) echo $html;
else file_put_contents('test1-5_results.html', $html);