pthreads和curl之间的PHP测试
我们计划建立实时投标,我们正在评估pthreads和curl之间的PHP测试,php,multithreading,curl,Php,Multithreading,Curl,我们计划建立实时投标,我们正在评估PHP与Java在吞吐量/响应时间等方面的性能。 (Java部分由团队其他成员负责) 初始启动: 我有一个测试脚本,可以与不同的服务器建立50个http连接 第一种方法 -我正在使用curl\u multi\u init函数,在7秒内得到响应 第二种方法 -我正在使用PHPpthreadsapi,试图进行并行调用,并期望相同或更少的响应时间。但平均总时间约为25秒 这是密码 <?php $g_request_arr = array(
PHP
与Java
在吞吐量/响应时间等方面的性能。
(Java部分由团队其他成员负责)
初始启动:
我有一个测试脚本,可以与不同的服务器建立50个http连接
第一种方法
-我正在使用curl\u multi\u init
函数,在7秒内得到响应
第二种方法
-我正在使用PHP
pthreadsapi
,试图进行并行调用,并期望相同或更少的响应时间。但平均总时间约为25秒
这是密码
<?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秒以下。您正在一个线程中请求所有数据,这里有一个更好的方法:
<?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个线程没有多大意义。那么Pthreads或Curl_MULTI?哪个更快?看起来不错。但我们能用游泳池达到同样的效果吗?可堆叠和池之间的区别是什么