Search code examples
k6

K6 - breakup of results for each api request


I am working on developing load testing for one workflow using HTTP request calls. In the workflow, the response of one API call is used in the next API's call either as a parameter or as a request body. So, the sequence in which API is called is very important.

My question is about the result. Is there any mechanism in K6 by which I get a breakup of how much time it took to execute each API call?


Solution

  • Yes, you can filter metrics by their (default or custom) tags. Here is an example:

    import { check } from 'k6';
    import 'http' from 'k6/http';
    
    export const options = {
      thresholds: {
        'http_req_duration{name:${}/endpoint1}': [ 'avg>0' ], // arbitrary threshold
        'http_req_duration{name:${}/endpoint2}': [ 'avg>0' ],
      },
    };
    
    export default function() {
      const server = 'http://example.com';
      const token = http.get(http.url`${server}/endpoint1`).json('token');
      check(
        http.post(http.url`${server}/endpoint2`, 'token=' + token),
        {
          'token accepted': r => r.status === 200,
        });
    }
    

    String templates tagged with http.url will automatically set the name tag for your requests, allowing you to easily filter a subset of requests.