Load Testing APIs with Artillery
October 4, 2020 • APIs, Testing • Published By Josh • 3 minute read
Artillery is a modern, powerful & easy-to-use load testing and functional testing toolkit. It can be used to ship scalable backends, APIs & services that stay performant and resilient under high load. Let’s learn how to use Artillery!
Table of Contents
System Requirements
Artillery should run on any system that uses Node.js version 12 or above.
Installation
Run the following command to install Artillery globally:
npm install -g artillery
Key Concepts
Write a test definition and Artillery will run it. It’s as simple as that! Test definitions are written as YAML files and they have two main parts:
- Configuration
- Define the apis you’re going to call
- Define how many users to generate over a period of time
- Define data to use in the test
- Define plugins to use
- Scenarios
- Define one or more actions the user will take during the test
Environments
As part of your configuration, you can define different environments. For example, a DEV environment may have different example data to use or a different api to call compared to a production environment. This helps you customize the test per environment.
config:
environments:
dev:
DEV_CONFIGURATION_HERE
prod:
PROD_CONFIGURATION_HERE
Test Definition Examples
Create 1 virtual user (arrivalRate) every second for 5 seconds (duration) making requests to https://pokeapi.co/api/v2/pokemon/pikachu.
config:
target: 'https://pokeapi.co/api/v2'
phases:
- duration: 5
arrivalRate: 1
scenarios:
- flow:
- get:
url: '/pokemon/pikachu'
Create 1 virtual user (arrivalRate) every second for 5 seconds (duration) in a loop based on the pokemon variable.
config:
target: 'https://pokeapi.co/api/v2'
phases:
- duration: 5
arrivalRate: 1
variables:
pokemon:
- ['pikachu', 'mewtwo', 'charizard']
scenarios:
- flow:
- loop:
- get:
url: '/pokemon/{{ $loopElement }}'
over: pokemon
Create 1 virtual user (arrivalRate) every second for 5 seconds (duration) making requests to https://pokeapi.co/api/v2/pokemon/pikachu. Using the result of the first request, get a value to make the next request.
config:
target: 'https://pokeapi.co/api/v2'
phases:
- duration: 5
arrivalRate: 1
scenarios:
- flow:
- get:
url: '/pokemon/pikachu'
capture:
json: '$.species.url'
as: 'species'
- get:
url: '/{{ species }}'
Run A Test
Let’s say you saved your test definition as get-pikachu.yml. To run a test, run the following:
artillery run get-pikachu.yml
If you have environments set up and want to run the test for a specific environment, include the environment flag along with the environment name. For example, if I wanted to run the test for an environment called dev, I would run:
artillery run -e dev get-pikachu.yml
Related Articles
- Include API Key In HTTP Requests
- Create Your Own APIs with Google Sheets and Google Apps Script (Part 2)
- Create Your Own APIs with Google Sheets and Google Apps Script (Part 1)
- Create Your Own APIs with Google Sheets and Google Apps Script (Part 4)
- Stop/Restart Requests When User Is Inactive using Angular, RxJS, and Page Visibility API