Skip to content

Commit

Permalink
feat: uses vus instead of stages
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 9, 2023
1 parent cd3b497 commit 5e89414
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
19 changes: 7 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ final class Factory

/**
* Creates a new instance of the run factory.
*
* @param array{stages: array<int, array{duration: string, target: int}>} $options
*/
private function __construct(private readonly string $url, private array $options)
private function __construct(private readonly string $url)
{
//
}
Expand All @@ -46,7 +44,7 @@ private function __construct(private readonly string $url, private array $option
*/
public static function make(string $url): self
{
return new self($url, ['stages' => []]);
return new self($url);
}

/**
Expand Down Expand Up @@ -98,16 +96,13 @@ public function run(): Result
return $this->result;
}

$this->options['stages'] = [[
'duration' => sprintf('%ds', $this->duration),
'target' => $this->concurrency,
]];

$this->options['throw'] = true;

return $this->result = ((new Run(
new Url($this->url),
$this->options,
[
'vus' => $this->concurrency,
'duration' => sprintf('%ds', $this->duration),
'throw' => true,
],
$this->verbose,
))->start());
}
Expand Down
66 changes: 40 additions & 26 deletions src/Printers/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,58 @@ public function tail(): void

/** @var array<int, array{data: array{time: string, value: float}}> $points */
$points = [];

$buffer = '';
$lastTime = null;
$lastTime = '';

while ($this->process->isRunning()) {
$output = trim($tail->getIncrementalOutput());
$this->fetch($tail, $points, $buffer, $lastTime);
}

if ($output === '') {
continue;
}
$this->fetch($tail, $points, $buffer, $lastTime);
}

/**
* Fetches the tail output.
*
* @param array<int, array{data: array{time: string, value: float}}> $points
*/
private function fetch(Process $tail, array &$points, string &$buffer, string &$lastTime): void
{
$output = trim($tail->getIncrementalOutput());

$output = $buffer.$output;
$buffer = '';
if ($output === '') {
return;
}

$lines = explode("\n", $output);
$output = $buffer.$output;
$buffer = '';

foreach ($lines as $line) {
if (str_starts_with($line, '{"metric":"http_req_duration","type":"Point"')) {
try {
/** @var array{data: array{time: string, value: float}}|null $point */
$point = json_decode($line, true, 512, JSON_THROW_ON_ERROR);
assert(is_array($point));
$lines = explode("\n", $output);

$currentTime = substr($point['data']['time'], 0, 19);
if ($lastTime !== $currentTime) {
$this->printCurrentPoints($points);
$points = [];
foreach ($lines as $line) {
if (str_starts_with($line, '{"metric":"http_req_duration","type":"Point"')) {
try {
/** @var array{data: array{time: string, value: float}}|null $point */
$point = json_decode($line, true, 512, JSON_THROW_ON_ERROR);
assert(is_array($point));

$lastTime = $currentTime;
}
$currentTime = substr($point['data']['time'], 0, 19);
if ($lastTime !== $currentTime) {
$this->printCurrentPoints($points);
$points = [];

$points[] = $point;
} catch (JsonException) {
$buffer .= $line;
$lastTime = $currentTime;
}

$points[] = $point;
} catch (JsonException) {
$buffer .= $line;
}
}

usleep(100000); // 100ms
}

usleep(10000); // 10ms

}

/**
Expand All @@ -128,6 +141,7 @@ private function printCurrentPoints(array $points): void

$greenDots = (int) (($average * $width) / $maxResponseTime);

$greenDots = min($greenDots, 150 - 23);
$greenDots = str_repeat('', $greenDots);

$average = sprintf('%4.2f', $average);
Expand Down
12 changes: 6 additions & 6 deletions src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class Run
/**
* Creates a new run instance.
*
* @param array{stages: array{0: array{duration: string, target: int}}} $options
* @param array{vus: int, duration: string} $options
*/
public function __construct(
readonly private Url $url,
Expand All @@ -43,12 +43,12 @@ public function __construct(
*/
public function start(): Result
{
renderUsing(
Container::getInstance()->get(OutputInterface::class)
);
$output = Container::getInstance()->get(OutputInterface::class);
assert($output instanceof OutputInterface);
renderUsing($output);

$concurrency = $this->options['stages'][0]['target'];
$duration = (int) $this->options['stages'][0]['duration'];
$concurrency = $this->options['vus'];
$duration = (int) $this->options['duration'];

$this->session = new Session(
$basePath = dirname(__DIR__),
Expand Down

0 comments on commit 5e89414

Please sign in to comment.