Skip to content

Commit e67346e

Browse files
Merge pull request #102
Added doc-blocks
2 parents c5eb885 + 985a218 commit e67346e

21 files changed

Lines changed: 708 additions & 5 deletions

src/Benchmark.php

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,22 @@ public function __construct(
3636
protected DeviationService $deviation = new DeviationService,
3737
) {}
3838

39+
/**
40+
* Creates a new benchmark instance.
41+
*
42+
* @return static
43+
*/
3944
public static function make(): static
4045
{
4146
return new static;
4247
}
4348

4449
/**
50+
* Sets a callback to be executed before all iterations for each comparison.
51+
*
4552
* @param Closure(int|string $name): mixed $callback
53+
*
54+
* @return $this
4655
*/
4756
public function before(Closure $callback): static
4857
{
@@ -52,7 +61,11 @@ public function before(Closure $callback): static
5261
}
5362

5463
/**
64+
* Sets a callback to be executed before each iteration.
65+
*
5566
* @param Closure(int|string $name, int<1, max> $iteration): mixed $callback
67+
*
68+
* @return $this
5669
*/
5770
public function beforeEach(Closure $callback): static
5871
{
@@ -62,7 +75,11 @@ public function beforeEach(Closure $callback): static
6275
}
6376

6477
/**
78+
* Sets a callback to be executed after all iterations for each comparison.
79+
*
6580
* @param Closure(int|string $name): mixed $callback
81+
*
82+
* @return $this
6683
*/
6784
public function after(Closure $callback): static
6885
{
@@ -72,7 +89,11 @@ public function after(Closure $callback): static
7289
}
7390

7491
/**
92+
* Sets a callback to be executed after each iteration.
93+
*
7594
* @param Closure(int|string $name, int<1, max> $iteration, float $time, float $memory): mixed $callback
95+
*
96+
* @return $this
7697
*/
7798
public function afterEach(Closure $callback): static
7899
{
@@ -82,7 +103,11 @@ public function afterEach(Closure $callback): static
82103
}
83104

84105
/**
106+
* Sets the number of iterations for each comparison.
107+
*
85108
* @param int<1, max> $count
109+
*
110+
* @return $this
86111
*/
87112
public function iterations(int $count): static
88113
{
@@ -92,7 +117,11 @@ public function iterations(int $count): static
92117
}
93118

94119
/**
120+
* Enables deviation calculation and sets the number of runs.
121+
*
95122
* @param int<2, max> $count
123+
*
124+
* @return $this
96125
*/
97126
public function deviations(int $count = 2): static
98127
{
@@ -104,7 +133,11 @@ public function deviations(int $count = 2): static
104133
}
105134

106135
/**
107-
* @param int<0, max>|null $precision
136+
* Sets the rounding precision for time values.
137+
*
138+
* @param int<0, max>|null $precision The number of decimal places. Null means no rounding.
139+
*
140+
* @return $this
108141
*/
109142
public function round(?int $precision): static
110143
{
@@ -113,13 +146,25 @@ public function round(?int $precision): static
113146
return $this;
114147
}
115148

149+
/**
150+
* Disables the progress bar display.
151+
*
152+
* @return $this
153+
*/
116154
public function disableProgressBar(): static
117155
{
118156
$this->view->progressBar()->disable();
119157

120158
return $this;
121159
}
122160

161+
/**
162+
* Registers callback functions for comparison.
163+
*
164+
* @param array|Closure ...$callbacks Callback functions or an array of callback functions for comparison.
165+
*
166+
* @return $this
167+
*/
123168
public function compare(array|Closure ...$callbacks): static
124169
{
125170
$this->clear();
@@ -130,6 +175,8 @@ public function compare(array|Closure ...$callbacks): static
130175
}
131176

132177
/**
178+
* Returns benchmark results as an array of data.
179+
*
133180
* @return Data\ResultData[]
134181
*/
135182
public function toData(): array
@@ -141,6 +188,9 @@ public function toData(): array
141188
return $this->mapResult();
142189
}
143190

191+
/**
192+
* Outputs benchmark results to the console as a table.
193+
*/
144194
public function toConsole(): void
145195
{
146196
if (! $data = $this->toData()) {
@@ -154,6 +204,11 @@ public function toConsole(): void
154204
);
155205
}
156206

207+
/**
208+
* Returns the assertion service for performing result checks.
209+
*
210+
* @return AssertService
211+
*/
157212
public function toAssert(): AssertService
158213
{
159214
if (! $data = $this->toData()) {
@@ -163,20 +218,31 @@ public function toAssert(): AssertService
163218
return new AssertService($data);
164219
}
165220

221+
/**
222+
* Performs the benchmark: simple comparison or with deviation calculation.
223+
*/
166224
protected function perform(): void
167225
{
168226
$this->deviations === 1
169227
? $this->performCompare()
170228
: $this->performDeviation();
171229
}
172230

231+
/**
232+
* Transforms collected data into an array of results.
233+
*
234+
* @return array
235+
*/
173236
protected function mapResult(): array
174237
{
175238
return $this->result->get(
176239
$this->collector->all()
177240
);
178241
}
179242

243+
/**
244+
* Performs a simple comparison of callback functions.
245+
*/
180246
protected function performCompare(): void
181247
{
182248
$callbacks = $this->callbacks->compare;
@@ -187,6 +253,9 @@ protected function performCompare(): void
187253
);
188254
}
189255

256+
/**
257+
* Performs a comparison with deviation calculation through multiple runs.
258+
*/
190259
protected function performDeviation(): void
191260
{
192261
$results = [];
@@ -210,6 +279,12 @@ protected function performDeviation(): void
210279
);
211280
}
212281

282+
/**
283+
* Wraps execution in a progress bar.
284+
*
285+
* @param Closure $callback The callback function to execute.
286+
* @param int $total The total number of progress bar steps.
287+
*/
213288
protected function withProgress(Closure $callback, int $total): void
214289
{
215290
$this->view->emptyLine();
@@ -222,11 +297,25 @@ protected function withProgress(Closure $callback, int $total): void
222297
$this->view->emptyLine(2);
223298
}
224299

300+
/**
301+
* Calculates the total number of steps for the progress bar.
302+
*
303+
* @param array $callbacks An array of callback functions.
304+
* @param int $multiplier The multiplier (number of runs).
305+
*
306+
* @return int
307+
*/
225308
protected function steps(array $callbacks, int $multiplier = 1): int
226309
{
227310
return count($callbacks) * $this->iterations * $multiplier;
228311
}
229312

313+
/**
314+
* Executes all callback functions with before/after hooks.
315+
*
316+
* @param array $callbacks An array of callback functions.
317+
* @param ProgressBarView $progressBar The progress bar.
318+
*/
230319
protected function chunks(array $callbacks, ProgressBarView $progressBar): void
231320
{
232321
foreach ($callbacks as $name => $callback) {
@@ -238,6 +327,13 @@ protected function chunks(array $callbacks, ProgressBarView $progressBar): void
238327
}
239328
}
240329

330+
/**
331+
* Executes all iterations for a single callback function.
332+
*
333+
* @param mixed $name The callback name.
334+
* @param Closure $callback The callback function to execute.
335+
* @param ProgressBarView $progressBar The progress bar.
336+
*/
241337
protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void
242338
{
243339
for ($i = 1; $i <= $this->iterations; $i++) {
@@ -253,16 +349,34 @@ protected function run(mixed $name, Closure $callback, ProgressBarView $progress
253349
}
254350
}
255351

352+
/**
353+
* Calls a callback function and returns the measurement results.
354+
*
355+
* @param Closure $callback The callback function to execute.
356+
* @param array $parameters Parameters to pass to the callback.
357+
*
358+
* @return array An array [time in milliseconds, memory in bytes].
359+
*/
256360
protected function call(Closure $callback, array $parameters = []): array
257361
{
258362
return $this->runner->call($callback, $parameters);
259363
}
260364

365+
/**
366+
* Stores measurement results in the collector.
367+
*
368+
* @param mixed $name The callback name.
369+
* @param float $time Execution time is specified in milliseconds.
370+
* @param float $memory Memory usage is specified in bytes.
371+
*/
261372
protected function push(mixed $name, float $time, float $memory): void
262373
{
263374
$this->collector->push($name, [$time, $memory]);
264375
}
265376

377+
/**
378+
* Clears results and collected data.
379+
*/
266380
protected function clear(): void
267381
{
268382
$this->result->clear();

src/Data/DeviationData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
readonly class DeviationData
88
{
9+
/**
10+
* Creates an object with deviation data.
11+
*
12+
* @param MetricData $percent Deviation metrics are specified in percentages.
13+
*/
914
public function __construct(
1015
public MetricData $percent,
1116
) {}

src/Data/MetricData.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
readonly class MetricData
88
{
9+
/**
10+
* Creates an object with time and memory metrics.
11+
*
12+
* When the metric is used to store deviations, the deviations are specified in percentages.
13+
*
14+
* @param float $time Time value is specified in milliseconds.
15+
* @param float $memory Memory value is specified in bytes.
16+
*/
917
public function __construct(
1018
public float $time,
1119
public float $memory,

src/Data/ResultData.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
readonly class ResultData
88
{
9+
/**
10+
* Creates an object with benchmark results.
11+
*
12+
* @param MetricData $min Minimum metric values.
13+
* @param MetricData $max Maximum metric values.
14+
* @param MetricData $avg Average metric values.
15+
* @param MetricData $total Total metric values.
16+
* @param DeviationData|null $deviation Deviation data.
17+
*/
918
public function __construct(
1019
public MetricData $min,
1120
public MetricData $max,

src/Exceptions/DeviationsNotCalculatedException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
class DeviationsNotCalculatedException extends ValueError
1212
{
13+
/**
14+
* Creates an exception indicating that deviations were not calculated for the specified callback name.
15+
*
16+
* @param int|string $name The name of the callback for which deviation data is missing.
17+
*/
1318
public function __construct(int|string $name)
1419
{
1520
parent::__construct(

src/Exceptions/NoComparisonsException.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class NoComparisonsException extends RuntimeException
1010
{
11+
/**
12+
* Creates an exception indicating that the "compare" method was not called.
13+
*/
1114
public function __construct()
1215
{
1316
parent::__construct('Method "compare" was not called. No comparisons were made.');

src/Exceptions/ValueIsNotCallableException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010

1111
class ValueIsNotCallableException extends TypeError
1212
{
13+
/**
14+
* Creates an exception indicating that the provided value does not match the Closure type.
15+
*
16+
* @param mixed $value The value that does not match the Closure type.
17+
*/
1318
public function __construct(mixed $value)
1419
{
15-
parent::__construct(sprintf('The property value must be of type Closure, %s given.', gettype($value)), 500);
20+
parent::__construct(sprintf('The property value must be of type Closure, %s given.', gettype($value)));
1621
}
1722
}

0 commit comments

Comments
 (0)