-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathBenchmarkCommand.php
More file actions
201 lines (176 loc) · 5.82 KB
/
BenchmarkCommand.php
File metadata and controls
201 lines (176 loc) · 5.82 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
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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since DebugKit 1.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Utility\Text;
/**
* Benchmark Command Class
*
* Provides basic benchmarking of application requests
* functionally similar to Apache AB
*
* @since DebugKit 1.0
*/
class BenchmarkCommand extends Command
{
/**
* @return string
*/
public static function getDescription(): string
{
return 'Benchmark a fully qualified URL.';
}
/**
* The console io
*/
protected ConsoleIo $io;
/**
* Execute.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$this->io = $io;
/** @var string $url */
$url = $args->getArgumentAt(0);
$times = [];
$io->out(Text::insert('-> Testing :url', compact('url')));
$io->out('');
$count = 10;
if ($args->hasOption('n')) {
$count = (float)$args->getOption('n');
}
$timeout = 100;
if ($args->hasOption('t')) {
$timeout = (float)$args->getOption('t');
}
for ($i = 0; $i < $count; $i++) {
if (floor($timeout - array_sum($times)) <= 0 || $count <= 1) {
break;
}
$start = microtime(true);
file_get_contents($url);
$stop = microtime(true);
$times[] = $stop - $start;
}
$this->_results($times);
return static::CODE_SUCCESS;
}
/**
* Prints calculated results
*
* @param array<float> $times Array of time values
* @return void
*/
protected function _results(array $times): void
{
$duration = array_sum($times);
$requests = count($times);
$this->io->out(Text::insert('Total Requests made: :requests', compact('requests')));
$this->io->out(Text::insert('Total Time elapsed: :duration (seconds)', compact('duration')));
$this->io->out('');
$this->io->out(Text::insert('Requests/Second: :rps req/sec', [
'rps' => round($requests / $duration, 3),
]));
$this->io->out(Text::insert('Average request time: :average-time seconds', [
'average-time' => round($duration / $requests, 3),
]));
$this->io->out(Text::insert('Standard deviation of average request time: :std-dev', [
'std-dev' => round($this->_deviation($times, true), 3),
]));
if (!empty($times)) {
$this->io->out(Text::insert('Longest/shortest request: :longest sec/:shortest sec', [
'longest' => round(max($times), 3),
'shortest' => round(min($times), 3),
]));
}
$this->io->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(array $times, bool $sample = true): float
{
$n = $mean = $M2 = 0;
foreach ($times as $time) {
$n += 1;
$delta = $time - $mean;
$mean += $delta / $n;
$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(array $times, bool $sample = true): float
{
return sqrt($this->_variance($times, $sample));
}
/**
* Gets the option parser instance and configures it.
*
* @param \Cake\Console\ConsoleOptionParser $parser The parser to build
* @return \Cake\Console\ConsoleOptionParser
*/
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser->setDescription(
'Allows you to obtain some rough benchmarking statistics' .
'about a fully qualified URL.',
)
->addArgument('url', [
'help' => 'The URL to request.',
'required' => true,
])
->addOption('n', [
'default' => '10',
'help' => 'Number of iterations to perform.',
])
->addOption('t', [
'default' => '100',
'help' =>
'Maximum total time for all iterations, in seconds. ' .
'If a single iteration takes more than the timeout, only one request will be made',
])
->setEpilog(
'Example Use: `cake benchmark --n 10 --t 100 http://localhost/testsite`. ' .
'<info>Note:</info> this benchmark does not include browser render times.',
);
return $parser;
}
}