2

We are planning to building real time bidding and we are evaluating performance of PHP compare to Java in terms of throughput/response times etc. (Java part is taken care by other member of team)

Initial start:

I have a test script which makes 50 http connection to different servers.

1st approach - I am using curl_multi_init function and I get response under 7 seconds.

2nd approach - I am using PHP pthreads api and trying to make parallel calls and expecting same response time or less.But total time on average is around 25 seconds

Here is the code

   <?php

    $g_request_arr = array(
        '0' => array(
            'request_url' => 'https://www.google.co.uk/?#q=56%2B12'
        ),
        ..
        ..
        ..
        '49'=>array(
            'request_url' => 'https://www.google.co.uk/?#q=256%2B132'
        )
    );


    class ChildThread extends Thread {

        public function __construct($urls) {
            $this->data = $urls;
        }

        public function run(){

            foreach($this->data as  $url_info ){
                $url = $url_info['request_url'];
                file_get_contents($url);
            }  

            $this->synchronized(function($thread){
                $thread->notify();
            }, $this);
        }
    }

    $thread = new ChildThread($g_request_arr);
    $thread->start();
    $thread->synchronized(function($thread){    
    }, $thread);


?>

I want to know what is missing in above code or is it possible to bring the response under 7 seconds.

1
  • Then which is faster Pthreads or Curl_MULTI? Commented Sep 25, 2014 at 17:03

1 Answer 1

6

You are requesting all the data in one thread, here's a better approach:

<?php

class WebRequest extends Stackable {
    public $request_url;
    public $response_body;

    public function __construct($request_url) {
        $this->request_url = $request_url;
    }

    public function run(){
        $this->response_body = file_get_contents(
            $this->request_url);
    }
}

class WebWorker extends Worker {
    public function run(){}
}

$list = array(
    new WebRequest("http://google.com"),
    new WebRequest("http://www.php.net")
);

$max = 8;
$threads = array();
$start = microtime(true);

/* start some workers */
while (@$thread++<$max) {
    $threads[$thread] = new WebWorker();
    $threads[$thread]->start();
}

/* stack the jobs onto workers */
foreach ($list as $job) {
    $threads[array_rand($threads)]->stack(
        $job);
}

/* wait for completion */
foreach ($threads as $thread) {
    $thread->shutdown();
}

$time = microtime(true) - $start;

/* tell you all about it */
printf("Fetched %d responses in %.3f seconds\n", count($list), $time);
$length = 0;
foreach ($list as $listed) {
    $length += strlen($listed["response_body"]);
}
printf("Total of %d bytes\n", $length);
?>

This uses multiple workers, which you can adjust by changing $max. There's not much point in creating 1000 threads if you have 1000 requests to process.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks good. But can we achieve the same withing with Pool? What is the difference betweet Stackable VS Pool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.