< Back

Cloud-scale your performance testing with Artillery: Let’s get started.

Author

Koen Van Belle

Date

11/08/2022

Share this article

Looking for a tool that helps you with your performance tests in a quick, clean and effortless way? Artillery offers you the solution. This open source tool helps you with load testing your software in an automated way. Being a Test Automation Engineer, I think this tool is worth looking at.

Let’s start with installing Artillery, using it and discover my two cents about this testing toolkit.

Installing Artillery

Before starting with the installation, you will have to make sure that you run it on Node 16.0.1 or higher. This is required to install the server package and the actual testing application. For the installation explained in this blog post, we will use a standalone JSON server to have endpoints to test. Considering that we are going to load test a JSON file, do expect this server to fail, because it will.

The installation

While both applications (Artillery and JSON-SERVER) can be installed globally, best practice dictates that npm packages are installed in a working directory using a package.json file. First we can start by initializing the project. So create a folder and perform the following command. Press enter until out of the flow.

npm init

Install both applications using the following commands:

npm install json-server --sav-dev
npm install artillery --sav-dev

With these commands, the package.json will contain both develop dependencies and versioning. And both libraries have been installed and usable.

JSON-SERVER

This is a fairly simple application which enables several endpoints. Used for frontend development, these endpoints interact with a JSON file, pretending to be a database.

The different endpoints provide the possibility of faking real user behavior. The load/performance test will be able to POST something and retrieve it. Faking user behavior in a test means the difference between a valid test and screwing around.

The only difference between screwing around and science is writing it down - Ballistics expert Alex Jason

I know the quote is mostly attributed to Adam Savage. He had it from Alex Jason. Always check sources, people.

Next start the server, so we have something to work against.

npx json-server --watch db.json


Usage of Artillery

Next step we should start configuring our artillery strike. This configuration is essential to fire off the multitude of API calls. Start by creating a file called postPost.yml. First add the coordinates of the strike or the url.

config:
 - target: "http://localhost:3000"

After configuring the target, define the different phases of the strike. In the case of this tutorial, just add a starting phase. In this phase declare the maximum number of virtual users (maxVusers), the speed at which this number (arrivalRate) will be reached and the duration (In seconds) of the phase. For clarity in the reporting, it is best to name each phase. Each phase can have its own duration, arrivalRate, maxUsers and name. Allowing us to increase the pressure on the application over time in separate phases.

phases:
- duration: 10
  arrivalRate: 2
  maxVusers: 10
  name: Let's go

Fire the scenario in Artillery

Next up is the actual scenario to fire. Again for rapporting, name the scenario something sufficiently descriptive. Time to get to the meat of the matter, the actual flow.

In this code-example a post with a JSON object has been defined, in which the return object is captured. The point of capturing this object is to use the value in the next call. Being able to chain different calls in such an easy fashion highlights the usability of this tool. Finally, declare a get call and utilize the newly capture value using {{ postId }}.

scenarios:
- name: "Post and retrieve a post"
flow:
  - post:
      url: "/posts"
      json:
         title: "json-server"
         author: "typicode"
      capture:
       - json: "$.id"
         as: "postId"
  - get: 
       url: "/posts/{{ postId }}"

Now, the moment everyone waited for. Let's run the actual test. Inside the directory in which the yaml file is located, run the next command.

npx run artillery postPost.yml

Enjoy being able to load test anything in a five-minute setup.

More information can be found here:

JSON-Server
Artillery