API load testing

Welcome to the second part of our series on load testing your APIs. In this segment, we’ll delve into practical coding examples to put the concepts we discussed in Part 1 into action. If you haven’t read the first part yet, I recommend checking it out to get a solid foundation.

Recap of Part 1

In the first part, we explored the concept of load testing your API by simulating heavy traffic to ensure it can handle high demand. We also introduced four types of load testing:

  • Load Testing
  • Stress Testing
  • Spike Testing
  • Soak Testing

Introducing Grafana K6

Using k6, you can test the reliability and performance of your application and infrastructure.

k6 helps engineering teams prevent errors and SLO breaches, enabling them to build resilient and high-performing applications that scale.

Engineering teams, including Developers, QA Engineers, SDETs, and SREs, commonly use k6 for:

  • Load and performance testing: k6 is optimized for minimal resource consumption and designed for running high-load performance tests such as spike, stress, or soak tests.

 

  • Browser performance testing: Through the k6 browser API, you can run browser-based performance tests and collect browser metrics to identify performance issues related to browsers. Additionally, you can mix browser tests with other performance tests to get a comprehensive view of your website’s performance.

 

  • Performance and synthetic monitoring: You can schedule tests to run with minimal load very frequently, continuously validating the performance and availability of your production environment. For this, you can also use Grafana Cloud Synthetic Monitoring, which supports running k6 scripts.

 

  • Automation of performance tests: k6 integrates seamlessly with CI/CD and automation tools, enabling engineering teams to automate performance testing as part of their development and release cycle.

 

  • Chaos and resilience testing: You can use k6 to simulate traffic as part of your chaos experiments, trigger them from your k6 tests or inject different types of faults in Kubernetes with xk6-disruptor.

 

  • Infrastructure testing: With k6 extensions, you can add support to k6 for new protocols or use a particular client to directly test individual systems within your infrastructure.

Let’s Code

Simple Testing

This test sends requests to an endpoint as quickly as possible for 5 seconds.

Command:


docker run -i loadimpact/k6 run - <1-simple.js

Code:


import http from 'k6/http';
import { check } from 'k6';

export let options = {
  noConnectionReuse: false,
  vus: 1,
  duration: '5s',
};

export default () => {
  const res = http.get('https://your.api.com');
  check(res, { 'status was 200': (r) => r.status == 200 });
}

Simple Testing – Multiple Endpoints

This test sends requests to multiple endpoints as quickly as possible for 5 seconds.

Command:


docker run -i loadimpact/k6 run - <1-simple-multiple.js

Code:


import http from 'k6/http';
import { check } from 'k6';

export let options = {
  noConnectionReuse: false,
  vus: 1,
  duration: '5s',
};

export default () => {
  const res = http.batch({
    'my-first-api': { method: 'GET', url: 'https://your.api.com' },
    'my-second-api': { method: 'GET', url: 'https://your.second.api.com' }
  });

  check(res['my-first-api'], { 'docker res status was 200': (r) => r.status == 200 });
  check(res['my-second-api'], { 'api gateway res status was 200': (r) => r.status == 200 });
}

Conclusion

Load testing isn’t just a checkbox—it’s a critical part of building systems that hold up under pressure. In this second part, we covered real k6 code examples to help you simulate load on your APIs, validate uptime, and uncover performance bottlenecks before they impact users.

Want to go further?

  • Integrate these tests into your CI/CD
  • Use Grafana dashboards to track performance trends
  • Combine load testing with synthetic monitoring for ongoing insight

Want to avoid performance surprises?

Book a discovery session with our team to review your current approach to API performance and identify gaps before they hit production.