Skip to content

Commit 935daaa

Browse files
Expand deviation handling in ResultTransformer and services.
1 parent 0a74bbc commit 935daaa

6 files changed

Lines changed: 78 additions & 19 deletions

File tree

src/Data/DeviationData.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Data;
6+
7+
readonly class DeviationData
8+
{
9+
public function __construct(
10+
public MetricData $avg,
11+
) {}
12+
}

src/Data/ResultData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public function __construct(
1111
public MetricData $max,
1212
public MetricData $avg,
1313
public MetricData $total,
14+
public ?DeviationData $deviation = null,
1415
) {}
1516
}

src/Services/DeviationService.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DragonCode\Benchmark\Services;
66

7+
use DragonCode\Benchmark\Data\DeviationData;
78
use DragonCode\Benchmark\Data\MetricData;
89
use DragonCode\Benchmark\Data\ResultData;
910

@@ -30,18 +31,37 @@ public function calculate(array $collection): array
3031

3132
protected function map(array $collection): array
3233
{
33-
return array_map(fn (array $item) => $this->make($item), $collection);
34+
return array_map(fn (array $item): ResultData => $this->make($item), $collection);
3435
}
3536

3637
protected function make(array $item): ResultData
3738
{
38-
dd($item);
3939
return new ResultData(
40-
min: $this->metric()
40+
min : $this->metric($item, 'min'),
41+
max : $this->metric($item, 'max'),
42+
avg : $this->metric($item, 'avg'),
43+
total : $this->metric($item, 'total'),
44+
deviation: $this->deviationMetric($item),
4145
);
4246
}
4347

44-
protected function metric(float $time, float $memory): MetricData {}
48+
protected function metric(array $item, string $key): MetricData
49+
{
50+
return $this->result->$key(
51+
$this->result->values($item[$key], 0, false),
52+
$this->result->values($item[$key], 1, false),
53+
);
54+
}
55+
56+
protected function deviationMetric(array $item): DeviationData
57+
{
58+
return new DeviationData(
59+
avg: $this->result->avg(
60+
$this->result->values($item['deviation'], 0, false),
61+
$this->result->values($item['deviation'], 1, false),
62+
),
63+
);
64+
}
4565

4666
protected function flatten(array $collection): array
4767
{
@@ -62,8 +82,8 @@ protected function flatten(array $collection): array
6282
}
6383

6484
$result[$key]['deviation'][] = [
65-
$this->deviation($default[$key][0], $item->avg->time),
66-
$this->deviation($default[$key][1], $item->avg->memory),
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)),
6787
];
6888
}
6989
}
@@ -80,4 +100,13 @@ protected function deviation(float $first, float $second): float
80100

81101
return sqrt(($deviation1 + $deviation2) / 2);
82102
}
103+
104+
protected function percentage(float $reference, float $value): float
105+
{
106+
if ($reference === 0.0) {
107+
return 0;
108+
}
109+
110+
return ($value - $reference) / $reference * 100;
111+
}
83112
}

src/Services/ResultService.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function force(array $collection): void
3535
/**
3636
* @return ResultData[]
3737
*/
38-
public function get(array $collections, bool $filterable = true): array
38+
public function get(array $collections): array
3939
{
40-
return $this->data ??= $this->map($collections, $filterable);
40+
return $this->data ??= $this->map($collections);
4141
}
4242

4343
public function clear(): void
@@ -48,21 +48,21 @@ public function clear(): void
4848
/**
4949
* @return ResultData[]
5050
*/
51-
public function map(array $collections, bool $filterable = true): array
51+
public function map(array $collections): array
5252
{
53-
return array_map(function (array $data) use ($filterable) {
53+
return array_map(function (array $data): ResultData {
5454
return $this->collect(
55-
$this->values($data, 0, $filterable),
56-
$this->values($data, 1, $filterable)
55+
$this->values($data, 0),
56+
$this->values($data, 1)
5757
);
5858
}, $collections);
5959
}
6060

61-
protected function values(array $data, int $column, bool $filterable): array
61+
public function values(array $data, int $column, bool $filter = true): array
6262
{
6363
$values = array_column($data, $column);
6464

65-
return $filterable ? $this->filter($values) : $values;
65+
return $filter ? $this->filter($values) : $values;
6666
}
6767

6868
protected function collect(array $times, array $memory): ResultData
@@ -75,31 +75,31 @@ protected function collect(array $times, array $memory): ResultData
7575
);
7676
}
7777

78-
protected function min(array $times, array $memory): MetricData
78+
public function min(array $times, array $memory): MetricData
7979
{
8080
return $this->metric(
8181
time : min($times),
8282
memory: min($memory),
8383
);
8484
}
8585

86-
protected function max(array $times, array $memory): MetricData
86+
public function max(array $times, array $memory): MetricData
8787
{
8888
return $this->metric(
8989
time : max($times),
9090
memory: max($memory),
9191
);
9292
}
9393

94-
protected function avg(array $times, array $memory): MetricData
94+
public function avg(array $times, array $memory): MetricData
9595
{
9696
return $this->metric(
9797
time : array_sum($times) / count($times),
9898
memory: array_sum($memory) / count($memory),
9999
);
100100
}
101101

102-
protected function total(array $times, array $memory): MetricData
102+
public function total(array $times, array $memory): MetricData
103103
{
104104
return $this->metric(
105105
time : array_sum($times),

src/Transformers/ResultTransformer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ protected function map(array $table, array $collection): array
5353
$table[2][$key] = $this->value($item->avg);
5454
$table[3][$key] = $this->value($item->total);
5555
$table[5][$key] = 0;
56+
57+
if (! $deviation = $item->deviation?->avg) {
58+
continue;
59+
}
60+
61+
$table[6] = [null];
62+
63+
$table[7]['#'] = 'deviation time';
64+
$table[8]['#'] = 'deviation memory';
65+
66+
$table[7][$key] = $this->deviation($deviation->time);
67+
$table[8][$key] = $this->deviation($deviation->memory);
5668
}
5769

5870
return $table;
@@ -88,6 +100,11 @@ protected function value(MetricData $metric): string
88100
: sprintf('%s ms', $time);
89101
}
90102

103+
protected function deviation(float $value): ?string
104+
{
105+
return ($value > 0 ? '+' : '') . round($value, 2) . '%';
106+
}
107+
91108
protected function time(float $value): float
92109
{
93110
if ($this->precision === null) {

src/View/TableView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function headers(array $data): array
4343

4444
protected function columnWidths(array $headers, array $data): array
4545
{
46-
$widths = array_map(fn ($header) => mb_strlen((string) $header), $headers);
46+
$widths = array_map(static fn ($header) => mb_strlen((string) $header), $headers);
4747

4848
foreach ($data as $row) {
4949
foreach (array_values($row) as $i => $value) {

0 commit comments

Comments
 (0)