Skip to content

Commit

Permalink
feat: expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 5, 2023
1 parent e27988a commit 5566c75
Show file tree
Hide file tree
Showing 22 changed files with 746 additions and 215 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ parameters:
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: true
ignoreErrors:
- "#Undefined variable: \\$this#"
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public function run(): Result
{
$this->running = true;

return $this->result = (new Run(
return $this->result ??= ((new Run(
new Url($this->url),
$this->options, // @phpstan-ignore-line
$this->verbose,
))->start();
))->start());
}

/**
Expand Down
34 changes: 0 additions & 34 deletions src/Options/Stage.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Result/DnsLookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Result;

use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*
* @property-read Duration $duration
*/
final readonly class DnsLookup
{
/**
* Creates a new requests instance.
*/
public function __construct(private Result $result)
{
//
}

/**
* Returns the details of the requests DNS Lookup duration.
*/
public function duration(): Duration
{
return new Duration($this->result->toArray()['metrics']['http_req_connecting']['values']);
}
}
40 changes: 40 additions & 0 deletions src/Result/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Result;

use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*
* @property-read Duration $duration
* @property-read Rate $data
*/
final readonly class Download
{
/**
* Creates a new requests instance.
*/
public function __construct(private Result $result)
{
//
}

/**
* Returns the details of the requests download data.
*/
public function data(): Rate
{
return new Rate($this->result->toArray()['metrics']['data_received']['values']);
}

/**
* Returns the details of the requests download duration.
*/
public function duration(): Duration
{
return new Duration($this->result->toArray()['metrics']['http_req_receiving']['values']);
}
}
84 changes: 84 additions & 0 deletions src/Result/Duration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Result;

/**
* @internal
*
* @property-read float $avg
* @property-read float $min
* @property-read float $med
* @property-read float $max
* @property-read float $p90
* @property-read float $p95
*/
final readonly class Duration
{
/**
* Creates a new duration instance.
*
* @param array{avg: float, min: float, med: float, max: float, "p(90)": float, "p(95)": float} $asArray
*/
public function __construct(private array $asArray)
{
//
}

/**
* Returns the average duration.
*/
public function avg(): float
{
return $this->asArray['avg'];
}

/**
* Returns the minimum duration.
*/
public function min(): float
{
return $this->asArray['min'];
}

/**
* Returns the median duration.
*/
public function med(): float
{
return $this->asArray['med'];
}

/**
* Returns the maximum duration.
*/
public function max(): float
{
return $this->asArray['max'];
}

/**
* Returns the 90th percentile duration.
*/
public function p90(): float
{
return $this->asArray['p(90)'];
}

/**
* Returns the 95th percentile duration.
*/
public function p95(): float
{
return $this->asArray['p(95)'];
}

/**
* Proxies the properties to methods.
*/
public function __get(string $name): mixed
{
return $this->{$name}(); // @phpstan-ignore-line
}
}
50 changes: 50 additions & 0 deletions src/Result/Rate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Result;

use Countable;

/**
* @internal
*
* @property-read int $count
* @property-read float $rate
*/
final readonly class Rate implements Countable
{
/**
* Creates a new duration instance.
*
* @param array{rate: float, passes: int, fails: int}|array{rate: float, count: int} $asArray
*/
public function __construct(private array $asArray)
{
//
}

/**
* Returns the rate.
*/
public function rate(): float
{
return $this->asArray['rate'];
}

/**
* Returns the count.
*/
public function count(): int
{
return $this->asArray['passes'] ?? $this->asArray['count']; // @phpstan-ignore-line
}

/**
* Proxies the properties to methods.
*/
public function __get(string $name): mixed
{
return $this->{$name}(); // @phpstan-ignore-line
}
}
111 changes: 111 additions & 0 deletions src/Result/Requests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Result;

use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*
* @property-read Duration $duration
* @property-read Download $download
* @property-read Rate $failed
* @property-read Server $server
* @property-read Upload $upload
* @property-read DnsLookup $dnsLookup
* @property-read float $rate
* @property-read int $count
* @property-read TlsHandshake $tlsHandshake
*/
final readonly class Requests
{
/**
* Creates a new requests instance.
*/
public function __construct(private Result $result)
{
//
}

/**
* Returns the details of the requests duration.
*/
public function duration(): Duration
{
return new Duration($this->result->toArray()['metrics']['http_req_duration']['values']);
}

/**
* Returns the details of the requests download.
*/
public function download(): Download
{
return new Download($this->result);
}

/**
* Returns the details of the failed requests.
*/
public function failed(): Rate
{
return new Rate($this->result->toArray()['metrics']['http_req_failed']['values']);
}

/**
* Returns the details of the requests server.
*/
public function server(): Server
{
return new Server($this->result);
}

/**
* Returns the details of the requests upload.
*/
public function upload(): Upload
{
return new Upload($this->result);
}

/**
* Returns the details of the requests DNS lookup.
*/
public function dnsLookup(): DnsLookup
{
return new DnsLookup($this->result);
}

/**
* Returns the rate.
*/
public function rate(): float
{
return $this->result->toArray()['metrics']['http_reqs']['values']['rate'];
}

/**
* Returns the count.
*/
public function count(): int
{
return $this->result->toArray()['metrics']['http_reqs']['values']['count'];
}

/**
* Returns the details of the requests TLS handshake duration.
*/
public function tlsHandshake(): TlsHandshake
{
return new TlsHandshake($this->result);
}

/**
* Proxies the properties to methods.
*/
public function __get(string $name): mixed
{
return $this->{$name}(); // @phpstan-ignore-line
}
}
Loading

0 comments on commit 5566c75

Please sign in to comment.