php提到多線程,主要用到的就是pthreads和curl_multi_init(),閒暇之餘,寫了個腳本對比了下性能。php
硬件nginx
CPU型號: Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHzgit
內存: 2Ggithub
環境php7
系統:CentOS Linux release 7.2.1511 x86_64多線程
語言: php7curl
其餘:nginx+fpm性能
pthreads.php測試
<?php class Request extends Thread { public $url; public $data; public function __construct($i) { $this->key = $i; } public function run() { $this->get(); //echo "線程 ".$this->key." 完成\n"; } public function get() { $url = 'http://baike.baidu.com/link?url=nEdHS2eAR0az63Q476rLmPaWvrCTzLzr6bBNhbVyATQxlFX1VkI4ogidtzzLendMU2PI-_hcpdOZFwpytlfRc8OGc49CKl01YEJpvPe8EOu2FgdqfMAGMLDaLfkF_FRlK5brnRe90jhqUFJhn8tg_K'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); curl_close($ch); } } for($i=0;$i<100;$i++) { $tmp[$i] = new Request($i); $tmp[$i]->start(); //echo "線程".$i."啓動\n"; }
curlmulti.phpthis
<?php $url = 'http://baike.baidu.com/link?url=nEdHS2eAR0az63Q476rLmPaWvrCTzLzr6bBNhbVyATQxlFX1VkI4ogidtzzLendMU2PI-_hcpdOZFwpytlfRc8OGc49CKl01YEJpvPe8EOu2FgdqfMAGMLDaLfkF_FRlK5brnRe90jhqUFJhn8tg_K'; $mh = curl_multi_init(); for($i=0; $i<100; $i++) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle($mh, $ch); $chs[] = $ch; } do{ curl_multi_exec($mh, $running); curl_multi_select($mh); }while($running>0); foreach($chs as $ch) { curl_multi_getcontent($ch); curl_multi_remove_handle($mh, $ch); }
curl.php
<?php $url = 'http://baike.baidu.com/link?url=nEdHS2eAR0az63Q476rLmPaWvrCTzLzr6bBNhbVyATQxlFX1VkI4ogidtzzLendMU2PI-_hcpdOZFwpytlfRc8OGc49CKl01YEJpvPe8EOu2FgdqfMAGMLDaLfkF_FRlK5brnRe90jhqUFJhn8tg_K'; for($i=0; $i<100; $i++) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); }
執行100次抓取的實驗結果: | 腳本 | CPU最高波動 | 執行時間 | | --- | --- | --- | | pthreads.php | 5.5 | 7.3s | | curlmulti.php | 10 | 6.9s | | curl.php | 0.1 | 31.3s |