Welcome back to our blog series on using k6 to test the performance of PokeAPI! In this second instalment, we'll guide you through the process of setting up your k6 environment and accessing PokeAPI to begin your performance testing journey. Let's dive in!
Installing k6 and setting up the environment
Before you can start using k6 for performance testing, you'll need to install it on your machine. Follow these steps to install k6:
For Windows users:
- Download the latest k6 release for Windows from the official GitHub repository.
- Extract the downloaded archive.
- Add the extracted folder to your system's PATH variable.
For macOS users:
- Use Homebrew to install k6 by running the following command in your terminal:
brew install k6
For Linux users:
- Use the appropriate package manager for your distribution to install k6. For example, on Ubuntu, you can run:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install k6
After the installation is complete, you can verify that k6 is installed correctly by running the following command in your terminal:
k6 version
This should display the installed k6 version number.
Accessing PokeAPI and exploring its endpoints
To begin testing PokeAPI, you'll first need to familiarize yourself with its endpoints. PokeAPI is a RESTful API, and you can access its resources using standard HTTP methods. The base URL for PokeAPI is https://pokeapi.co/api/v2/
.
Here are some examples of common PokeAPI endpoints:
List of Pokémon:
GET https://pokeapi.co/api/v2/pokemon
Specific Pokémon details:
GET https://pokeapi.co/api/v2/pokemon/{id or name}
List of Pokémon types:
GET https://pokeapi.co/api/v2/type
List of Pokémon abilities:
GET https://pokeapi.co/api/v2/ability
To explore more endpoints, you can refer to the official PokeAPI documentation.
Creating a basic k6 test script for PokeAPI
Now that you have k6 installed and are familiar with PokeAPI's endpoints, it's time to create a simple test script. Using your favourite text editor or IDE, create a new file named pokeapi-test.js
and add the following code:
import http from 'k6/http';
import { check } from 'k6';
export let options = {
vus: 10, // 10 virtual users
duration: '30s', // test duration
};
export default function () {
const response = http.get('https://pokeapi.co/api/v2/pokemon');
check(response, {
'status is 200': (r) => r.status === 200,
});
}
This basic test script simulates 10 virtual users sending requests to the Pokémon list endpoint for 30 seconds. It also checks if the response status code is 200 (OK).
To run the test script, open your terminal and navigate to the directory where you saved the pokeapi-test.js
file. Then, execute the following command:
k6 run pokeapi-test.js
As the test runs, k6 will display real-time information about the test progress, including the number of virtual users, duration, request rates, response times, and more. Once the test is complete, k6 will provide a summary of the test results.
In this basic test script, we focused on the Pokémon list endpoint. In the next blog post, we'll explore more advanced test scenarios and expand our test script to cover multiple API endpoints.
To recap, in this blog post, we walked you through setting up your k6 environment and accessing PokeAPI. We also demonstrated how to create a basic k6 test script and run it to test the performance of the PokeAPI. Now you're all set to start building more complex test scenarios and analysing the performance of PokeAPI!
Stay tuned for the next instalment in our blog series, where we'll dive into building and refining test scenarios to ensure the PokeAPI performs optimally under various conditions.