Global scope in setup

if I do changes to a global variable in setup it is not reflected outside of setup(),
is it because setup runs once while global runs per vu.

// clobals
var mycache = __ENV.CACHE || {};

inside setup()
      // cache the groupid so we do not look it up
      if(!(testenvs[i] in mycache)) {
        mycache[testenvs[i]] = {};
      }
      if(!(verticals[j] in mycache[testenvs[i]])) {
        mycache[testenvs[i]][verticals[j]] = {};
        if(!('groupid' in mycache[testenvs[i]][verticals[j]])) {
          mycache[testenvs[i]][verticals[j]]['groupid'] = groupadd(testenvs[i], verticals[j], accessToken, groupname);
        }

in setup cache look like this:

INFO[0002] cache: {"test12":{"trk":{"groupid":"1415731c-9fb0-103d-9c6d-7bbe67e726f1"}}}  source=console  

inside default():

            // cache
            console.log(`cache: ${JSON.stringify(mycache)}`);
            let groupid = mycache[testenvs[i]][verticals[j]]['groupid'];

and I get this:

INFO[0012] cache: {}                                     source=console
ERRO[0012] TypeError: Cannot read property 'trk' of undefined                                                                                                                                                                                                                                       
running at file:///C:/dist/work/fido2-performance-test/fido2-scim-groups.js:324:47(57)  executor=constant-vus scenario=default source=stacktrace   

I works if I add it into default(), but it is more natural to do this in setup() where group is prepared.

Hi @mortenb123,

As I mentioned in your other question this won’t work as VUs are completely separate and setup is executed in a separate VU once.

You can see more detailed explanation here

1 Like