[Node] Performance Test using Artillery

Performance Test - Load Test

ยท

3 min read

[Node] Performance Test using Artillery

๐Ÿ“Œ Load Testing

Load testing is a type of testing where the load is steadily increased until reaching the threshold. It is used to test how much load a server can handle, i.e., how many requests it can withstand.

When your code is actually deployed, you can identify syntax and logic issues to some extent through unit tests and integration tests. However, predicting how many simultaneous users or daily users your server can accommodate is challenging.

Even if your code is free of syntax and logic issues, the service may still experience disruptions due to hardware constraints of the server. One common example is the Out of Memory (OOM) issue, which occurs when the server's memory capacity is exceeded as the amount of memory keeps increasing due to storing information for each user.

Load testing helps to predict such issues to some extent.

๐Ÿ“Œ How to Use Artillery for Load Testing

1. Download the Module

  1. Run npm i -D artillery to install the Artillery module.

2. Perform Quick Load Testing

  1. Run npx artillery quick --count 100 -n 50 http://localhost:3000
  • Options:

    1. --count: Number of virtual users

    2. -n: Number of requests

    3. --rate: Requests per second

  • This command simulates 100 virtual users sending 50 requests each, resulting in a total of 5000 requests sent to the server.

Note: When performing load testing on a server with a live database connection, it may lead to service disruption or incur significant charges if using AWS or GCP's pay-as-you-go pricing. Therefore, it is recommended to use a staging server with similar specifications to the production server (using a test database instead of the actual database).

3. Perform Load Testing with Scenarios

During load testing, you can create scenarios that mimic the behavior of actual users or target specific APIs.

  • You can create scenarios in JSON or YAML format.

Example YAML file:

config:
  target: http://localhost:3000
  phases:
    - duration: 10
      arrivalRate: 100
scenarios:
  - name: "Retrieve all world cups"
    flow:
      - get:
          url: "/api/worldcup"
  • config section:

    • target: URL address of the server to test

    • phases: duration - test duration, arrivalRate - request rate (e.g., 100 requests per second for 10 seconds, totaling 1000 requests)

  • scenarios section:

    • name: Scenario name

    • flow: Sequence of test actions in the scenario

      • You can use GET, POST, UPDATE, PATCH, DELETE requests.

4. Save Load Testing Results

  1. Save the test results in a JSON file: npx artillery run load-test-results.json

  2. Generate an HTML report to visualize the test results: npx artillery run -o [report-name] load-test-results.json

Please note that the provided commands and examples are for demonstration purposes and may need to be adjusted based on your specific requirements and environment.

ย