📖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.
- 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
- Install k6
- Unix-Like Operating System to use cron and mail Configure mail to send mails from the command line
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 https://test-api.loadimpact.com/
This test will get the list of public crocodiles and get the first one of the list.
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.
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.
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
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.
Edit and close the file.
The test is executed every 5 minutes and if our performance test fails, we will receive a mail notification as:
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!