Generated Token every 10 mins and use it during load test

Hi @Sreenivas

I believe a combination of Unique login for each virtual User - #11 by immavalls2 and the aforementioned Periodically refreshing a token aside a script - #3 by codebien can help here.

In the VU stage (default function), check if the last token refresh was 10 minutes ago, and generate a new token. You can separate the code to get a new token into another function to make it cleaner.

Based on the last piece of code you shared, I would refactor it a bit into something similar to:

import { check, sleep } from 'k6';
import http from 'k6/http';

const OauthURL = 'https://test.com/token';
let refreshed = 0;
let vuAuthToken;

export function getToken(client_id, client_secret) {
    var url1 = OauthURL; //generate token
    const payload1 = {
        ["client_id"]: client_id,
        ["client_secret"]: client_secret,
    }

    const params1 = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        tags: {
            name: 'Oauth', // first request
        },
    };
    const Response1 = http.post(url1, payload1, params1, check);
    check(url1, {
        'is Auth ': (r) => Response1.status === 200,

    });
    sleep(5); // do you need to sleep here?
    return Response1.json('access_token');
}

export default function () {
    var diff = new Date() - refreshed;
    if (diff > 9999) {
        console.log(`refresh the token`);
        vuAuthToken = getToken("test", "test");
        refreshed = new Date();
    }
    var url = 'https://test.com/source';
    const payload = `{Json Payload here}`;
    const params = {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${vuAuthToken}`,//expires in 10mins
        },

        tags: {
            name: 'test', // first request
        },

    };
    const Response = http.post(url, payload, params, check);
    check(url, {
        'is test ': (r) => Response.status === 200,
    });
    sleep(1);
}

I hope this helps.

Cheers!