How to test async REST API performance

Hi, welcome to the forum!

While k6 currently doesn’t support async/await, Promises, etc. (it is on the short to mid-term roadmap, though), it sounds like you don’t actually need them, and your workflow is entirely synchronous as far as the client is concerned, so you should be able to make those requests sequentially with the current version of k6.

That is, you could have something like:

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

const baseURL = 'http://...';
const createChecks = 10; // you could also plug this in from the environment with e.g. __ENV.CREATE_CHECKS

export default function () {
  let createRes = http.post(`${baseURL}/api/createAsset`, ...);
  let assetID = createRes.json()['id'];
  let asset;
  for (let i=0; i<createChecks; i++) {
    let getRes = http.get(`${baseURL}/api/getAsset/${assetID}`, ...);
    asset = getRes.json();
    if (asset['status'] === 'finished') {
      break;
    } else {
      sleep(5);
    }
  }
  // Do something with asset
}

For measuring the time between the first POST request until when the asset is created, you can measure the elapsed time with Date.now() (see this example) and record it with a custom Trend metric, so you can have statistics about it in the end-of-test summary.

Hope this helps!

1 Like