Skip to content

Commit 9da0c5c

Browse files
Refactor deviation calculations and enhance related tests
1 parent 935daaa commit 9da0c5c

6 files changed

Lines changed: 52 additions & 35 deletions

File tree

src/Data/DeviationData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
readonly class DeviationData
88
{
99
public function __construct(
10-
public MetricData $avg,
10+
public MetricData $percent,
1111
) {}
1212
}

src/Services/DeviationService.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use DragonCode\Benchmark\Data\ResultData;
1010

1111
use function array_map;
12+
use function array_sum;
13+
use function count;
1214
use function sqrt;
1315

1416
class DeviationService
@@ -55,58 +57,46 @@ protected function metric(array $item, string $key): MetricData
5557

5658
protected function deviationMetric(array $item): DeviationData
5759
{
60+
$time = $this->result->values($item['avg'], 0, false);
61+
$memory = $this->result->values($item['avg'], 1, false);
62+
5863
return new DeviationData(
59-
avg: $this->result->avg(
60-
$this->result->values($item['deviation'], 0, false),
61-
$this->result->values($item['deviation'], 1, false),
64+
percent: $this->metricData(
65+
$this->deviation($time),
66+
$this->deviation($memory),
6267
),
6368
);
6469
}
6570

71+
protected function metricData(float $time, float $memory): MetricData
72+
{
73+
return new MetricData($time, $memory);
74+
}
75+
6676
protected function flatten(array $collection): array
6777
{
68-
$default = [];
69-
$result = [];
78+
$result = [];
7079

71-
foreach ($collection as $i => $items) {
80+
foreach ($collection as $items) {
7281
foreach ($items as $key => $item) {
7382
$result[$key]['min'][] = [$item->min->time, $item->min->memory];
7483
$result[$key]['max'][] = [$item->max->time, $item->max->memory];
7584
$result[$key]['avg'][] = [$item->avg->time, $item->avg->memory];
7685
$result[$key]['total'][] = [$item->total->time, $item->total->memory];
77-
78-
if ($i === 0) {
79-
$default[$key] = [$item->avg->time, $item->avg->memory];
80-
81-
continue;
82-
}
83-
84-
$result[$key]['deviation'][] = [
85-
$this->percentage($default[$key][0], $this->deviation($default[$key][0], $item->avg->time)),
86-
$this->percentage($default[$key][1], $this->deviation($default[$key][1], $item->avg->memory)),
87-
];
8886
}
8987
}
9088

9189
return $result;
9290
}
9391

94-
protected function deviation(float $first, float $second): float
92+
protected function deviation(array $values): float
9593
{
96-
$avg = ($first + $second) / 2;
97-
98-
$deviation1 = ($first - $avg) ** 2;
99-
$deviation2 = ($second - $avg) ** 2;
94+
$avg = array_sum($values) / count($values);
10095

101-
return sqrt(($deviation1 + $deviation2) / 2);
102-
}
103-
104-
protected function percentage(float $reference, float $value): float
105-
{
106-
if ($reference === 0.0) {
107-
return 0;
96+
foreach ($values as &$value) {
97+
$value = ($value - $avg) ** 2;
10898
}
10999

110-
return ($value - $reference) / $reference * 100;
100+
return sqrt(array_sum($values) / count($values));
111101
}
112102
}

src/Transformers/ResultTransformer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function map(array $table, array $collection): array
5454
$table[3][$key] = $this->value($item->total);
5555
$table[5][$key] = 0;
5656

57-
if (! $deviation = $item->deviation?->avg) {
57+
if (! $deviation = $item->deviation?->percent) {
5858
continue;
5959
}
6060

@@ -100,9 +100,11 @@ protected function value(MetricData $metric): string
100100
: sprintf('%s ms', $time);
101101
}
102102

103-
protected function deviation(float $value): ?string
103+
protected function deviation(float $value): string
104104
{
105-
return ($value > 0 ? '+' : '') . round($value, 2) . '%';
105+
$value = round($value, 6);
106+
107+
return ($value > 0 ? '+' : '') . $value;
106108
}
107109

108110
protected function time(float $value): float

tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap renamed to tests/.pest/snapshots/Unit/Result/ToConsoleTest/compare.snap

File renamed without changes.

tests/.pest/snapshots/Unit/Result/ToConsoleTest/deviations.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
5+
+------------------+-------------------------------+------------------------------+
6+
| # | 0 | 1 |
7+
+------------------+-------------------------------+------------------------------+
8+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
9+
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
10+
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
11+
| total | 848.40397522 ms - 3.21 KB | 95.08640394 ms - 1.65 KB |
12+
+------------------+-------------------------------+------------------------------+
13+
| order | 2 | 1 |
14+
+------------------+-------------------------------+------------------------------+
15+
| deviation time | 0 | 0 |
16+
| deviation memory | 0 | 0 |
17+
+------------------+-------------------------------+------------------------------+

tests/Unit/Result/ToConsoleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
declare(strict_types=1);
44

5-
test('output', function () {
5+
test('compare', function () {
66
benchmark()->toConsole();
77

88
expectOutputToMatchSnapshot();
99
});
10+
11+
test('deviations', function () {
12+
benchmark()
13+
->deviations()
14+
->toConsole();
15+
16+
expectOutputToMatchSnapshot();
17+
});

0 commit comments

Comments
 (0)