Question

What is the best practice for benchmarking in PHP?

This is what I want to do:

I have setup a Saas product and I want to issue with cURL multiple parallel http requests towards that URL.

Up to now I have built a cURL function that looks like so:

function getCurl($strUrl, $arrParams, $intTimeout = null, $blnReturn = true, $blnVerbose = false, $blnHeader = false){
    if(isset($strUrl)){
        if(is_array($arrParams)){   
            $strUrl.= '?';
            foreach($arrParams as $key=>$value){
                $strUrl.=$key.'='.$value;
                $strUrl.='&';
            }
            $strUrl = substr_replace($strUrl,'',strlen($strUrl)-1,1);           
            try{
                $curl = curl_init($strUrl);
            }catch(Exception $e){
                throw new Exception('Invalid URL');
            }           
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, $blnReturn);
            curl_setopt($curl, CURLOPT_VERBOSE, $blnVerbose);
            if($intTimeout){
                curl_setopt($curl, CURLOPT_TIMEOUT, $intTimeout);
            }
            curl_setopt($curl, CURLOPT_HEADER, $blnHeader);
            if(strpos($strUrl, 'https') !== FALSE){
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            }
            $data = curl_exec($curl);       
            if($blnReturn){
                return $data;
            }
            curl_close($curl);
            return true;
        }
    }
    return false;
}

Any idea how to improve this to allow parallel reuests? Any other way to do this in respect to best practice?

Thank you!

Was it helpful?

Solution

CURL does not do parallel requests.

You will need to install puf (apt-get install puf in UB) if you want parallel HTTP requests:

puf -lc 20 -Tc 20 -Td 20 -i list-of-urls.txt

Download a \n separated list of urls from a file w/ 20 parallel connections.

You could also look at pthreads in PHP, this may do parallel requests; not sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top