Introducing Stressless - Stress Testing with PEST
This week was full of treats for Laravel developers due to LaraconAU. After the launch of Laravel Pulse, Nuno Maduro announced a new plugin called Stressless which allows you to stress test your application.
What is stress testing
Stress testing in software testing aims to assess the performance of an application or system when subjected to exceptionally high levels of stress. This form of testing involves exposing the system or application to scenarios specifically created to exceed its typical operational limits.
Within Pest, you have the ability to integrate Stress Testing with the Expectation API, guaranteeing the absence of stability and reliability regressions over time. This proves valuable in confirming the stability and reliability of your application following a new release or deployment.
Stressless
Stressless utilizes k6, a potent open-source tool for load testing, to assess the performance of APIs, microservices, and websites behind the scenes.
Installation
To start using Stressless and stress test your code you need to require the plugin via Composer:
composer require pestphp/pest-plugin-stressless --dev
You may start using it in two different ways:
-
Using the
stresscommand: It's useful when you want to quickly stress test a URL, without setting expectations on the result. -
Using the
stress()method: It's useful when you want to stress test a URL and set expectations on the result.
The Stress Command
The stress command proves handy when you need to rapidly conduct a stress test on a URL, examine the outcomes, all without specifying expectations. It offers the fastest means to initiate a stress test, directly within the terminal.
You can use the stress command by providing the URL you wish to stress test:
./vendor/bin/pest stress example.com
By default, the stress test will last for 10 seconds, but you have the flexibility to tailor this duration by using the --duration option:
./vendor/bin/pest stress example.com --duration=5
You can also customize the concurrent requests value using the --concurrency flag:
./vendor/bin/pest stress example.com --concurrency=5
The Stress Function
To begin, create a standard test and employ the stress() function to conduct a stress test on a specified URL:
<?php
use function Pest\Stressless\stress;
it('has a fast response time', function () {
$result = stress('example.com');
expect($result->requests()->duration()->med())->toBeLessThan(100); // < 100.00ms
});
You can customize the stress test duration value using the for()->seconds() method:
$result = stress('example.com')->for(5)->seconds();
To modify the number of concurrent requests you can use the concurrently() method:
$result = stress('example.com')->concurrently(requests: 2)->for(5)->seconds();
For a complete list of options / features please see the official docummentation.
Demo
You can watch Nuno's video presentation of the plugin where he is going through the features and the options you have and might want to use while stress testing your Laravel application.