Guides

Laravel Load Testing

How to easily load test your Laravel web application and optimize its performance.

Prefer to learn by doing?

LoadForge can automatically scan your Laravel application, generate a load test and begin testing within minutes.

p.s. did you know, LoadForge is a Laravel app?

Sign up
Laravel Load Test

Overview

Testing your Laravel application is very different from testing a web server (e.g. nginx, apache). With an application you need to ensure you test the full stack, including the "heaviest" workloads. By examples these can include:

  • PHP generated pages
  • Logging users in
  • Running searches
  • CRUD operations, especially Updates or Creates

These operations are interactive, dynamic content. With Laravel, they all require PHP processing, and often require a SQL database interaction or Redis cache. These are the common bottlenecks in a modern web application. Server load testing is very different, and has to do with raw requests per second from the web server itself, e.g. Nginx, Apache, etc.

Breaking your Laravel app into components

You want to ensure your load testing attacks all the critical components of your application. Typically, this is three things:

  • Static content (images, stylesheets, javascript)
  • Dynamic content (pages, user profiles, etC)
  • Database operations

In developing this strategy you'll want to determine the average wait time of a user of your service. Typically, a user waits on average between 5 and 9 seconds between webpages. So for each user we simulate we now know 1 dynamic page per 5-9 seconds.

After that, we want to gauge how many static content items are returned by your average webpage. Browse a few with Chrome or Firefox Developer tools open and watch the bottom bar for the total requests per page, as seen in this screenshot:

objects-per-page.png

As you can see above, we requested 19 objects. However, in our case many of those are not hosted on our system as we use a CDN. You can look at the domain to work out how many items hit your site. Lets assume that on average its 10 static content items (stylesheets, CSS, fonts, images).

The final component is to ensure that the dynamic pages you are visiting are causing database load. For example, make sure you are logging in. Adding items to carts. Searching, opening blog posts, etc. In total our plan has led to these decisions:

  • We're going to request 1 dynamic page every 5-9 seconds
  • For every dynamic page we will ask for 10 static content items
  • We're going to use database-driven pages

Creating a test

LoadForge gives you the opportunity to design tests to suit your needs, like we've described above. You can also use the wizard to crawl your Laravel app and discover all the testable content.

We can now write our LoadForge test, which we will execute against our Laravel site we've defined above. We'll start by defining just the basics - the URL to run it against, and that we'll start with 500 simultaneous users. Remember that with our 5-9 seconds wait time decision that will be around 500/7 = 71 per second.

create-a-test.png

As you can see above that is defined very easily. The next step is where we can customize, defining the locustfile. We'll show you our complete one, and then break it down:

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(5, 9)

    @task(1)
    def laravel_page(self):
        self.client.get("/")
        self.client.get("/images/logo.png")
        self.client.get("/css/app.css")
        self.client.get("/js/app.js")

Let's break this down, it's quite similar to our default template, and if you want advanced uses check out our documentation.

As you can see we've defined wait_time as between(5, 9) - between 5 and 9 seconds as discussed above. Next we have the laravel_page test, which is fetching simply /. We could add additional ones here as well. We're also using this to do the 3 static gets on our page.

Watching your Laravel system

The test will automatically help you to identify any errors, and scale your app. But, you can monitor these on your actual Laravel webserver:

p.s. Did you know? LoadForge is Laravel!

Tuning Laravel for high performance

Caching and optimizing your site and server can lead to huge improvements in Laravel performance.

Laravel Settings Caches

Considering caching your config, routes, and more for performance improvements:

php artisan config:cache
php artisan route:cache

Cache expensive queries

Laravel has an excellent caching system, and it's easy to wrap Eloquent or DB queries inside, as shown below:

$posts = Cache::remember('posts', 60, function() {
    return Posts::all();
});

This will store the results for 60 seconds, meaning you don't need to fetch them every time.

Tune your PHP and webserver

Make sure you read our guides on high performance Nginx and PHP-FPM:

Understanding results

Interpreting the results of your test are as important as running the test, if not more. Continue this on our reading load test results guide.

Ready to run your test?
Start your first test within minutes.