Integrations 04 October 2019

Performance monitoring with cron and k6

Pepe Cano

📖What you will learn

  • How to schedule k6 tests wih cron
  • How to monitor the performance of your system using k6
  • How to configure a k6 test to send the test output to Kafka


k6 OSS is an open-source load testing tool for testing the performance of backend services. Performance monitoring of a backend service is the process of regularly verifying how fast your service is running.

cron is a Linux utility to schedule a command to run periodically at fixed times, dates, or intervals. Cron tasks are useful in automating repetitive tasks.


This is a basic tutorial to present an idea of how to set up quickly a performance monitoring stack with k6 and cron.

  • Step 1 - Create your test
  • Step 2 - Configure your performance validations
  • Step 3 - Write a bash script to run the test
  • Step 4 - Schedule the test with cron


Step 1 - Create your test

First, we start creating a simple performance test generating a few HTTP requests to our API demo. The documentation of our demo API is available at

This test will get the list of public crocodiles and get the first one of the list.

import http from 'k6/http';
import { sleep } from 'k6';
const BASE_URL = '';
export default function () {
let req;
req = http.get(`${BASE_URL}/public/crocodiles/`);
const crocs = JSON.parse(req.body);
if (crocs && crocs.length) {
req = http.get(`${BASE_URL}/public/crocodiles/${crocs[0].id}/`);

Step 2 - Configure your performance validations

Now, we will define the performance goals of your system in terms of how fast the system should respond.

k6, via the Thresholds API, provides a lot of flexibility to configure the rules to validate your performance goals. You could define how fast one particular request or a measure of all the requests should be.

In our test, we will validate that the 99th percentile of all the response times is below 500 ms.

export let options = {
thresholds: {
"http_req_duration": ["p(99)<500"]

We are now ready to configure the type of execution of the test. We ask ourselves:

  • Will the same test run multiple times simultaneously? Check out the vus and stages options.
  • Will the test run continuously for a period or multiple successive executions? Check out the duration and iterations options.

Using a load testing tool like k6 for performance monitoring provides you a lot of flexibility to validate the performance of your system in multiple ways.

We are going to choose to run the test continuously for 45 seconds.

In this way, we want to avoid a failing test if there is a momentary and "tolerable" hiccup in the service.

export let options = {
duration: '45s',
thresholds: {
"http_req_duration": ["p(99)<500"]

Step 3 - Write a bash script to run the test

After creating our performance test (see the full script), we will create a simple bash script to be used as a cron job. The bash script will:

  • Run our load test
  • Check the result of the test
  • If the test failed, send one mail notification
k6 run my-performance-test.js
if [ $? -ne 0 ]; then
currentDate=`date +"%D %T"`
mail -s "🚨 Failed performance test" <<< "${currentDate} - My performance test"
exit 1
As prerequisite, you must configure the mail command to send mails from the command line.

Step 4 - Schedule the test with cron

It is time to schedule the test to run it continuously. We previously configured the execution of the test to run for 45 seconds. Now, we will configure crontab to run our test every 5 minutes.

crontab -e

Edit and close the file.

*/5 * * * * /path/


The test is executed every 5 minutes and if our performance test fails, we will receive a mail notification as:

Performace testing notification


This tutorial has explored how you can use k6 along with cron to monitor the performance of your system continuously. If your performance drops, you will receive a mail notification.

This is a basic example to show you a simple option to schedule the run of k6 tests periodically. LoadImpact users can run cloud tests from the bash script or use the Scheduling feature along with Notifications to get notified when your test fails.

Good tests are designed to your particular needs.
You could customize this stack with other type of performance tests, schedule frequency, notifications, or alerts.
Or just extend it with other integrations.

Happy performance monitoring!

< Back to all posts