Skip to content

How to run a load test with random JSON selection? #769

@aagumin

Description

@aagumin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions