Skip to content

Commit

Permalink
Add ttfb timing to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Apr 11, 2024
1 parent 544e626 commit d2badb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeploymentHawk;

use Illuminate\Support\Collection;

class Request
{
protected array $request;
Expand Down Expand Up @@ -65,7 +67,8 @@ public function timings(int $precision = 2): array
'connect' => $this->connectTiming($precision),
'send' => $this->sendTiming($precision),
'wait' => $this->waitTiming($precision),
'receive' => $this->sendTiming($precision),
'receive' => $this->receiveTiming($precision),
'ttfb' => $this->ttfbTiming($precision),
];
}

Expand Down Expand Up @@ -112,6 +115,18 @@ public function receiveTiming(int $precision = 2): float
return round($this->request['timings']['receive'], $precision);
}

public function ttfbTiming(int $precision = 2): float
{
$ttfb = (new Collection([
$this->dnsTiming($precision),
$this->connectTiming($precision),
$this->sendTiming($precision),
$this->waitTiming($precision),
]))->filter()->sum();

return round($ttfb, $precision);
}

public function requestHeaders(): array
{
return $this->request['request']['headers'];
Expand Down
9 changes: 8 additions & 1 deletion tests/Unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
'connect' => 52.07,
'send' => 0.25,
'wait' => 26.75,
'receive' => 0.25,
'receive' => 46.90,
'ttfb' => 79.08,
]);
});

Expand Down Expand Up @@ -100,6 +101,12 @@
->and($this->request->receiveTiming(3))->toEqual(46.900);
});

test('request ttfb timing', function () {
expect($this->request->ttfbTiming())->toEqual(79.08)
->and($this->request->ttfbTiming(0))->toEqual(79)
->and($this->request->ttfbTiming(3))->toEqual(79.075);
});

test('request headers', function () {
expect($this->request->requestHeaders())->toBeArray()
->and($this->request->requestHeaders()[0])->toMatchArray([
Expand Down

0 comments on commit d2badb2

Please sign in to comment.