How to deal with load tests on APIs that make costly external calls?

Hello, I’m currently trying to understand the correct approach of using Grafana k6 to load test an API route that internally makes other API calls to services that costs $$.

I have a simple use case that could help the community to understand my problem:

I want to load test a route POST /forgot-password
This route is very simple, it receives as body input the e-mail address of a user, and returns 204.
Internally, the code that handles this makes two operations:

  1. Make a database call to search for a user with this e-mail address
  2. In case the user is found, it makes a external call to another API (send-email) that sends an e-mail to the e-mail address sent as input to the route. (This costs $$)

I want to use Grafana k6 to load test this forgot-password API, but I want step 2 to be mocked, since I don’t want to send real e-mails calling the external service.

I thought about a solution, I would like to know what the Grafana k6 community thinks about it:

  • I’ll update the code that handles the forgot-password endpoint, and I’ll create a mock of the send-email API call.
  • I’ll make the code read the k6 tags sent when calls are made using Grafana k6 tool (HTTP Requests | Grafana k6 documentation), if the code detects these tags as input, I’ll inject the mock version of the send-email API call, otherwise the code will continue to work as expected (making the real call that costs $$)

It this the correct approach when dealing with load tests with APIs that makes costly external calls?

Yes, but make sure you add the correct latency(latency of the real service) to the mock you created.

1 Like