Skip to content

Commit 526a0bf

Browse files
Refine deviation calculations to include percentage formatting, adjust precision, and update related tests.
1 parent 155a7d9 commit 526a0bf

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/Services/DeviationService.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ protected function deviationMetric(array $item): DeviationData
6060
$time = $this->result->values($item['avg'], 0, false);
6161
$memory = $this->result->values($item['avg'], 1, false);
6262

63+
$avg = $this->metric($item, 'avg');
64+
6365
return new DeviationData(
6466
percent: $this->metricData(
65-
$this->deviation($time),
66-
$this->deviation($memory),
67+
$this->percentage($avg->time, $this->deviation($time)),
68+
$this->percentage($avg->memory, $this->deviation($memory)),
6769
),
6870
);
6971
}
@@ -99,4 +101,17 @@ protected function deviation(array $values): float
99101

100102
return sqrt(array_sum($values) / count($values));
101103
}
104+
105+
protected function percentage(float $value1, float $value2): float
106+
{
107+
if (! $value1 && ! $value2) {
108+
return 0;
109+
}
110+
111+
if ($value1 === 0.0) {
112+
return INF;
113+
}
114+
115+
return ($value2 / $value1) * 100;
116+
}
102117
}

src/Transformers/ResultTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ protected function value(MetricData $metric): string
102102

103103
protected function deviation(float $value): string
104104
{
105-
$value = round($value, 6);
105+
$value = round($value, 2);
106106

107-
return ($value > 0 ? '+' : '') . $value;
107+
return ($value > 0 ? '+' : '') . $value . '%';
108108
}
109109

110110
protected function time(float $value): float

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
+------------------+-------------------------------+------------------------------+
1313
| order | 2 | 1 |
1414
+------------------+-------------------------------+------------------------------+
15-
| deviation time | 0 | 0 |
16-
| deviation memory | 0 | 0 |
15+
| deviation time | 0% | 0% |
16+
| deviation memory | 0% | 0% |
1717
+------------------+-------------------------------+------------------------------+

0 commit comments

Comments
 (0)