oha icon indicating copy to clipboard operation
oha copied to clipboard

How to run a load test with random JSON selection?

Open aagumin opened this issue 5 months ago • 1 comments

Hi! How can I run a test with random selection of JSON data for a POST request? For example, in k6 I can do it like this

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

var path = 'input.json';

const data = new SharedArray("some name", function () {
    const d = JSON.parse(open(path));
    console.log(d);
    return d;
});

export const options = {
    discardResponseBodies: false,
    scenarios: {
        contacts: {
            executor: 'shared-iterations',
            vus: 10,
            iterations: 1000,
            maxDuration: '100s',
        },
    },
};

export function setup() {

    var params = {
        headers: {'Content-Type': 'application/json'},
    };

    console.log(params);
    return {headers: params};
}

export default function (setup) {

    let randIndex = getRandomInt(0, data.length);
    const input_params = data[randIndex];
    // console.log(input_params);
    const headers = setup.headers;
    const res = http.post("http://127.0.0.1:8080/v2/models/bert-base-cased-squad.onnx/versions/v1/infer", JSON.stringify(input_params), headers);
    check(res, {
    'login succeeded': (r) => r.status === 200,
  })
}

export function teardown() {
}

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

// Run k6 run --out csv=test_results.csv script.js

My input.json

[
{"key":"value"},
{"key":"value"},
]

I would be glad to see a folder with examples for the most common cases of load testing.

aagumin avatar Aug 05 '25 08:08 aagumin

  -Z <BODY_PATH_LINES>
          HTTP request body from file line by line.

You can use -Z <BODY_PATH_LINES> now.

hatoo avatar Nov 04 '25 10:11 hatoo