-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRunner.php
More file actions
46 lines (33 loc) · 920 Bytes
/
Runner.php
File metadata and controls
46 lines (33 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark\Services;
use Closure;
use function hrtime;
class Runner
{
public function __construct(
protected readonly Memory $memory = new Memory
) {}
public function call(Closure $callback, array $parameters = []): array
{
$this->clean();
return $this->run($callback, $parameters);
}
protected function clean(): void
{
$this->memory->reset();
}
protected function run(Closure $callback, array $parameters = []): array
{
$ramFrom = $this->memory->now();
$startAt = hrtime(true);
$callback(...$parameters);
$time = $this->diff(hrtime(true), $startAt);
$ram = $this->memory->diff($ramFrom);
return [$time, $ram];
}
protected function diff(float $now, float $startAt): float
{
return ($now - $startAt) / 1e+6;
}
}