Load Testing a PHP Application

Knowing how your app behaves under heavy load is essential for any successful online business

.com software
Dev Genius

--

Photo by Oleksandr Brovko on Unsplash

Have you ever asked yourself “how many users can my application handle?” Thanks to load testing tools this question isn’t that hard to answer.

About load testing

Load testing is a useful testing technique that allows you to verify your application’s behavior when exposed to a large number of concurrent sessions. Testing application before the release gives you an opportunity to spot otherwise hard to predict issues within the code.

There are number of tools to carry out such test, divided into two main categories:

How to conduct a test?

Before running any load test you need to define scenarios. Those can vary from simple ones like “load a single page” or complex ones, composed of multiple steps:

  • log-in to the application
  • look for a product using the search
  • open a product page

The application should be monitored periodically as the performance of the application may degrade over time due to the increasing amount of data in the database.

Where to conduct the test?

  • Test can be conducted:
  • in the production environment, preferably outside the peak hours not to disrupt regular users with possible outages
  • in the staging environment, preferably with server infrastructure matching or being close to the production and the databases seeded with the same amount of data
  • using local dev environment is not recommended unless the environment is using production ready settings

In case of the local environment:

  • the server needs to be somehow exposed to the Internet (eg. via an exposed port or a tunneling service)
  • the production mode should be enabled
  • the database should contain enough data
  • the networking throughput must be large enough to handle increased number of requests

For basic usage I’d recommend using the Loader.io service by SendGrid labs. The service is effortless to use and offers generous Free plan with 10.000 possible clients per test, which is more than enough to start playing with the service. Disclaimer: I’m not in any way affiliated with this provider.

In order to start you have add the host name of your web application and prove the ownership by placing a verification file in the root directory.

A list of verified hostnames

After the successful validation we can define our scenario. I will test the homepage rendering speed in my dev environment. Please keep in mind the server is not optimized in any way 😊

I will choose the “maintain client load” test to verify how my environment performs when there’s a constant number of 50 clients requesting the homepage each second during a one minute test. I’m going to include the cookies with the session id so the session is authenticated during the request and the consent cookie not to bother the request with the GDPR modal.

Scenario form filled with values

As soon as I hit the “Run test” button it begins to run. Sure enough, just after a while as I expected it failed to execute fully.

As you can tell from the chart, due to the high amount of concurrent requests the response time was increasing until the 10s limit was hit and the test aborted. My dev environment didn’t manage to handle 50 concurrent sessions. I will edit the scenario and go for 10 simultaneous sessions.

This time it managed to finish, with an average response time of 4733ms.

Wouldn’t call it success, but at least it works.

https://imgflip.com

I love load testing and I do it all the time. It allows to compare the “before” and “after” results of any kind of performance modifications.

Will you give it a try? Maybe you know a good service you would like to share, just hit me in the comments.

Any doubts? Thoughts? Please leave me a comment on what you think. Let’s together make our code beautiful. Subscribe to my publications for more articles like this to become a better developer.

Was this story of any value to you? Please support my work by leaving a 👏 clap as a token of appreciation. Did you know that you can clap more than once? 🥰 Thank You.

--

--

Father • PHP developer • entrepreneur • working for a €1bn unicorn startup as a backend engineer >>> https://bit.ly/dotcom-software