BenchmarkShell.php
5.3 KB
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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since DebugKit 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Shell;
use Cake\Console\Shell;
use Cake\Utility\Text;
/**
* Benchmark Shell Class
*
* Provides basic benchmarking of application requests
* functionally similar to Apache AB
*
* @since DebugKit 1.0
* @todo Print/export time detail information
* @todo Export/graphing of data to .dot format for graphviz visualization
* @todo Make calculated results round to leading significant digit position of std dev.
*/
class BenchmarkShell extends Shell
{
/**
* Main execution of shell
*
* @return void
*/
public function main()
{
$url = $this->args[0];
$defaults = ['t' => 100, 'n' => 10];
$options = array_merge($defaults, $this->params);
$times = [];
$this->out(Text::insert(__d('debug_kit', '-> Testing :url'), compact('url')));
$this->out("");
for ($i = 0; $i < $options['n']; $i++) {
if (floor($options['t'] - array_sum($times)) <= 0 || $options['n'] <= 1) {
break;
}
$start = microtime(true);
file_get_contents($url);
$stop = microtime(true);
$times[] = $stop - $start;
}
$this->_results($times);
}
/**
* Prints calculated results
*
* @param array $times Array of time values
* @return void
*/
protected function _results($times)
{
$duration = array_sum($times);
$requests = count($times);
$this->out(Text::insert(__d('debug_kit', 'Total Requests made: :requests'), compact('requests')));
$this->out(Text::insert(__d('debug_kit', 'Total Time elapsed: :duration (seconds)'), compact('duration')));
$this->out("");
$this->out(Text::insert(__d('debug_kit', 'Requests/Second: :rps req/sec'), [
'rps' => round($requests / $duration, 3)
]));
$this->out(Text::insert(__d('debug_kit', 'Average request time: :average-time seconds'), [
'average-time' => round($duration / $requests, 3)
]));
$this->out(Text::insert(__d('debug_kit', 'Standard deviation of average request time: :std-dev'), [
'std-dev' => round($this->_deviation($times, true), 3)
]));
$this->out(Text::insert(__d('debug_kit', 'Longest/shortest request: :longest sec/:shortest sec'), [
'longest' => round(max($times), 3),
'shortest' => round(min($times), 3)
]));
$this->out("");
}
/**
* One-pass, numerically stable calculation of population variance.
*
* Donald E. Knuth (1998).
* The Art of Computer Programming, volume 2: Seminumerical Algorithms, 3rd edn.,
* p. 232. Boston: Addison-Wesley.
*
* @param array $times Array of values
* @param bool $sample If true, calculates an unbiased estimate of the population
* variance from a finite sample.
* @return float Variance
*/
protected function _variance($times, $sample = true)
{
$n = $mean = $M2 = 0;
foreach ($times as $time) {
$n += 1;
$delta = $time - $mean;
$mean = $mean + $delta / $n;
$M2 = $M2 + $delta * ($time - $mean);
}
if ($sample) {
$n -= 1;
}
return $M2 / $n;
}
/**
* Calculate the standard deviation.
*
* @param array $times Array of values
* @param bool $sample ''
* @return float Standard deviation
*/
protected function _deviation($times, $sample = true)
{
return sqrt($this->_variance($times, $sample));
}
/**
* Get option parser.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->description(__d(
'debug_kit',
'Allows you to obtain some rough benchmarking statistics' .
'about a fully qualified URL.'
))
->addArgument('url', [
'help' => __d('debug_kit', 'The URL to request.'),
'required' => true
])
->addOption('n', [
'default' => 10,
'help' => __d('debug_kit', 'Number of iterations to perform.')
])
->addOption('t', [
'default' => 100,
'help' => __d(
'debug_kit',
'Maximum total time for all iterations, in seconds.' .
'If a single iteration takes more than the timeout, only one request will be made'
)
])
->epilog(__d(
'debug_kit',
'Example Use: `cake benchmark --n 10 --t 100 http://localhost/testsite`. ' .
'<info>Note:</info> this benchmark does not include browser render times.'
));
return $parser;
}
}