Http request timeout

I am trying to use the timeout param for one http request that can take more than 60s to return, but it seems like I can lower the timeout, but never increase,

    apiWsdl,
    addTicketsToBasketBody,
    { timeout: '70s', tags: { "name": 'addTicketsToBasketBody' }, headers: { 'Content-Type': 'text/xml' } }
  );

Using the above results in:
avg=26.43s min=1.94s med=20.97s max=1m0s p(90)=56.27s p(95)=1m0s
Making it lower than 60s correctly loewers the max time to the expected value.

AM I doing something wrong? Or is the max timeout 60s?

Hi @Petr.Kujan !

Welcome to the community forums! :wave:

It should work!

For example, my endpoint http://127.0.0.1:8085/lorem is configured to respond in 100 seconds:

import http from 'k6/http';

export default function () {
  const params = {
    timeout: '120s',
  };
  http.post('http://127.0.0.1:8085/lorem', {}, params);
}

And it results:

     http_req_duration..............: avg=1m40s    min=1m40s    med=1m40s    max=1m40s    p(90)=1m40s    p(95)=1m40s
       { expected_response:true }...: avg=1m40s    min=1m40s    med=1m40s    max=1m40s    p(90)=1m40s    p(95)=1m40s

Which HTTP method do you use? :thinking:

It is a soap api call, this is the structure I am using for the test

let requestBody = <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.address/"> <soapenv:Header/> <soapenv:Body> soapRequestValues </soapenv:Body> </soapenv:Envelope>
let addTicketsToBasket = http.post(
apiWsdl,
requestBody,
{ timeout: ‘70s’, tags: { “name”: ‘requestBody’ }, headers: { ‘Content-Type’: ‘text/xml’ } }
);

Okay, so it POST.

But what precisely does k6’s error look like? :thinking: Is it something like:

WARN[0060] Request Failed  error="Post \"http://127.0.0.1:8085/lorem\": request timeout"

First, thank you for your time to answer this, I really appreciate it.

I do not see any actual error being shown now, but I can see the request max being returned as 1m0s

✓ { name:"addTicketsToBasketBody" }....: avg=46.55s min=6.65s med=52.7s max=1m0s p(90)=55.36s p(95)=59.35s

If I lower the timeout, it is then shown as the max in the results, but I cannot get that max over 1m0s.
And also the following calls after this one do not work as there is no return from this call.

This could be a server-side timeout, in other words, one that you yourself would not be able to change. The server should hopefully be sending back some kind of error status code when it hits its timeout though. Do you see any http_req_failed metrics?

It isn’t entirely inconceivable that the server sends back a HTTP 200 even though the request technically failed. Try console.log(response.body) for additional debugging.

1 Like

Thanks, that is something that just now occurred to me as a possible issue. I will check if that is the problem

Steps to resolve long HTTP request timeout

  1. Sleep Function
    The sleep function is an inbuilt function of PHP. It just make delay the execution of current script by specifying time in seconds. The sleep() function accepts second as parameter and give binary true/false as output.

Syntax of Sleep() function

<?php // It just print the below line. echo "This is above script from the complex script \n"; // It just sleep or don't resolve this request for 10 seconds sleep(10); // This is the below query echo "This is below script from the complex script \n"; echo file_get_contents('url'); ?>

PHP
The above script delay the execution of HTTP request by 10 second. It means that it print the first echo message and then sleep for 10 second and then again print the below message.

  1. cURL
    As we know cURL is a library which is used to make HTTP request in PHP and transferring the data in url syntax. cURL is used to make request from one server to another server and handle or transfer bulk amount of data.So, we need to enable cURL library in order to use this function. you need to install and enable cURL in you web sever before using it.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
PHP
3. Ajax or multi Ajax request
AJAX stands for Asynchronous JavaScript And XML. So the name itself describe that it works Asynchronously and handle Asynchronous request. The Asynchronous request means that the request serves one after another. No simultaneous or multiple request will handle same time.

Now we can divide our script into multiple chunks and call it through the AJAX request. So the large HTTP request is now divided into multiple small request and each AJAX request consider as single HTTP request. Now the server can this request and can able to resolve request timeout situation.

function firstAjaxrequest(){
$.ajax({
success: function(){
secondAjaxrequest();
}
});
}
function secondAjaxrequest(){
$.ajax({
success: function(){
thirdAjaxrequest();
}
});
}
function thirdAjaxrequest(){
$.ajax({
success: function(){
fourAjaxrequest();
}
});
}
JavaScript
4. Close, open and close database connectivity
Recently, I have used this technique in one of my application. Actually, I was in need to filter the data from our database based on several condition and pass those values into one of the third party API and get the result back to our application based on that input values and store those response into our database.

Here our query was also very complex to get the filtered data and the third party API is very slow to give the response. So in this case, every time my script give request timeout issue.

My solution : I just close the connection of database just above the call of third party api because on the above, i have written script to get the filtered data from our script. Once the third party API called and get the response then again I opened the database connection and store those value into our database. So I achieve to resolve this HTTP request timeout issue by using this technique.

  1. increase the max execution time of PHP script
    This is very quick fix solution for HTTP request timeout problem or handle the heavy script code. The max execution time of PHP script can be increased by two way:

(a). Add the below line in the beginning of the script where the heavy execution is happening. This will be only applicable for that page where you have used this code.

Note : This code must use from very beginning of the script.

ini_set(‘max_execution_time’, 120); // 120 (seconds) = 2 Minutes
ini_set(‘max_execution_time’, 180); // 180 (seconds) = 3 Minutes
ini_set(‘max_execution_time’, 0); // 0 = Unlimited times
PHP
Setting the max_execution_time as ‘0’ is not a good practice on live server. So I recommend it to avoid in live server.

(b). Setting the Max_Execution_Time globally in php.ini file.

The below step will show you to change the max_execution_time in php.ini file

Locate the php.ini file as per your system configuration.
Open the file and find max_execution_time
change the value of this directive
Restart your Apache web server
; Maximum execution time of each script, in seconds
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30
PHP
Here also setting the value as ‘0’, gives the unlimited execution time of your php script so this is not recommended.
This change will be applicable for your entire application as you have done the changes in php configuration file.

I have tested all the above listed technique to resolve the request timeout problem but the sleep() method and Closing and opening database connectivity is my favorite one.

Regards,
Rachel Gomez