This example demonstrates a simple load test on a public HTTP REST API. It uses the faker package to generate synthetic data, and Multiple’s built-in axios HTTP client, which automatically captures request latency and status codes for you. This API we are testing requires Bearer authentication. In vuInit() each VU logs in to get a JWT, and uses the JWT to set the axios Authorization header.

Code

Environment Variables
# Base URL for the API
API_BASE_URL=https://example-api.multiple.dev
Test Spec
// faker for generating synthetic data
import { faker } from '@faker-js/faker';

class LotsOfChatMxTestSpec {
  npmDeps = {
    '@faker-js/faker': '7.6.0',
  };

  async globalInit(ctx) {
    // `globalInit` runs once at the start of the test
    // Not needed for this test
  }

  async vuInit(ctx) {
    // Set the base url of Multiple's built-in axios instance
    // The built-in axios instance automatically capture metrics
    ctx.axios.defaults.baseURL = process.env.API_BASE_URL;

    // Log in as a user and get a JWT
    const res = await ctx.axios.post('login', {
      email: `user+${ctx.info.vuId}@multiple.dev`,
      password: 'testpassword#1234',
    });
    const jwt = res.data.token;

    // Debug Run log to check we are getting the JWT correctly
    console.debug('JWT Token: ', jwt);

    // Set the authorization header
    ctx.axios.defaults.headers.common['Authorization'] = `Bearer ${jwt}`;
  }

  async vuLoop(ctx) {
    // Send a POST request to the chat endpoint with a random message
    await ctx.axios.post('chat', {
      // Generate synthetic data with faker
      message: faker.lorem.paragraph(),
    });

    // Send a GET request to the chat endpoint
    await ctx.axios.get('chat');
  }

  async vuCleanup(ctx) {
    // `vuCleanup` runs once per VU (Virtual User) after `vuLoop`
    // Not needed for this test
  }

  async globalCleanup(ctx) {
    // `globalCleanup` runs once at the end of the test after `vuCleanup`
    // Not needed for this test
  }
}