Skip to content

Instantly share code, notes, and snippets.

@miyasinarafat
Forked from gilbitron/curl-multi-test.php
Created May 31, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyasinarafat/0050efc08682fdb75970a310b3fd152a to your computer and use it in GitHub Desktop.
Save miyasinarafat/0050efc08682fdb75970a310b3fd152a to your computer and use it in GitHub Desktop.
Testing 95 regular curl_exec requests vs curl_multi_exec requests
<?php
$sites = [
'http://www.google.com/',
'http://www.facebook.com/',
'http://www.youtube.com/',
'http://www.yahoo.com/',
'http://www.live.com/',
'http://www.wikipedia.org/',
'http://www.baidu.com/',
'http://www.blogger.com/',
'http://www.msn.com/',
'http://www.qq.com/',
'http://www.twitter.com/',
'http://www.yahoo.co.jp/',
'http://www.google.co.in/',
'http://www.taobao.com/',
'http://www.google.de/',
'http://www.google.com.hk/',
'http://www.wordpress.com/',
'http://www.sina.com.cn/',
'http://www.amazon.com/',
'http://www.google.co.uk/',
'http://www.microsoft.com/',
'http://www.myspace.com/',
'http://www.google.fr/',
'http://www.bing.com/',
'http://www.ebay.com/',
'http://www.yandex.ru/',
'http://www.google.co.jp/',
'http://www.linkedin.com/',
'http://www.google.com.br/',
'http://www.163.com/',
'http://www.mail.ru/',
'http://www.flickr.com/',
'http://www.craigslist.org/',
'http://www.fc2.com/',
'http://www.google.it/',
'http://www.rapidshare.com/',
'http://www.conduit.com/',
'http://www.vkontakte.ru/',
'http://www.google.es/',
'http://www.soso.com/',
'http://www.bbc.co.uk/',
'http://www.imdb.com/',
'http://www.doubleclick.com/',
'http://www.livejasmin.com/',
'http://www.go.com/',
'http://www.aol.com/',
'http://www.youku.com/',
'http://www.google.com.mx/',
'http://www.apple.com/',
'http://www.googleusercontent.com/',
'http://www.sohu.com/',
'http://www.orkut.com.br/',
'http://www.pornhub.com/',
'http://www.xvideos.com/',
'http://www.google.ca/',
'http://www.cnn.com/',
'http://www.ask.com/',
'http://www.photobucket.com/',
'http://www.google.ru/',
'http://www.adobe.com/',
'http://www.youporn.com/',
'http://www.orkut.com/',
'http://www.about.com/',
'http://www.mediafire.com/',
'http://www.espn.go.com/',
'http://www.tudou.com/',
'http://www.ebay.de/',
'http://www.rakuten.co.jp/',
'http://www.files.wordpress.com/',
'http://www.4shared.com/',
'http://www.google.co.id/',
'http://www.xhamster.com/',
'http://www.google.cn/',
'http://www.cnet.com/',
'http://www.ameblo.jp/',
'http://www.google.com.tr/',
'http://www.paypal.com/',
'http://www.imageshack.us/',
'http://www.google.com.au/',
'http://www.hotfile.com/',
'http://www.livejournal.com/',
'http://www.hi5.com/',
'http://www.uol.com.br/',
'http://www.orkut.co.in/',
'http://www.sogou.com/',
'http://www.cpxinteractive.com/',
'http://www.nytimes.com/',
'http://www.livedoor.com/',
'http://www.hao123.com/',
'http://www.google.pl/',
'http://www.mixi.jp/',
'http://www.godaddy.com/',
'http://www.tube8.com/',
'http://www.tianya.cn/',
'http://www.kaixin001.com/',
];
$timeStart = microtime(true);
foreach ($sites as $site) {
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $site);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
curl_close($curl);
} catch (Exception $e) {
echo $e->getMessage();
}
}
echo 'cURL: Total execution time in seconds: ' . (microtime(true) - $timeStart) . "\n";
$timeStart = microtime(true);
$mh = curl_multi_init();
$handles = [];
$errors = [];
foreach ($sites as $site) {
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $site);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $curl);
$handles[] = $curl;
} catch (Exception $e) {
echo $e->getMessage();
}
}
$running = null;
do {
curl_multi_exec($mh, $running);
usleep(100000);
} while ($running > 0);
for ($i = 0; $i < count($handles); $i++) {
curl_multi_remove_handle($mh, $handles[$i]);
}
curl_multi_close($mh);
echo 'cURL Multi: Total execution time in seconds: ' . (microtime(true) - $timeStart) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment