Skip to content

Commit f46a845

Browse files
Merge pull request #106
Refactor `AssertService`
2 parents 9331be8 + 8a3dfe3 commit f46a845

1 file changed

Lines changed: 48 additions & 78 deletions

File tree

src/Services/AssertService.php

Lines changed: 48 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace DragonCode\Benchmark\Services;
66

77
use AssertionError;
8+
use Closure;
9+
use DragonCode\Benchmark\Data\ResultData;
810
use DragonCode\Benchmark\Exceptions\DeviationsNotCalculatedException;
911

1012
class AssertService
@@ -23,194 +25,162 @@ public function __construct(
2325
*
2426
* @param float|null $from Start value is specified in milliseconds.
2527
* @param float|null $till End value is specified in milliseconds.
28+
*
2629
* @return $this
2730
*/
2831
public function toBeMinTime(?float $from = null, ?float $till = null): static
2932
{
30-
$from = $this->resolveFrom($from, $till);
31-
32-
foreach ($this->result as $item) {
33-
$this->assertGreaterThan($item->min->time, $from, 'minimum time');
34-
$this->assertLessThan($item->min->time, $till, 'minimum time');
35-
}
36-
37-
return $this;
33+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->min->time, 'minimum time');
3834
}
3935

4036
/**
4137
* Asserts that the maximum execution time is within the specified range.
4238
*
4339
* @param float|null $from Start value is specified in milliseconds.
4440
* @param float|null $till End value is specified in milliseconds.
41+
*
4542
* @return $this
4643
*/
4744
public function toBeMaxTime(?float $from = null, ?float $till = null): static
4845
{
49-
$from = $this->resolveFrom($from, $till);
50-
51-
foreach ($this->result as $item) {
52-
$this->assertGreaterThan($item->max->time, $from, 'maximum time');
53-
$this->assertLessThan($item->max->time, $till, 'maximum time');
54-
}
55-
56-
return $this;
46+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->max->time, 'maximum time');
5747
}
5848

5949
/**
6050
* Asserts that the average execution time is within the specified range.
6151
*
6252
* @param float|null $from Start value is specified in milliseconds.
6353
* @param float|null $till End value is specified in milliseconds.
54+
*
6455
* @return $this
6556
*/
6657
public function toBeAvgTime(?float $from = null, ?float $till = null): static
6758
{
68-
$from = $this->resolveFrom($from, $till);
69-
70-
foreach ($this->result as $item) {
71-
$this->assertGreaterThan($item->avg->time, $from, 'average time');
72-
$this->assertLessThan($item->avg->time, $till, 'average time');
73-
}
74-
75-
return $this;
59+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->avg->time, 'average time');
7660
}
7761

7862
/**
7963
* Asserts that the total execution time is within the specified range.
8064
*
8165
* @param float|null $from Start value is specified in milliseconds.
8266
* @param float|null $till End value is specified in milliseconds.
67+
*
8368
* @return $this
8469
*/
8570
public function toBeTotalTime(?float $from = null, ?float $till = null): static
8671
{
87-
$from = $this->resolveFrom($from, $till);
88-
89-
foreach ($this->result as $item) {
90-
$this->assertGreaterThan($item->total->time, $from, 'total time');
91-
$this->assertLessThan($item->total->time, $till, 'total time');
92-
}
93-
94-
return $this;
72+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->total->time, 'total time');
9573
}
9674

9775
/**
9876
* Asserts that the minimum memory usage is within the specified range.
9977
*
10078
* @param float|null $from Start value is specified in bytes.
10179
* @param float|null $till End value is specified in bytes.
80+
*
10281
* @return $this
10382
*/
10483
public function toBeMinMemory(?float $from = null, ?float $till = null): static
10584
{
106-
$from = $this->resolveFrom($from, $till);
107-
108-
foreach ($this->result as $item) {
109-
$this->assertGreaterThan($item->min->memory, $from, 'minimum memory');
110-
$this->assertLessThan($item->min->memory, $till, 'minimum memory');
111-
}
112-
113-
return $this;
85+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->min->memory, 'minimum memory');
11486
}
11587

11688
/**
11789
* Asserts that the maximum memory usage is within the specified range.
11890
*
11991
* @param float|null $from Start value is specified in bytes.
12092
* @param float|null $till End value is specified in bytes.
93+
*
12194
* @return $this
12295
*/
12396
public function toBeMaxMemory(?float $from = null, ?float $till = null): static
12497
{
125-
$from = $this->resolveFrom($from, $till);
126-
127-
foreach ($this->result as $item) {
128-
$this->assertGreaterThan($item->max->memory, $from, 'maximum memory');
129-
$this->assertLessThan($item->max->memory, $till, 'maximum memory');
130-
}
131-
132-
return $this;
98+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->max->memory, 'maximum memory');
13399
}
134100

135101
/**
136102
* Asserts that the average memory usage is within the specified range.
137103
*
138104
* @param float|null $from Start value is specified in bytes.
139105
* @param float|null $till End value is specified in bytes.
106+
*
140107
* @return $this
141108
*/
142109
public function toBeAvgMemory(?float $from = null, ?float $till = null): static
143110
{
144-
$from = $this->resolveFrom($from, $till);
145-
146-
foreach ($this->result as $item) {
147-
$this->assertGreaterThan($item->avg->memory, $from, 'average memory');
148-
$this->assertLessThan($item->avg->memory, $till, 'average memory');
149-
}
150-
151-
return $this;
111+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->avg->memory, 'average memory');
152112
}
153113

154114
/**
155115
* Asserts that the total memory usage is within the specified range.
156116
*
157117
* @param float|null $from Start value is specified in bytes.
158118
* @param float|null $till End value is specified in bytes.
119+
*
159120
* @return $this
160121
*/
161122
public function toBeTotalMemory(?float $from = null, ?float $till = null): static
162123
{
163-
$from = $this->resolveFrom($from, $till);
164-
165-
foreach ($this->result as $item) {
166-
$this->assertGreaterThan($item->total->memory, $from, 'total memory');
167-
$this->assertLessThan($item->total->memory, $till, 'total memory');
168-
}
169-
170-
return $this;
124+
return $this->assertRange($from, $till, static fn (ResultData $item) => $item->total->memory, 'total memory');
171125
}
172126

173127
/**
174128
* Asserts that the execution time deviation is within the specified range.
175129
*
176130
* @param float|null $from Start value is specified in percentages.
177131
* @param float|null $till End value is specified in percentages.
132+
*
178133
* @return $this
179134
*/
180135
public function toBeDeviationTime(?float $from = null, ?float $till = null): static
181136
{
182-
$from = $this->resolveFrom($from, $till);
183-
184-
foreach ($this->result as $key => $item) {
137+
return $this->assertRange($from, $till, static function (ResultData $item, int|string $key) {
185138
if (! $item->deviation) {
186139
throw new DeviationsNotCalculatedException($key);
187140
}
188141

189-
$this->assertGreaterThan($item->deviation->percent->time, $from, 'deviation time');
190-
$this->assertLessThan($item->deviation->percent->time, $till, 'deviation time');
191-
}
192-
193-
return $this;
142+
return $item->deviation->percent->time;
143+
}, 'deviation time');
194144
}
195145

196146
/**
197147
* Asserts that the memory usage deviation is within the specified range.
198148
*
199149
* @param float|null $from Start value is specified in percentages.
200150
* @param float|null $till End value is specified in percentages.
151+
*
201152
* @return $this
202153
*/
203154
public function toBeDeviationMemory(?float $from = null, ?float $till = null): static
204155
{
205-
$from = $this->resolveFrom($from, $till);
206-
207-
foreach ($this->result as $name => $item) {
156+
return $this->assertRange($from, $till, static function (ResultData $item, int|string $key) {
208157
if (! $item->deviation) {
209-
throw new DeviationsNotCalculatedException($name);
158+
throw new DeviationsNotCalculatedException($key);
210159
}
211160

212-
$this->assertGreaterThan($item->deviation->percent->memory, $from, 'deviation memory');
213-
$this->assertLessThan($item->deviation->percent->memory, $till, 'deviation memory');
161+
return $item->deviation->percent->memory;
162+
}, 'deviation memory');
163+
}
164+
165+
/**
166+
* Asserts that the value extracted by the callback is within the specified range for all results.
167+
*
168+
* @param float|null $from The start value of the range.
169+
* @param float|null $till The end value of the range.
170+
* @param callable $callback Callback to extract the value from a result item.
171+
* @param string $name The name of the metric being checked.
172+
*
173+
* @return $this
174+
*/
175+
protected function assertRange(?float $from, ?float $till, Closure $callback, string $name): static
176+
{
177+
$from = $this->resolveFrom($from, $till);
178+
179+
foreach ($this->result as $key => $item) {
180+
$value = $callback($item, $key);
181+
182+
$this->assertGreaterThan($value, $from, $name);
183+
$this->assertLessThan($value, $till, $name);
214184
}
215185

216186
return $this;

0 commit comments

Comments
 (0)