Skip to content

Commit

Permalink
feat: refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 4, 2023
1 parent 6b46023 commit 73637ad
Show file tree
Hide file tree
Showing 25 changed files with 1,398 additions and 357 deletions.
2 changes: 1 addition & 1 deletion bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default () => {

export function handleSummary(data) {
return {
'summary.json': JSON.stringify(data),
[__ENV.PEST_STRESS_TEST_SUMMARY_PATH]: JSON.stringify(data),
};
}
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
"license": "MIT",
"require": {
"php": "^8.2",
"pestphp/pest": "@dev",
"pestphp/pest": "^2.24.1",
"pestphp/pest-plugin": "^2.1.1",
"ext-curl": "*"
},
"repositories": [
{
"type": "path",
"url": "../pest"
}
],
"autoload": {
"psr-4": {
"Pest\\Stressless\\": "src/"
Expand Down
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__.'/src',
__DIR__.'/tests',
]);

$rectorConfig->rules([
Expand Down
57 changes: 57 additions & 0 deletions src/Blocks/NetworkDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Blocks;

use Pest\Stressless\Contracts\Block;
use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*/
final readonly class NetworkDuration implements Block
{
/**
* Creates a block instance.
*/
public function __construct(
private Result $result,
) {
//
}

/**
* Gets the block label.
*/
public function value(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['http_req_connecting']['values']['avg']
+ $array['metrics']['http_req_tls_handshaking']['values']['avg']
+ $array['metrics']['http_req_duration']['values']['avg']
- $array['metrics']['http_req_waiting']['values']['avg'];

return sprintf('%4.2f ms', $duration);
}

/**
* Gets the block color.
*/
public function color(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['http_req_connecting']['values']['avg']
+ $array['metrics']['http_req_tls_handshaking']['values']['avg']
+ $array['metrics']['http_req_duration']['values']['avg']
- $array['metrics']['http_req_waiting']['values']['avg'];

return match (true) {
$duration < 100 => 'green',
$duration < 200 => 'yellow',
default => 'red',
};
}
}
55 changes: 55 additions & 0 deletions src/Blocks/ResponseDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Blocks;

use Pest\Stressless\Contracts\Block;
use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*/
final readonly class ResponseDuration implements Block
{
/**
* Creates a block instance.
*/
public function __construct(
private Result $result,
) {
//
}

/**
* Gets the block label.
*/
public function value(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['req_connecting']['values']['avg']
+ $array['metrics']['req_tls_handshaking']['values']['avg']
+ $array['metrics']['req_duration']['values']['avg'];

return sprintf('%4.2f ms', $duration);
}

/**
* Gets the block color.
*/
public function color(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['req_connecting']['values']['avg']
+ $array['metrics']['req_tls_handshaking']['values']['avg']
+ $array['metrics']['req_duration']['values']['avg'];

return match (true) {
$duration < 200 => 'green',
$duration < 400 => 'yellow',
default => 'red',
};
}
}
51 changes: 51 additions & 0 deletions src/Blocks/ServerDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Blocks;

use Pest\Stressless\Contracts\Block;
use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*/
final readonly class ServerDuration implements Block
{
/**
* Creates a block instance.
*/
public function __construct(
private Result $result,
) {
//
}

/**
* Gets the block label.
*/
public function value(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['http_req_waiting']['values']['avg'];

return sprintf('%4.2f ms', $duration);
}

/**
* Gets the block color.
*/
public function color(): string
{
$array = $this->result->toArray();

$duration = $array['metrics']['http_req_waiting']['values']['avg'];

return match (true) {
$duration < 100 => 'green',
$duration < 200 => 'yellow',
default => 'red',
};
}
}
47 changes: 47 additions & 0 deletions src/Blocks/SuccessRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Blocks;

use Pest\Stressless\Contracts\Block;
use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*/
final readonly class SuccessRate implements Block
{
/**
* Creates a block instance.
*/
public function __construct(
private Result $result,
) {
//
}

/**
* Gets the block label.
*/
public function value(): string
{
$array = $this->result->toArray();

$percentage = (float) ($array['metrics']['http_req_failed']['values']['fails'] * 100 / $array['metrics']['http_reqs']['values']['count']);

return sprintf('%4.1f %%', $percentage);
}

/**
* Gets the block color.
*/
public function color(): string
{
$array = $this->result->toArray();

return $array['metrics']['http_req_failed']['values']['fails'] === $array['metrics']['http_reqs']['values']['count']
? 'green'
: 'red';
}
}
21 changes: 21 additions & 0 deletions src/Contracts/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Pest\Stressless\Contracts;

/**
* @internal
*/
interface Block
{
/**
* Gets the block value.
*/
public function value(): string;

/**
* Gets the block color.
*/
public function color(): string;
}
2 changes: 2 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Pest\Stressless;

use Pest\Stressless\ValueObjects\Result;

/**
* @internal
*
Expand Down
47 changes: 45 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Pest\Stressless;

use Pest\Stressless\Fluent\WithOptions;
use Pest\Stressless\ValueObjects\Result;
use Pest\Stressless\ValueObjects\Url;

/**
* @internal
Expand All @@ -13,6 +15,11 @@
*/
final class Factory
{
/**
* Weather or not the run should be verbose.
*/
private bool $verbose = false;

/**
* The computed result, if any.
*/
Expand Down Expand Up @@ -70,7 +77,43 @@ public function stage(int $requests, int $seconds): self
*/
public function run(): Result
{
return $this->result = (new Run($this->url, $this->options))->start();
return $this->result = (new Run(
new Url($this->url),
$this->options,
$this->verbose,
))->start();
}

/**
* Specifies that the run should be verbose, and then exits.
*/
public function dd(): never
{
$this->dump();

exit(1);
}

/**
* Specifies that the run should be verbose.
*/
public function dump(): self
{
$this->verbosely();

$this->run();

return $this;
}

/**
* Specifies that the run should be verbose.
*/
public function verbosely(): self
{
$this->verbose = true;

return $this;
}

/**
Expand All @@ -80,7 +123,7 @@ public function run(): Result
*/
public function __call(string $name, array $arguments): mixed
{
if (! $this->result instanceof \Pest\Stressless\Result) {
if (! $this->result instanceof Result) {
$this->run();
}

Expand Down
Loading

0 comments on commit 73637ad

Please sign in to comment.