Generated Token every 10 mins and use it during load test

in my scenario, I want to generate a token and use it for 10 mins (validity ) Please help me to understand more about this logic, Where should we add this code, should we add this to generate token API or before using a token

my scenario looks like
import { group } from ‘k6’;
import {check , sleep} from ‘k6’;
import http from ‘k6/http’;
import { htmlReport } from “K6 Load Test: <%= title %>”;
import { textSummary } from ‘404 - resource not found
import { SharedArray } from “k6/data”;
import papaparse from “404 - resource not found;
import encoding from ‘k6/encoding’;
import { findBetween } from ‘404 - resource not found;

export const options = {

scenarios: {
setup: { // some arbitrary scenario name for genarate token
executor: ‘per-vu-iterations’,
vus: 1,
//duration: ‘1s’,
iterations: 2,
gracefulStop: ‘0s’, // do not wait for iterations to finish in the end
tags: { test_type: ‘setup’ }, // extra tags for the metrics generated by this scenario
exec: ‘setup’, // the function this scenario will execute
},
scenario1: { // some arbitrary scenario name
executor: ‘constant-vus’,
//executor: ‘per-vu-iterations’,
vus: 15,
duration: ‘60m’,
//iterations: 10,
gracefulStop: ‘0s’, // do not wait for iterations to finish in the end
tags: { test_type: ‘scenario1’ }, // extra tags for the metrics generated by this scenario
exec: ‘scenario1’, // the function this scenario will execute
},
scenario2: { // some arbitrary scenario name
executor: ‘constant-vus’,
//executor: ‘per-vu-iterations’,
vus: 15,
duration: ‘60m’,
//iterations: 10,
gracefulStop: ‘0s’, // do not wait for iterations to finish in the end
tags: { test_type: ‘scenario2’ }, // extra tags for the metrics generated by this scenario
exec: ‘scenario2’, // the function this scenario will execute
},

},
discardResponseBodies: false,
thresholds: {
// we can set different thresholds for the different scenarios because
// of the extra metric tags we set!
‘http_req_duration{test_type:api}’: [‘p(95)<250’, ‘p(99)<350’],
‘http_req_duration{test_type:website}’: [‘p(99)<500’],
http_req_duration: [{
threshold: ‘p(90) < 500’,
threshold: ‘p(95) < 900’,
abortOnFail: false,
}]

  // we can reference the scenario names as well
  //'http_req_duration{scenario:my_api_test_2}': ['p(99)<300'],

}

};

export function setup () { //genarating token here

var url1 = ‘token genaration url’;

const payload1 = {
[“client_id”]: “13246546”,
[“client_secret”]:“466464646”,

}
const params1 = {
headers : {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Accept’ : ‘application/json’,
},

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

 };

var Response1 = http.post(url1, payload1, params1, check);

// console.log(Response1.body);
// console.log(Response1.status);
// console.log(Response1);

check(url1, {
'is Auth ': (r) => Response1.status === 200,

});

let securityToken = findBetween(Response1.body, ‘“token”:“’, ‘”,’);

return {securityToken};
//sleep(3600);

}

export default function (data) {
//console.log((data.securityToken));
let authToken = data.json(‘access_token’);
let securityToken = findBetween(Response1.body, ‘“token”:“’, ‘”,’);
let refreshToken = findBetween(Response1.body, ‘“token”:“’, ‘”,’);
console.log(refreshToken);

}

export function scenario1 (data) {

var url = ‘’;
const payload = ``;

const params = {
headers : {
‘Content-Type’ : ‘application/json’,
‘token = bearer token’ // using here

},

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

};

//console.log(payload);
const Response = http.post(url, payload, params, check);
//console.log(Response.body);
//console.log(Response.status);
//console.log(globalThis.vars [“vuAuthToken”]);
check(url, {
'123456 ': (r) => Response.status === 200,
});
sleep(0.8)
}
var refreshed = 0;

export function scenario2 (data) {
var diff = new Date() - refreshed
if (diff > 4999) {
console.log(refresh the token)
refreshed = new Date()
}
}
var url = ‘scenario2’;
const payload = ``;

const params = {
headers : {
‘Content-Type’ : ‘application/json’,
‘Authorization’: Bearer token, // token using here
},

tags: {
name: ‘1313gdgd’, // first request
},

};

//console.log(payload);
const Response = http.post(url, payload, params, check);
// console.log(Response.body);
// console.log(Response.status);
console.log(data.securityToken);

check(url, {
'134654 ': (r) => Response.status === 200,
});
sleep(0.8)
}

can anyone help me with this fix

Hi @Sreenivas

The recommendations in Periodically refreshing a token aside a script - #3 by codebien are valid for your case. Are you finding it difficult to introduce in your script?

I’ve tried to get the script from your initial message but it’s not easy. I had to replace characters (probably from the copy&paste if you are using Windows?), and some of the syntax is still not correct. It would help if you can provide a working script within block codes like below, but simplified and working, so I can reproduce your code:

import { group } from 'k6';
import { check, sleep } from 'k6';
import http from 'k6 / http';
import { htmlReport } from 'K6 Load Test: <%= title %>';
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js'
import { SharedArray } from 'k6 / data';
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import encoding from 'k6 / encoding';
import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

export const options = {
    scenarios: {
        setup: { // some arbitrary scenario name for genarate token
            executor: 'per- vu - iterations',
            vus: 1,
            //duration: '1s',
            iterations: 2,
            gracefulStop: '0s', // do not wait for iterations to finish in the end
            tags: { test_type: 'setup' }, // extra tags for the metrics generated by this scenario
            exec: 'setup', // the function this scenario will execute
        },
        scenario1: { // some arbitrary scenario name
            executor: 'constant- vus',
            //executor: 'per-vu-iterations',
            vus: 15,
            duration: '60m',
            //iterations: 10,
            gracefulStop: '0s', // do not wait for iterations to finish in the end
            tags: { test_type: 'scenario1' }, // extra tags for the metrics generated by this scenario
            exec: 'scenario1', // the function this scenario will execute
        },
        scenario2: { // some arbitrary scenario name
            executor: 'constant- vus',
            //executor: 'per-vu-iterations',
            vus: 15,
            duration: '60m',
            //iterations: 10,
            gracefulStop: '0s', // do not wait for iterations to finish in the end
            tags: { test_type: 'scenario2' }, // extra tags for the metrics generated by this scenario
            exec: 'scenario2', // the function this scenario will execute
        },

    },
    discardResponseBodies: false,
    thresholds: {
        // we can set different thresholds for the different scenarios because
        // of the extra metric tags we set!
        'http_req_duration{ test_type: api }': ['p(95) < 250', 'p(99) < 350'],
        'http_req_duration{ test_type: website }': ['p(99) < 500'],
        http_req_duration: [{
            threshold: 'p(90) < 500',
            threshold: 'p(95) < 900',
            abortOnFail: false,
        }]

        // we can reference the scenario names as well
        //'http_req_duration{scenario:my_api_test_2}': ['p(99)<300'],

    }

};

export function setup() { //genarating token here

    var url1 = 'token genaration url';

    const payload1 = {
        [“client_id”]: “13246546”,
        [“client_secret”]:“466464646”,

    }
    const params1 = {
        headers: {
            'Content - Type': 'application / x - www - form - urlencoded',
            'Accept': 'application / json',
        },

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

    };

    var Response1 = http.post(url1, payload1, params1, check);

    // console.log(Response1.body);
    // console.log(Response1.status);
    // console.log(Response1);

    check(url1, {
        'is Auth ': (r) => Response1.status === 200,

    });

    let securityToken = findBetween(Response1.body, '“token”:“', '”,');

    return { securityToken };
    //sleep(3600);

}

export default function (data) {
    //console.log((data.securityToken));
    let authToken = data.json('access_token');
    let securityToken = findBetween(Response1.body, '“token”:“', '”,');
    let refreshToken = findBetween(Response1.body, '“token”:“', '”,');
    console.log(refreshToken);

}

export function scenario1(data) {

    var url = '';
    const payload = '';

    const params = {
        headers: {
            'Content - Type': 'application / json',
            'token': 'bearer token' // using here

        },

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

    };

    //console.log(payload);
    const Response = http.post(url, payload, params, check);
    //console.log(Response.body);
    //console.log(Response.status);
    //console.log(globalThis.vars [“vuAuthToken”]);
    check(url, {
        '123456': (r) => Response.status === 200,
    });
    sleep(0.8)
}

var refreshed = 0;

export function scenario2(data) {
    var diff = new Date() - refreshed
    if (diff > 4999) {
        console.log(refresh the token)
        refreshed = new Date()
    }
    var url = 'scenario2';
    const payload = ``;

    const params = {
        headers: {
            'Content - Type': 'application / json',
            'Authorization': Bearer token, // token using here
        },

        tags: {
            name: '1313gdgd', // first request
        },

    };

    //console.log(payload);
    const Response = http.post(url, payload, params, check);
    // console.log(Response.body);
    // console.log(Response.status);
    console.log(data.securityToken);

    check(url, {
        '134654': (r) => Response.status === 200,
    });
    sleep(0.8)
}

Also a bit more context will help. I’m not familiar with the system you’re testing and interacting with. It would be really tricky to give you a satisfying answer without getting more information from you.

I see that you are creating the initial token in the setup() function which is also an scenario? You might want to have a look at the test lifecycle. This will share the token with all VUs, but after 10 minutes each VU will generate a new token. Will that work for your system?

You also have a mix of scenarios and the default() function. I would initially simplify that, work on the token refresh and then reintroduce the complexity. Once your token refresh strategy is working.

To summarize, it will help if you provide more context about your system and what you want to achieve, and to provide a simplified test that we can execute, so we can suggest a concrete alternative.

Cheers!

Thank you @eyeveebe for your support
My scenario :

  • step 1: HTTP request //generate token here and use this token for next request , but this token is valid for 10mins only
  • step 2: http request and using token (generated in step1) in headers , this step I want do it for longer duration like 1 hour or 3hours , but after 10 mins test getting failed due invalid token. In step2 I want to use new token after every 10mins

How this is possible in K6 , can we try with while loop execute step2 10mins and exit , generate token and run step 2 or any possibility

I see below code from Periodically refreshing a token aside a script, how to use this in my scenario

var refreshed = 0;

export default function() {
	var diff = new Date() - refreshed 
	if (diff > 4999) {
		console.log(`refresh the token`)
		refreshed = new Date()
	}
}

My sample k6 script

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

const OauthURL = 'https://test.com/token';
//console.log(OauthURL);
  
export default function () {

  var url1 = OauthURL; //generate token
        const payload1 = {
              ["client_id"]: "test",
              ["client_secret"]:"test",
              
}

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)
    let vuAuthToken = Response1.json('access_token');
            //console.log(vuAuthToken);


        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)
    
    
}

Hi @eyeveebe , it would be great any solution to resolve this issue.

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!

Thank you @eyeveebe , I will try this

1 Like