Cover

Generating Dynamic Test Data in k6 with Faker.js

Introduction
Generating Dynamic Test Data in k6 with Faker.js

One of the ongoing challenges is generating realistic and varied test data, especially when simulating large numbers of users. Manually creating this data can be both time-consuming and prone to errors. Fortunately, there’s a tool that can help streamline this process: Faker.js. When combined with k6, an open-source load testing tool, Faker.js allows us to generate dynamic, realistic test data with ease.

The Pitfalls of Static Test Data

If you’ve been involved in performance testing for any length of time, you’ll know how crucial it is to avoid using static or repetitive data in your tests. Real-world usage patterns involve a wide array of inputs, and your tests should reflect this. Using a small, static dataset can lead to misleading results and missed bottlenecks.

Consider an example where you're testing an API endpoint that processes user comments. You might use the same text, like "Lorem ipsum dolor sit amet," for every request. This approach might seem convenient, but it can create several issues. The server could cache the response to this specific input, leading to artificially improved performance metrics. Moreover, the application might behave differently with varied inputs, and repeated data doesn't test how the system handles unique constraints or diverse content.

The Value of Dynamic Data with Faker.js

This is where Faker.js comes into its own. Instead of using the same "lorem ipsum" text over and over, Faker.js allows you to generate varied and realistic data on the fly. For example, rather than submitting a single static comment, you can generate a new, unique comment for each request:

import { faker } from 'https://esm.sh/@faker-js/faker';

const comment = faker.lorem.sentence(); // e.g., "Voluptatem et perspiciatis ut autem ea dolores."

By using Faker.js, each virtual user can submit a different, randomly generated comment, closely mimicking real-world usage. This approach ensures that your load test is more reflective of actual user behaviour, leading to more accurate and meaningful performance metrics.

Setting Up Faker.js in Your k6 Scripts

So how you can integrate Faker.js into your K6 scripts to automatically generate diverse datasets for your load tests?

Importing Faker.js

To start with, you’ll need to import Faker.js into your k6 script. With k6’s support for ECMAScript Modules, this is a straightforward process:

import { faker } from 'https://esm.sh/@faker-js/faker';

const randomName = faker.person.fullName(); // e.g., Willie Bahringer
const randomEmail = faker.internet.email(); // e.g., Tomasa_Ferry14@hotmail.com

This snippet shows the basics: generating a random name and email address. These are just the tip of the iceberg, as Faker.js can create a wide variety of data types.

Creating a User Data Generator

To make your testing more robust, it’s wise to encapsulate data generation in a function. This keeps your scripts clean and makes it easy to tweak the data generation process as needed:

export const generateUser = () => ({
  fullName: faker.person.fullName(),
  email: faker.internet.email(),
  username: faker.internet.userName(),
  password: faker.internet.password(),
  phone: faker.phone.number(),
});

This generateUser function creates a user object with several fields, each populated by Faker.js. This approach ensures that each virtual user (VU) in your k6 test has unique and realistic data.

Applying Generated Data in Your Tests

With our data generation function ready, we can now use it in a k6 test script. Below is an example where we simulate a user registration process:

import http from 'k6/http';
import { check } from 'k6';
import { generateUser } from './user-generator.js';

export const options = {
  vus: 100,
  duration: '30s',
};

export default function () {
  const user = generateUser();
  const payload = JSON.stringify(user);

  const res = http.post('https://your-api.com/signup', payload, {
    headers: { 'Content-Type': 'application/json' },
  });

  check(res, {
    'status is 200': (r) => r.status === 200,
    'response time < 500ms': (r) => r.timings.duration < 500,
  });
}

Here, each virtual user generates a unique user profile and submits it to an API endpoint. The check function verifies that the response status is 200 and that the response time is under 500 milliseconds.

Static or repetitive test data, like using "lorem ipsum" text across all requests, can lead to misleading results in performance testing. It’s vital to introduce variability in your inputs to uncover potential issues that might otherwise go unnoticed. Faker.js provides a straightforward way to generate diverse test data, helping you create more realistic and robust performance tests.

Incorporating Faker.js into your k6 load testing scripts is a small change that can yield significant benefits. It allows you to simulate a wide range of user interactions with minimal effort, ensuring that your tests are both realistic and scalable. If you haven’t tried it yet, I’d recommend giving it a go in your next test—once you see how easy it is to generate dynamic data, you’ll wonder how you ever managed without it.

Lewys
Author

Lewys

Experienced tester at a mission-critical communications company. With a focus on performance and non-functional testing, I share insights to help myself and fellow testers enhance our skills.

View Comments
Previous Post

Enhancing Performance Testing with Observability