Want to prevent Vu to take the same item from csv which is already taken by other VU

I have series of api to hit which is in pipeline . Now I want to prevent other VU to take the item on which i am already applying series of api hit . For that particular time frame i want to prevent the item to be processed by other VU or if the item is already processed by other VU it should not be taken by anyother VU .

Hi @prishu25 can you elaborate more with sample k6 scripts?

import exec from "k6/execution";
import { SharedArray } from "k6/data";
import http from "k6/http";
import papaparse from "https://jslib.k6.io/papaparse/5.1.1/index.js";
import { check } from 'k6';

const applyHoldRawPayload = JSON.parse(open('../../resource/input/cancelorder/applyHoldCancelMod.json'));

const computeCancelRawPayload = JSON.parse(open('../../resource/input/cancelorder/cancelOrderCompute_5L.json'));

const applyCancelRawPayload = JSON.parse(open('../../resource/input/cancelorder/cancelOrderApply_5L.json'));

const loginSalesStore = new SharedArray("loginSalesStore", function () {
    console.log("Input CSV File Name = ", "loginSalesStore");
    let feed_file_data = papaparse.parse(open("../../resource/input/cancelorder/loginSalesStore.csv"), { header: true }).data;
    console.log("Reading Order from csv file");
    return feed_file_data;

const cancelOrder= new SharedArray("cancelOrder", function () {
    console.log("Input CSV File Name = ", "cancelOrder");
    let feed_file_data = papaparse.parse(open("../../resource/input/cancelorder/cancelOrder_5L.csv"), { header: true }).data;
    console.log("Reading Order from csv file");
    return feed_file_data;

function prepare5LPayload(payload, holdAppliedOrderDetails, storesDetails, cancelOrderNo) {

    payload.login_id = storesDetails.LoginId;
    payload.salesman_id = storesDetails.SalesmanID;
    payload.logged_in_store = storesDetails.Node;
    payload.order_number = cancelOrderNo.orderNo

    payload.transaction_id = holdAppliedOrderDetails.order_hold_types.order_hold_type[0].transaction_id;
    payload.order_header_key = holdAppliedOrderDetails.order_header_key;

    for (let i = 0; i < 5; i++) {
        payload.order_lines.order_line[i].order_line_key = holdAppliedOrderDetails.order_lines.order_line[i].order_line_key;
        payload.order_lines.order_line[i].qty_to_cancel = holdAppliedOrderDetails.order_lines.order_line[i].ordered_quantity;
        payload.order_lines.order_line[i].prime_line_no = holdAppliedOrderDetails.order_lines.order_line[i].prime_line_no;
    return payload;


export default function() {

    // fetching random loginSalesStore details and insert in applyHoldRawPayLaod
    let index = Math.floor(Math.random() * (loginSalesStore.length - 1 - 0 + 1)) + 0;
    const storesDetails = loginSalesStore[index];
    applyHoldRawPayload.login_id = storesDetails.LoginId;
    applyHoldRawPayload.salesman_id = storesDetails.SalesmanID;
    applyHoldRawPayload.logged_in_store = storesDetails.Node;

    //fetching random order to be cancel 
    let cancelIndex = Math.floor(Math.random() * (cancelOrder.length - 1 - 0 + 1)) + 0;
    const cancelOrderNo = cancelOrder[cancelIndex];
    applyHoldRawPayload.order_number = cancelOrderNo.orderNo


    //Adding params to request 
    const params = {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Basic Ysudulc3Q6YXV0aHBhc3N3b3Jk'


    // Applying hold and geting getOrderDetails as a successfull response
    const applyHoldResponse = http.post('https://xxxx/getOrderDetails', JSON.stringify(applyHoldRawPayload), params);
    const holdAppliedOrderDetails = applyHoldResponse.json();

    // compute payload
    const computeCancelPayload = prepare5LPayload(computeCancelRawPayload, holdAppliedOrderDetails, storesDetails, cancelOrderNo);

    // compute call made and compute response gets in return 
    const computeResponse = http.post('https://xxxx/computeCancelTotals/v2', JSON.stringify(computeCancelPayload), params);

    check(computeResponse, {
        'is status 200 for compute response': (r) => r.status === 200

    // apply payload
    const applyCancelPayload = prepare5LPayload(applyCancelRawPayload, holdAppliedOrderDetails, storesDetails, cancelOrderNo);

    //compute cancel call made 
    const applyResponse = http.post('https://xxxx/cancelOrder', JSON.stringify(applyCancelPayload), params);

    check(applyResponse, {
        'is status 200 for apply response': (r) => r.status === 200,

If u see I am fetching orders from csv files and hiting a series of apis . So i want only one vu to take a particular order . It should not be taken by any other VU if it already taken by some current VU or it is already processed by any other VU .

My Code flow is "put a hold on order-> compute total → then cancel the Order ".
No VU should pick the same order if any VU are performing these flow or If it is already performed the flow. Since I cann’t cancel an order which already cancelled .

Hi @prishu25

Would exec.scenario.iterationInTest mentioned in Question about exec.vu.idInTest - #3 by immavalls2 work for you?

You can also have a look at the Data Parameterization section in the docs, with some examples.


1 Like