K6 Timeout config with Syntax example

I know this might seem like a repeated post/question but it’s not, not really. I have looked and searched the forums as well as K6 documentation and have not found an answer, well at least not an answer that works in my script unless I am using it wrong (which is very likely).

I have Postman requests that I have converted to K6 scripts using the Postman-to-K6 tool. When I run the test doing multiple user requests, I get a time out error when I reach about 25 of 40 users.
The API/webservice itself is not timing out, we have looked at the logs and there are no errors.
While the test is running in K6, I go to Postman and manually run the same request from there as well. It starts off with a response time of about 9 seconds, but then as the users increase on K6 so does the wait time on Postman, up to about 1m45s, Postman still comes back with the response regardless of the wait time but K6 starts showing timeout errors. This leads me to believe that the default K6 timeout is being reached at which point K6 starts throwing the error.

Now I understand that there is a way to up the timeout, it is in the documentation of K6, but when I try to up the timeout it does not work. I cannot find a decent code sample of how exactly this is supposed to be done anywhere, support and other users just seems to refer people back to the K6 documentation which only gives theory and is not very detailed and has no practical samples. I have even used the parameters file when using the Postman-To-K6 conversion tool, it does add the adjusted timeout to the script but that does not allow the script to run, K6 just immediately throws an error, and the run does not even start.

Below is a snippet of my code, the commented-out code (when it was not commented out), and currently active code does not adjust the timeout, the script does however run without error, so the syntax is correct and K6 accepts the parameters, but it seems like K6 does not apply them.

// Auto-generated by the postman-to-k6 converter

import "./libs/shim/core.js";
import http from 'k6/http';

//let params = {timeout: '240s'}; commented out because it did not work.

export let options = { maxRedirects: 4, // timeout: '240s', commented out because it did not work.
  stages: [
    { duration: '10s', target: 5 },
    { duration: '5m', target: 40 },
    { duration: '1m', target: 5 },
    { duration: '5m', target: 40 },
    { duration: '10s', target: 0 },
  ],
  thresholds: {
    'http_req_duration': ['p(99)<1500'], // 99% of requests must complete below 1.5s
  },
};

const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
  options
});

export default function() {
  let params = {timeout: '240s'}; // currently active code but still does not work.
  postman[Request]({
    name: "Track Req Response Limit Reached K6",
    id: "sample ID",
    method: "POST",

Hi @RenierGeyer !

Welcome to the community forums! :wave:

To set a timeout of the HTTP requests in the k6 you have to use the Params object.

For example:

import http from 'k6/http';

export default function () {
  const params = {
    timeout: '360s'
};

const res = http.get('https://test.k6.io', params);

The snippet of the code looks to me incomplete, at least, I can’t see where the definitions of their postman[Request] are happening. However, I guess it’s related to the fact that the code is generated by the Postman to k6, which is no longer under our maintenance.

Maybe you could check with project maintainers how the timeouts could be specified there :thinking: GitHub - apideck-libraries/postman-to-k6: Converts Postman collections to k6 script code

Cheers!

1 Like

Good day @olegbespalov,

Thank you for your response, I have added the timeout to the export default function but it seems to just ignore it.
This is my entire script, for privacy and security purposes I have changed the actual data, but the layout of the script has remained exactly the same.

I have added “timeout: ‘240s’” to the existing export default function, just above “name”, but it does not make any difference.

When I add it as in your code snippet below I get the error below.
image

This is the entire script without any working timeout added, what would your suggestion be?

// Auto-generated by the postman-to-k6 converter

import “./libs/shim/core.js”;
import http from ‘k6/http’;

//let params = {timeout: ‘240s’}; commented out because it does nothing.

export let options = { maxRedirects: 4, //timeout: ‘240s’, commented out because it does nothing.
stages: [
{ duration: ‘10s’, target: 5 },
{ duration: ‘5m’, target: 40 },
{ duration: ‘1m’, target: 5 },
{ duration: ‘5m’, target: 40 },
{ duration: ‘10s’, target: 0 },
],
thresholds: {
‘http_req_duration’: [‘p(99)<1500’], // 99% of requests must complete below 1.5s
},
};

const Request = Symbol.for(“request”);
postman[Symbol.for(“initial”)]({
options
});

export default function() {
postman[Request]({
name: “Track Req Response Limit Reached K6”,
id: “111”,
method: “POST”,
address: “htt://thisURL.con:8022/Customer_Webservice.asmx”,
data:
‘<soapenv:Envelope xmlns:soapenv=“htt://schemas.xmlsoap.org/soap/envelope/” xmlns:web=“htt://thisURL.com/webservices”>\r\n <:Header/>\r\n <v:Body>\r\n <:GetTracking>\r\n <b:pRequest>\r\n <b:userID>…</wb:userID>\r\n \r\n <w:billTo>111</web:billTo>\r\n \r\n <w:securityToken>…</w:securityToken>\r\n \r\n\r\n \r\n \r\n \r\n \r\n \r\n \r\n <w:BeginShipDate>2022-11-09T16:00:00</w:BeginShipDate>\r\n <w:EndShipDate>2023-03-13T16:00:00</w:EndShipDate>\r\n <w:DeliveredShipmentsOnly>0</web:DeliveredShipmentsOnly>\r\n </web:pRequest>\r\n </web:GetTracking>\r\n </soapenv:Body> \r\n</soapenv:Envelope>’,
headers: {
“Content-Type”: “text/xml”
},
post(response) {
pm.test(“Status code is 200”, function() {
pm.response.to.have.status(200);
});
}
});

postman[Request]({
name: “Multi Carier Rate Request”,
id: “111”,
method: “POST”,
address: “htts://thisURL.com:8022/Customer_Webservice.asmx”,
data:
‘<soapenv:Envelope xmlns:soapenv=“htt://schemas.xmlsoap.org/soap/envelope/” xmlns:web=“htt://thisURL.com/webservices”>\r\n <v:Header/>\r\n <v:Body>\r\n <w:GetQuoteAllCarriers>\r\n <w:pRequest>\r\n <w:userID>…</w:userID>\r\n \r\n <w:billTo>…</w:billTo>\r\n \r\n <w:securityToken>…</w:securityToken>\r\n \r\n <w:origin>…</w:origin>\r\n \r\n <w:destination>…</w:destination>\r\n \r\n <w:freightlist>\r\n \r\n <w:structFreight>\r\n <w:frtClass>…</w:frtClass>\r\n <w:weight>17</w:weight>\r\n <w:length>11</w:length>\r\n <w:height>11</w:height>\r\n <w:width>11</w:width>\r\n </w:structFreight>\r\n </web:freightlist>\r\n \r\n <w:accessorials>\r\n \r\n <w:string>…</w:string>\r\n </web:accessorials>\r\n\r\n </w:pRequest>\r\n </w:GetQuoteAllCarriers>\r\n </soapenv:Body>\r\n</soapenv:Envelope>’,
headers: {
“Content-Type”: “text/xml”
},
post(response) {
pm.test(“Status code is 200”, function() {
pm.response.to.have.status(200);
});
}
});
}

You have added this as the variable, but it’s passing nowhere.

From what I see in the project’s GitHub discussion, you should create the file and convert postman collection by passing the file’s name as the flag.

Please have a look at the link:

I have done that, and when I run that script through K6 it starts with a warning message in the log that states,

"level=warning msg=“There were unknown fields in the options exported in the script” error=“json: unknown field "timeout"”

I converted a simple postman collection and played with it, so the options are passed along with the address and method.

// Auto-generated by the postman-to-k6 converter

import "./libs/shim/core.js";

export let options = { maxRedirects: 4 };

const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
  options
});

export default function() {
  postman[Request]({
    name: "demo",
    id: "ec5fe4c7-83f2-422c-aa08-5a744524381b",
    method: "GET",
    address: "http://localhost:8085/lorem",
    options: { timeout: '1200s' } // this is a place where HTTP requests options should be passed
  });
}

However, I don’t know where the postman to k6 docs are located and can’t check if it’s mentioned somewhere.

I hope that helps.
Cheers

1 Like

Ok this worked, I added it into my script after the request and before the response and it is working now.

Thank you for your assistance in this, it is greatly appreciated.

2 Likes