Skip to content

Commit 155a7d9

Browse files
Update README with usage examples and detailed deviation explanations
1 parent 9da0c5c commit 155a7d9

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
3434
>
3535
> The result of the execution is printed to the console, so make sure you call the code from the console.
3636
37+
### Quick Start
38+
39+
```php
40+
use DragonCode\Benchmark\Benchmark;
41+
42+
new Benchmark()
43+
->compare(
44+
static fn () => true,
45+
static fn () => true
46+
)
47+
->toConsole();
48+
```
49+
50+
### How To Use
51+
3752
```php
3853
use DragonCode\Benchmark\Benchmark;
3954

@@ -160,6 +175,82 @@ Result example:
160175
+-------+----------------------+----------------------+
161176
```
162177

178+
### Deviation Values
179+
180+
In some cases, it is necessary to test not only the functionality of the options themselves, but also to determine the
181+
level of deviation between them. To do this, use the `deviations` method.
182+
183+
When you specify this method during a call, all your loops will repeat the specified number of times.
184+
Meanwhile, the resulting table and DTO will display values from the results, where:
185+
186+
- `min` - minimum among all result values
187+
- `max` - maximum among all results
188+
- `avg` - arithmetic mean of all result values
189+
- `total` - the total value among the values of all results
190+
- `deviation` - deviation calculated from the mean of all results
191+
192+
For example:
193+
194+
```php
195+
new Benchmark()
196+
->deviations(4)
197+
->iterations(5)
198+
->compare(
199+
foo: fn () => /* some code */,
200+
bar: fn () => /* some code */,
201+
)
202+
->toConsole();
203+
```
204+
205+
```bash
206+
+------------------+----------------------+-----------------------+
207+
| # | foo | bar |
208+
+------------------+----------------------+-----------------------+
209+
| min | 0.0011 ms - 0 bytes | 0.0009 ms - 0 bytes |
210+
| max | 0.0111 ms - 0 bytes | 0.0082 ms - 0 bytes |
211+
| avg | 0.00453 ms - 0 bytes | 0.002715 ms - 0 bytes |
212+
| total | 0.0906 ms - 0 bytes | 0.0543 ms - 0 bytes |
213+
+------------------+----------------------+-----------------------+
214+
| order | 2 | 1 |
215+
+------------------+----------------------+-----------------------+
216+
| deviation time | +0.002768 | +0.000919 |
217+
| deviation memory | 0 | 0 |
218+
+------------------+----------------------+-----------------------+
219+
```
220+
221+
#### To understand work without deviations
222+
223+
```php
224+
$iterations = 10;
225+
$result = [];
226+
227+
for ($i = 0; $i < $iterations; $i++) {
228+
$result[] = /* perform some code */;
229+
}
230+
231+
return $result;
232+
```
233+
234+
#### To understand work with deviations
235+
236+
```php
237+
$iterations = 10;
238+
$deviations = 4;
239+
$result = [];
240+
241+
for ($i = 0; $i < $deviations; $i++) {
242+
$insideResult = [];
243+
244+
for ($j = 0; $j < $iterations; $j++) {
245+
$insideResult[] = /* perform some code */;
246+
}
247+
248+
$result[] = $this->performResult($insideResult);
249+
}
250+
251+
return $result;
252+
```
253+
163254
### Callbacks
164255

165256
#### Before
@@ -401,6 +492,35 @@ new Benchmark()
401492
+-------+----------------------+----------------------+
402493
```
403494

495+
##### Option 5: With Deviation Values
496+
497+
```php
498+
new Benchmark()
499+
->deviations(4)
500+
->round(2)
501+
->compare(
502+
foo: static fn () => /* some code */,
503+
bar: static fn () => /* some code */,
504+
)
505+
->toConsole();
506+
```
507+
508+
```bash
509+
+------------------+-----------------------+---------------------+
510+
| # | 0 | 1 |
511+
+------------------+-----------------------+---------------------+
512+
| min | 15.68 ms - 202 bytes | 2.35 ms - 102 bytes |
513+
| max | 112.79 ms - 209 bytes | 9.76 ms - 109 bytes |
514+
| avg | 53.03 ms - 205 bytes | 5.94 ms - 105 bytes |
515+
| total | 1696.81 ms - 6.42 KB | 190.17 ms - 3.30 KB |
516+
+------------------+-----------------------+---------------------+
517+
| order | 2 | 1 |
518+
+------------------+-----------------------+---------------------+
519+
| deviation time | +0.100715 | +0.114023 |
520+
| deviation memory | 0 | 0 |
521+
+------------------+-----------------------+---------------------+
522+
```
523+
404524
#### toData
405525

406526
This method returns benchmark results as an array of `DragonCode\Benchmark\Data\ResultData` DTO objects.
@@ -457,6 +577,73 @@ array:2 [
457577
]
458578
```
459579

580+
##### With Deviation Values
581+
582+
When calling the benchmark with deviation calculation, the DTO will contain the `deviations` property:
583+
584+
```php
585+
return new Benchmark()
586+
->deviations()
587+
->compare(
588+
foo: fn () => /* some code */,
589+
bar: fn () => /* some code */,
590+
)
591+
->toData();
592+
```
593+
594+
```bash
595+
array:2 [
596+
"foo" => DragonCode\Benchmark\Data\ResultData {#23
597+
+min: DragonCode\Benchmark\Data\MetricData {#64
598+
+time: 0.001
599+
+memory: 0.0
600+
}
601+
+max: DragonCode\Benchmark\Data\MetricData {#65
602+
+time: 0.0036
603+
+memory: 0.0
604+
}
605+
+avg: DragonCode\Benchmark\Data\MetricData {#66
606+
+time: 0.0024209375
607+
+memory: 0.0
608+
}
609+
+total: DragonCode\Benchmark\Data\MetricData {#67
610+
+time: 0.7747
611+
+memory: 0.0
612+
}
613+
+deviation: DragonCode\Benchmark\Data\DeviationData {#68
614+
+percent: DragonCode\Benchmark\Data\MetricData {#69
615+
+time: 0.0007048383984778
616+
+memory: 0.0
617+
}
618+
}
619+
}
620+
"bar" => DragonCode\Benchmark\Data\ResultData {#70
621+
+min: DragonCode\Benchmark\Data\MetricData {#71
622+
+time: 0.001
623+
+memory: 0.0
624+
}
625+
+max: DragonCode\Benchmark\Data\MetricData {#72
626+
+time: 0.0032
627+
+memory: 0.0
628+
}
629+
+avg: DragonCode\Benchmark\Data\MetricData {#73
630+
+time: 0.00242875
631+
+memory: 0.0
632+
}
633+
+total: DragonCode\Benchmark\Data\MetricData {#74
634+
+time: 0.7772
635+
+memory: 0.0
636+
}
637+
+deviation: DragonCode\Benchmark\Data\DeviationData {#75
638+
+percent: DragonCode\Benchmark\Data\MetricData {#76
639+
+time: 0.00061642429076895
640+
+memory: 0.0
641+
}
642+
}
643+
}
644+
]
645+
```
646+
460647
#### toAssert
461648

462649
This method allows you to validate benchmark results against expected values.

0 commit comments

Comments
 (0)