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.

caspersky 48
  • 239
  • 1
  • 4
  • 11

1 Answers1

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.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62