Search code examples
k6

How to run test in sequential order in loadimpact?


We have 2 APIs which we wanted to test with load impact and the second API is the so-called dynamic target which is built upon the basis of data we get from the response of the first API.

Hence, We want to run this test sequentially. How can we achieve this?

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

    export default function() {
            let res, res_body, claim_url
            res = http.batch([req])
            check(res[0], {
                 "form data OK": function (res) {
                        console.log(res.status);
                        claim_url = JSON.parse(res.body)
                        console.log(claim_url.details.claim_uri)
                        return false;
                 }
    });

Does grouping of different APIs in a different function help?


Solution

  • You are not limited to a single http request per default function iteration in any way. So you can just use whatever you want from the previous request and do a new one.

    There is an example in the http.post documentation but here is another simple one:

    import { check, sleep } from 'k6';
    import http from 'k6/http';
    
    export default function() {
            let res, res_body, claim_url
            res = http.get(req);
    
            check(res, { // check that we actually didn't get error when getting the url
                      "response code was 200": (res) => res.status == 200,
                 });
            claim_url = JSON.parse(res.body) // if the body is "http://example.org" for example
            res2 = http.get(claim_url); // use the returned url
            check(res2, { // here it's res2 not res
                      "response code was 200": (res) => res.status == 200,
                 });
            // do more requests or checks
    
    });