Intended for: Developers

There’s a very nice tool to find out whether your website will work for high traffic and if your memory is sufficient to process that.

It’s called K6.io

The PHP errors can be detected by PHPCS, Github Bots, in-built code editors, but the memory issues can only be detected by doing load testing and simulating live environment and user interactions with backend.

Hence the K6.io load testing. 🙂

I found it very useful while testing the user requests, load testing and stress testing. Normally if you have a good code and less memory consuming codes it won’t let the site go down. However, if there is a PHP process that is going to take time to execute and consumes all the memory allotted, there will be chances that site goes unresponsive, terribly slow, and eventually the site can throw Fatal error due to memory exhaustion.

Something like:

PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 266240 bytes) in /var/www/wp-content/db.php on line 1064

So how to detect such things and how to allot only available memory and not exhaust the memory.

This can be done by checking the php function memory_get_usage and see if it is using less amount of memory.

You can see memory limit by printing this:

<?php
phpinfo();
?>

How to do load testing with k6.io

There are a few things you should do before running this.

  1. Import Live DB
    Important thing would be to import live database on your local system, such as localbyflywheel or your preferred local development server.
  2. Make code base exactly like live site
    This is easy because mostly you just need to pull master branch, by using these commads.
    > git checkout master
    > git pull origin master

The load testing in k6.io is very easy, just follow these steps:

  1. Installation
  2. Run K6: read here how to do following:
    • Run a test.
    • Add virtual users.
    • Increase the test duration.
    • Ramp the number of requests up and down as the test runs.
  3. Modify script to include your own website link (just test site or local site, not the production site)

A normal script would look like this, you can place it anywhere in your computer.

// script.js
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  http.get('https://test.k6.io');
  sleep(1);
}

Run the command like this for above script:

$ k6 run script.js

In the above script you can add more details like adding virtual users (to simulate real requests to the site), test duration and ramp-up requests.

Something like this:

// script.js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
  vus: 10,
  duration: '30s',
};
export default function () {
  http.get('http://test.k6.io');
  sleep(1);
}

However for a complete test, you can run with this script.

// script.js
import http from 'k6/http';
import { check, group, sleep } from 'k6';

export const options = {
  scenarios: {
    contacts: {
      executor: 'ramping-vus',
      startVUs: 3,
      stages: [
        { target: 20, duration: '30s' }, // linearly go from 3 VUs to 200 VUs for 30s
        { target: 100, duration: '0' }, // instantly jump to 100 VUs
        { target: 100, duration: '10m' }, // continue with 100 VUs for 10 minutes
      ],
    },
  },
};

const BASE_URL = 'https://your-domain.com';

export default () => {

  const myObjects = http.get(`${BASE_URL}`);;
  check(myObjects, { 'status was 200': (r) => r.status == 200 });

  sleep(1);
};

Please change the BASE_URL with your website domain you want to load test, name it script.js

and run it with following command.

$ k6 run script.js

After running the script, you will see that the site becomes little unresponsive and slow. You can monitor the php/error.log and see if there are any fatal errors. You will also need to access some high memory consuming pages on backend of the plugins you want to test it with. Such pages where DB queries run and cron functions run.

You can see the errors and then find errors and remove the erroneous codes or modify them to use less ram by limiting DB queries rows, and complexity. Or removing such features or pages altogether if not needed.

Leave a comment

Your email address will not be published. Required fields are marked *