Data shared between vus

Hi there.

I have the below script

let contact = http.securedPost(endpoint.CONTACTS, payload);

    if (!check(contact, {'add contact is 200': (res) => res.status === 200})) {
        errorRate.add(true);
        fail('Api did not return 200 for add contact');
    } else {
        addContactTrend.add(contact.timings.duration);
    }

    const contactId = contact.json('_id');
    console.error("@@@@@@@@@@@@@@@@@@@@@@@: " + contactId)

And I’m getting the below output.

ERRO[0006] @@@@@@@@@@@@@@@@@@@@@@@: 6229174421c8a6001bb06161  source=console
ERRO[0006] @@@@@@@@@@@@@@@@@@@@@@@: 6229174421c8a6001bb06161  source=console
ERRO[0007] @@@@@@@@@@@@@@@@@@@@@@@: 622917476bfc980015fa7eaa  source=console

As you can see the first and the second output have the same value for contactId. The issue here is with the second request that has a bug on the api where the returned entity doesn’t have the id attribute (the id attribute is missing).

My question is why is the value of the first vu saved/shared in contactId const for the second vu too. I supposed I would get undefined in such cases where a given json selector wouldn’t find the element?!

Hi @rdt.one,
can you provide a more complete example, please? The response could be different based on which part of the test life cycle your code is running.

My question is why is the value of the first vu saved/shared in contactId const for the second vu too

Can you provide more details on how you are asserting that they are different VUs, please?

Hi @codebien.

My code is running on VU life cycle. Check the whole test script I’ve written and the console output if that helps.

Summary
import * as http from '../../../util/http_utils.js';
import * as endpoint from '../../../util/endpoint.js';
import {CONTACT, updateEndpoint} from '../../../util/endpoint.js';
import {check, fail, sleep} from "k6";
import {Rate, Trend} from 'k6/metrics';
import {htmlReport} from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import {textSummary} from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';

export let errorRate = new Rate('error_rate');

let realmsTrend = new Trend('get_realms');
let addContactTrend = new Trend('add_contact');
let deleteContactTrend = new Trend('delete_contact');

export let options = {
    noConnectionReuse: true,
    stages: [
        {duration: '4s', target: 4}, // simulate ramp-up of traffic
    ],
    thresholds: {
        http_req_duration: ['p(99)<1500'], // 99% of requests must complete below 1.5s
        errorRate: [
            // more than 10% of errors will abort the test
            {threshold: 'rate<0.1', abortOnFail: true},
        ],
    }
};

export default function addContact() {

    const object = "{'definition': '', 'parentType': 'contact', 'type': 'contact'}";

    let realms = http.securedGetWithParams(endpoint.REALM, object);

    if (!check(realms, {'get realms is 200': (res) => res.status === 200})) {
        errorRate.add(true);
        fail('Api did not return 200 for realms');
    } else {
        errorRate.add(false);
        realmsTrend.add(realms.timings.duration);
    }

    // let payload = '...';

    let contact = http.securedPost(endpoint.CONTACTS, payload);

    if (!check(contact, {'add contact is 200': (res) => res.status === 200})) {
        errorRate.add(true);
        fail('Api did not return 200 for add contact');
    } else {
        addContactTrend.add(contact.timings.duration);
    }

    const contactId = contact.json('_id');
    console.log("@@@@@@@@@@@@@@@@@@@@@@@ __VU: " + __VU + " iteration: " + __ITER + " contactId: " + contactId)
    sleep(1);

    let deleteContact = http.securedDelete(updateEndpoint(CONTACT, contactId));

    if (!check(deleteContact, {'delete contact is 200': (res) => res.status === 200})) {
        errorRate.add(true);
        fail('Api did not return 200 for delete contact');
    } else {
        deleteContactTrend.add(deleteContact.timings.duration);
    }

    sleep(1);
}

export function handleSummary(data) {
    return {
        "summary.html": htmlReport(data),
        'stdout': textSummary(data, {indent: ' ', enableColors: true}), // Show the text summary to stdout...
    };
}

Console output

I’ve been using the __VU global variable to check that.

Hi @rdt.one,
considering the code, your expectations are right, you shouldn’t see any data shared across VUs/iters.
I tested k6 with a simpler script without any external library or dependency and it works as expected.

Have you asserted that the 2nd time the returned contactId is really undefined? Also, have you tried a test excluding the securedPost dependency and relying directly on the k6’s http module?

If you require more insights, it would be helpful if you could provide an example with the problem using maybe https://test.k6.io or https://httpbin.test.k6.io so I can run and debug directly your issue.

1 Like

HI @codebien.

Thanks for taking time and looking into it. The issue appears to be with the SUT and not with the data shared between VUs. Apparently the json structure is not the same every time and I’ve missed the ‘_id’ attribute while eye scanning the json object. I didn’t do any tests as you requested as the issue is clear now and I think there no point.

Thanks.