Stop Init function executing multiple time

I have defined the locale variable in init function and displaying the value in init & default function. Value is getting changed in default function as init function is getting executed multiple times.

Output:

INFO[0001] Init= fr_fr                                   source=console
INFO[0001] CSV_Prod= fr_fr                               source=console
INFO[0001] CSV_SDP= fr_fr                                source=console
  execution: local
     script: .\csv_locale.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0001] Init= fr_be                                   source=console
INFO[0001] VU: 1 - ITER: 0                               source=console
INFO[0001] RandomProduct from fr_be= 0641939001          source=console

Hi @vish92, sorry for the slow reply :bowing_man:

You can not stop as it was explained in Why init called 4 times? - #2 by nedyalko all of those are needed.

Do you have a particular problem you are trying to solve?

Yes I have script with shared array which need to be declared in INIT function. I am using same variable in shared array(INIT) and group(export default function), value of variable is getting changed due to multiple execution of init function.

Again the init is executed once per each VU that needs to initialized. There is no way that we can have a VU without running it’s init context and there are no variables in the init context of a VU before we run the init context.

I would suggest that you try to show how this is breaking something for you with a some full example - it can be a much smaller version of your real script.

Below is the snippet of code, Basically I want the locale variable constant throughout init & export default function.

Code:

//importing locale value from other script
const locale1=locale;
console.log("Init= "+locale1);

const csvProduct= new SharedArray("Product",function() {
console.log("CSV_Prod= "+locale1);
return papaparse.parse(open('./Test_Data/Product_choice_'+locale+'.csv'), {header:false}).data;
});

const randomProduct= csvProduct[Math.floor(Math.random() * csvProduct.length)];


export default function () {
console.log('RandomProduct from ' +locale1+'= '+randomProduct);
}

I am confused - nothing seems to be modifying either the locale or the locale1 variables in this code snippet? :confused: What exactly is the problem?

Refer output mention above.
locale value is getting changed in export function from init function. The cause of this is multiple execution of init function resulting to multiple value of locale, export function is taking the latest value of locale.

This is not true though. The init context is executed multiple times, sure, but only once in every VU :confused: Every VU is just an independent JavaScript runtime, it has its own scope and variables and context. And, for a VU to be initialized, it needs to execute the global scope (the so called “init context”). So the exported default function will use the value of locale that was initialized in the init context of that VU.

Test data locale value is different from default function locale due to multiple executions of init.
Below is the code snippet

import { check, group, sleep } from 'k6';
import { SharedArray } from "k6/data";
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';


const locale = locale_generator();

const csvProduct= new SharedArray("Product",function() {
    console.log("TestData_locale= "+locale);
    return papaparse.parse(open('./Test_Data/Product_choice_'+locale+'.csv'), {header:false}).data;

});

export default function(){

    group('indexPage', (_) => 
    {
      console.log("Index_locale= "+locale);
      
    });

}

export function locale_generator(){
        let country_selector=randomIntBetween(1,4);
      
         switch(country_selector) {
              case 1:
                  return 'en_gb';
              case 2:
                  return 'fr_be';
              case 3:
                  return 'fr_fr';
              default:
                  return 'en_ie';
         }
    }

Output:

INFO[0001] TestData_locale= en_gb                        source=console
  execution: local
     script: .\ticket.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0001] Index_locale= en_ie                           source=console

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU