November 17th, 2023

Introducing Stressless - Stress Testing with PEST

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:

1composer require pestphp/pest-plugin-stressless --dev

You may start using it in two different ways:

  • Using the stress command: 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:

1./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:

1./vendor/bin/pest stress example.com --duration=5

You can also customize the concurrent requests value using the --concurrency flag:

1./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:

1<?php
2 
3use function Pest\Stressless\stress;
4 
5it('has a fast response time', function () {
6 $result = stress('example.com');
7 
8 expect($result->requests()->duration()->med())->toBeLessThan(100); // < 100.00ms
9});

You can customize the stress test duration value using the for()->seconds() method:

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

To modify the number of concurrent requests you can use the concurrently() method:

1$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.

Statamic Ninja

Comments

Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.