PaulM
March 4, 2022, 2:19pm
1
Hey!
I wanted to pass a dynamic value through an array to another group. Tell me is it possible?
Code example:
const blocing_user = [];
export function set () {
var inQ = uuidv4();
blocing_user.push(inQ);
console.log("Set# Size array " + blocing_user.length);
console.log("Set# Add value " + inQ);
};
export function get () {
var outQ = blocing_user.shift();
console.log("Get# Size array " + blocing_user.length);
console.log("Get# Get value " + outQ);
};
But the Get function does not see the value:
INFO[0001] Set# Size array 1 source=console
INFO[0001] Set# Add value c135c873-cd18-41fd-8090-de0c30f4eb7f source=console
INFO[0002] Get# Size array 0 source=console
INFO[0002] Get# Get value undefined source=console
inanc
March 7, 2022, 7:51am
3
Hi Paul,
Could you please take a look at this answer and tell us whether it solves the issue.
Thanks.
PaulM
March 9, 2022, 4:06pm
4
Inanc, thanks for your answer.
I want to fix main things:
Every function creates it’s own virtual machine.
Global variable available for reading and recording inside functions, but recording values are not available for other functions.
Different ways of passing dynamic value:
3.1 Using third party data store(for example http protocol/database)
Hi @PaulM ,
I tried your code running the functions sequentially:
export default function() {
set()
get()
}
It works as expected for me returning the value on getting. How are you invoking the functions?
Can you also expand a bit on the various points, maybe adding some code examples, please?
PaulM
March 19, 2022, 7:25pm
9
Hi @codebien ,
Thanks for this example, it will help me a lot in the future.
Do I understand correctly that in your example the setup code will be one for two functions (set() and get()) ?
Example:
export const options = { //setup code
stages: [
{ duration: '2m', target: 100 },
{ duration: '5m', target: 100 },
],
};
export default function() {
set()
get()
}
@PaulM ,
can you clarify what do you mean by setup code , please? In k6
setup code is a very specific part where you define it by declaring a setup
function. You can read about it here: https://k6.io/docs/using-k6/test-life-cycle/#setup-and-teardown-stages
Regarding your posted code seems you mean the options configuration and yes, it will be the same executed iteration function for all the stages.
If you would define different execution functions then you should use the exec property from the scenarios’ common option.