Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different verbs payload #8

Merged
merged 17 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
devajmeireles marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ In addition, the number of concurrent requests will be `1`. However, you may als
./vendor/bin/pest stress example.com --concurrency=5
```

You can choose to use GET or POST requests using the `--method` option:
devajmeireles marked this conversation as resolved.
Show resolved Hide resolved

```bash
./vendor/bin/pest stress example.com --method=POST
```

In addition to the `--method` option, also `--get` and `--post` shortcuts are provided:

```bash
./vendor/bin/pest stress example.com --get
# OR
./vendor/bin/pest stress example.com --post
```

You can choose to use a custom payload using the `--payload` option:

```bash
./vendor/bin/pest stress example.com --method=POST --payload='{"name": "Nuno"}'
```

The concurrency value represents the number of concurrent requests that will be made to the given URL. For example, if you set the concurrency to `5`, Pest will **constantly make 5 concurrent requests** to the given URL until the stress test duration is reached.

You may want to be mindful of the number of concurrent requests you configure. If you configure too many concurrent requests, you may overwhelm your application, server or hit rate limits / firewalls.
Expand Down Expand Up @@ -96,6 +116,26 @@ In addition, the number of concurrent requests will be 1. However, you may also
$result = stress('example.com')->concurrently(requests: 2)->for(5)->seconds();
```

You can choose to use GET or POST requests using the `method()` method:

```php
$result = stress('example.com')->method('POST')->for(5)->seconds();
```

Alternatively you can use the `get()` and `post()` shortcuts:

```php
$result = stress('example.com')->post()->for(5)->seconds();
```

You can choose to use a custom payload using the `payload()` method:

```php
$result = stress('example.com')->method('POST')->payload(['name' => 'Nuno'])->for(5)->seconds();
// or
$result = stress('example.com')->post()->payload(['name' => 'Nuno'])->for(5)->seconds();
```

At any time, you may `dd` the stress test result to see its details like if you were using the `stress` command):

```php
Expand Down
21 changes: 18 additions & 3 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import http from 'k6/http';
export const options = JSON.parse(__ENV.PEST_STRESS_TEST_OPTIONS);

export default () => {
http.get(__ENV.PEST_STRESS_TEST_URL, {
headers: { 'user-agent': 'Pest Plugin Stressless (https://pestphp.com) + K6 (https://k6.io)' },
});

let userAgent = 'Pest Plugin Stressless (https://pestphp.com) + K6 (https://k6.io)';
let url = __ENV.PEST_STRESS_TEST_URL;
let payload = options.payload ? JSON.stringify(options.payload) : JSON.stringify({});
let method = options.method ? options.method : 'get';

switch (method) {
case 'get':
http.get(url, {
headers: { 'user-agent': userAgent },
});
break;
case 'post':
http.post(url, payload, {
headers: { 'user-agent': userAgent },
});
break;
}
}

export function handleSummary(data) {
Expand Down
54 changes: 54 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ final class Factory
*/
private int $duration = 5;

/**
* The HTTP method to use.
*/
private string $method = 'get';

/**
* The payload to send.
* @var array<string, mixed>
*/
private array $payload = [];

/**
* The computed result, if any.
*/
Expand Down Expand Up @@ -59,6 +70,47 @@ public function duration(int $seconds): self
return $this;
}

/**
* Specifies that the test should be run using the given HTTP method.
*/
public function method(string $method): self
devajmeireles marked this conversation as resolved.
Show resolved Hide resolved
{
$method = strtolower($method);

assert(in_array($method, ['get', 'post'], true), 'The method must be one of: get, post');

$this->method = $method;

return $this;
}

/**
* Force the test to use get method
*/
public function get(): self
{
return $this->method('get');
}

/**
* Force the test to use post method
*/
public function post(): self
nahime0 marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->method('post');
}

/**
* Specifies the payload to send for the test, if any.
* @param array<string, mixed> $payload
*/
public function payload(array $payload): self
{
$this->payload = $payload;

return $this;
}

/**
* Specifies that run should run with the given number of concurrent requests.
*/
Expand Down Expand Up @@ -101,6 +153,8 @@ public function run(): Result
[
'vus' => $this->concurrency,
'duration' => sprintf('%ds', $this->duration),
'method' => $this->method,
'payload' => $this->payload,
'throw' => true,
],
$this->verbose,
Expand Down
30 changes: 28 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,38 @@ public function handleArguments(array $arguments): array
if (str_starts_with($argument, '--duration=')) {
$run->duration((int) str_replace('--duration=', '', $argument));
}
}

foreach ($arguments as $argument) {
if (str_starts_with($argument, '--concurrency=')) {
$run->concurrently((int) str_replace('--concurrency=', '', $argument));
}

if (str_starts_with($argument, '--method=')) {
nahime0 marked this conversation as resolved.
Show resolved Hide resolved
$run->method(str_replace('--method=', '', $argument));
}

if ($argument === '--get') {
$run->get();
}

if($argument === '--post') {
$run->post();
}

if (str_starts_with($argument, '--payload=')) {
try {
$payload = json_decode(str_replace('--payload=', '', $argument),
true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
View::render('components.badge', [
'type' => 'ERROR',
'content' => 'Invalid JSON payload. Please provide a valid JSON payload.' .
'Example: --payload=\'{"name": "Nuno"}\''
]);

exit(0);
}
$run->payload($payload);
}
}

$run->dd();
Expand Down
4 changes: 2 additions & 2 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{vus: int, duration: string} $options
* @param array{vus: int, duration: string, method: string, payload: array, throw: boolean} $options
*/
public function __construct(
readonly private Url $url,
Expand Down Expand Up @@ -71,7 +71,7 @@ public function start(): Result
$process = new Process([
K6::make(), 'run', 'run.js', '--out', "json={$this->session->progressPath()}",
], $basePath.'/bin', [
'PEST_STRESS_TEST_OPTIONS' => json_encode($this->options, JSON_THROW_ON_ERROR),
'PEST_STRESS_TEST_OPTIONS' => json_encode($this->options, JSON_FORCE_OBJECT|JSON_THROW_ON_ERROR),
'PEST_STRESS_TEST_URL' => $this->url,
'PEST_STRESS_TEST_SUMMARY_PATH' => $this->session->summaryPath(),
]);
Expand Down
Loading