pthread和curl之间的PHP测试 [英] PHP testing between pthreads and curl

查看:291
本文介绍了pthread和curl之间的PHP测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正计划制作实时出价,我们正在评估 PHP 的性能,与 Java 相比吞吐量/响应时间等。
(Java部分由团队的其他成员负责)

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)

我有一个测试脚本,可将50个http连接到不同的服务器。

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

>第一种方法
- 我使用 curl_multi_init 函数,我在7秒内得到响应。

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

第二种方法
- 我使用 PHP pthreads api 尝试进行并行调用,并期待相同的响应时间或更少。但平均总时间约为25秒

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

以下是代码

   <?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);


?>

我想知道上面的代码中缺少什么,或者可以将响应时间设置为7秒。

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

推荐答案

您在一个线程中请求所有数据,这里有一个更好的方法:

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);
?>

这使用多个工作者,您可以通过更改$ max来调整。如果您有1000个要处理的请求,创建1000个主题没有多少意义。

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.

这篇关于pthread和curl之间的PHP测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆