tags:

views:

359

answers:

2

All,

I have a PHP CURL request to an HTTPS site which takes a JSON and request and returns a JSON response. Usually it should not take more than 1 second. But, I have seen that the CURL response time is variable. Sometimes, it takes 4 seconds to respond and sometimes 1.2 seconds. Why is this so and How can I make it faster and process the whole request in less than a second?

Following is the curl response times for the same request in 3 different times..

Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 4.213941
    [namelookup_time] => 2.1E-5
    [connect_time] => 0.015284
    [pretransfer_time] => 0.722955
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 131
    [speed_upload] => 52
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 4.015112
    [redirect_time] => 0
)


Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 1.27581
    [namelookup_time] => 8.3E-5
    [connect_time] => 0.016223
    [pretransfer_time] => 0.104996
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 433
    [speed_upload] => 174
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.273922
    [redirect_time] => 0
)

Array
(
    [url] => https://xx.xx.xxx.xx/site/y
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 358
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 1.294147
    [namelookup_time] => 7.0E-5
    [connect_time] => 0.025723
    [pretransfer_time] => 0.123456
    [size_upload] => 222
    [size_download] => 553
    [speed_download] => 427
    [speed_upload] => 171
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.292577
    [redirect_time] => 0
)
+1  A: 

What else is the site doing ? If it has a fixed number of threads (say), and each is busy, then your request will have to wait for a thread to service it. If no threads are busy, then it'll be serviced immediately.

Of course the simplest scenario is given that the server CPU has finite resources and servicing an indeterminate number of requests, there are no service guaranteees.

Brian Agnew
This sort of makes sense.. If the site is taking the JSON request, processes it for a second, and returns back the response, why will there be variable curl times. I am not very sure if the site is doing multi-threading.
Vincent
A: 

CURL is a the best way to make HTTP requests in PHP. It's almost certainly the service that is taking time. Like waiting in line for a sandwich at the deli. If the speed of the service is out of your hands, and there are no alternative services you can use, then the best thing to do is to make sure everything else on your page continues to load while the request is being made.

Isaac